vendredi 2 juin 2017

Laravel: How to fetched JOINed data in models which are in relationship?

I have following query:

select table1.col as col, 
table3.updated_at as mod_date
from table1
join table2 on table2.communicator_id = communicators.id
join quotes on table2.quote_id = table3.id
Where year(table3.updated_at) = 2017
group by col
order by mod_date DESC

Now in Model Table there is a relationship defined as:

public function table3() {

        return $this->belongsToMany( 'App\Models\Table2', 'table3', 'table1_id' );

    }

How can I achieve result of query given above by using Relationships? I did following but it did not work:

$communicators = Table1::whereYear( 'created_at', '=', $year )->orderBy( 'created_at' )
                                     ->select( \DB::raw( 'table1.col as col' ) )
                                     ->groupBy( 'col' )->limit(20)->toSql();



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2rOZoni
via IFTTT

Aucun commentaire:

Enregistrer un commentaire