Foreach with PHP StdClass

3

My page shows posts that have been registered to the database. Some posts have files that have been uploaded. These files are saved in server folders and the database has the table "Pivot_files" with columns ID, Post, Name and Size.

$postagens = DB::table('postagens')
                ->where('dsa', '=', $id)
                ->OrderBy('Data_publicacao', 'Desc')
                ->get();

foreach ($postagens as $postagem => $post)
{
  $arquivos_postagens[$post['Id_postagem']] = DB::table('arquivos_postagens')
  ->where('postagem','=', $post['Id_postagem'])
  ->first();
}

I'm now trying to use the above foreach to traverse the posts and perform another select in the "PivotTable" table where the "post" column receives the value of the post id and stores the result in another array so I send it to the page and can work with each of them.

However, it is not working. I searched and saw some Array videos in PHP but did not get the information I needed. Could you help me?

Thank you

Bank:

Table Posts Fields: ID_Post, date_publication, description, discipline (dsa), Title

Table File Fields: File_ID, Name, Post (PID_ID), Size

Select desired:

Assuming the id_id is equal to 5, I want it to return a table like this:

File_ID Name Post Size Size 3 leiame.txt 5 1024
4 foto.jpg 5 6780
5 aula.pdf 5 12304

    
asked by anonymous 23.11.2016 / 08:34

2 answers

3

I do not know if your DB class allows you to run query queries, but, you could try JOINs .

A practical example for this query would be:

SELECT p.*, ap.dado1, ap.dado2, ap.dado3 FROM postagens p
JOIN Arquivos_postagem ap
ON p.dsa = ap.postagem
WHERE dsa = {$id}
ORDER BY Data_publicacao DESC

Where on lines 2 and 3 I merge another table to pull its data and on line 1 I specify between SELECT and FROM what data I want from each table. Please read here to learn more about JOINs.

    
23.11.2016 / 10:20
0

If the relationship is 1 for many strong> could be done Eloquent Model , which will make it easy to load your relationships :

Posts

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Postagens extends Model
{
    protected $primaryKey = 'ID_Postagem';
    protected $fillable = array('data_publicacao',
                                'descricao',
                                'dsa',
                                'titulo');
    protected $table = 'Postagens';
    public $timestamps = false;
    // aqui seria a relação de 1 para muitos...
    public function arquivos()
    {
        return $this->hasMany(ArquivoPostagens::class, 'postagem','ID_Postagem');
    }

}

ArchivePosts

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class ArquivoPostagens extends Model
{
    protected $primaryKey = 'ID_arquivo';
    protected $fillable = array('nome',
                                'postagem',
                                'tamanho');
    protected $table = 'Arquivo_postagens';
    public $timestamps = false;
    // relação onde 1 ArquivoPostagens é de uma postagem
    public function postagem()
    {
        return $this->belongsTo(Postagens::class, 'postagem','ID_Postagem');
    }

}

How to use:

To load the relationship use the with command, so that at the moment of the creation is also loaded the relation:

$postagens = Postagens::with(['arquivos'])
                      ->where('dsa', '=', $id)
                      ->orderBy('Data_publicacao', 'Desc')
                      ->get();

Within the variable $postagens if there is a relation it will have a arquivos item with the relation of the files to that determined $postagens . That way you do not have to worry about searching the files separately, the has the function to bring the data according to the relation. I do not see any reasons then to use DB:: in this case because of the fact that for each item of the post it is done Select , this can cause a certain slowness in the loading process.

References:

23.11.2016 / 13:30