jeudi 11 octobre 2018

Laravel relation one to many get results joined

I´m kinda lost one this one, i really can´t find the problem, i have been watching a lot of questions all over the web but still can´t seem to put this working properly.

I have two tables, the tabelaAngaricao table and the tabelaFotos table with a relationship of one-to-many, meaning that a tabelaAngariacao can have many tabelaFotos, and tabelaFotos as a angariacaoid(foreign key) so my tabelaAngariacao model is:

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\TabelaFotos;

class TabelaAngariacao extends Model
{
    protected $table = 'tabelaAngariacao'; 
    public function TabelaFotos()
    {
        return $this->hasMany(TabelaFotos::class, 'angariacaoid');
    }
}

and my tabelaFotos model is:

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\TabelaAngariacao;

class TabelaFotos extends Model
{
    protected $table = 'tabelaFotos'; 

    public function TabelaAngariacao()
    {
        return $this->belongsTo('App\TabelaAngariacao');
    }
}

What i want is to get all results joined by the angariacaoid, so in my controller i have:

public function index()
    {

        $results = DB::table('tabelaAngariacao')
            ->leftJoin('tabelaFotos', 'tabelaAngariacao.id', '=', 'tabelaFotos.angariacaoid')
            ->select('tabelaAngariacao.*')
            ->get();

    }

Could someone help me on finding the problem? what is that i´m doing wrong?

Regards



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire