First, you need to setup relations. After that, you can run your query like this;
$province = Province::with('regioni')->get(array(
'province.id AS provincia_id',
'province.name AS provincia',
...
));
var_dump($province);
Tnx ChrisRM, but I would like only certain fields
That's what get(array())
does. You can define which columns to retrieve in the same way as you do select(array())
.
I need of a "where" clause to filter only some records: How can I convert this query?
return \DB::table('province')
->join('regioni', 'province'.'.regione_id', '=', 'regioni.id')
->select('province'.'.id', 'province'.'.name AS provincia', 'province'.'.sigla', 'province'.'.slug',
'regioni.name AS regione', 'regioni.slug AS regione_slug', 'regioni.id AS regione_id')
->where($relationName.'.slug', $slug)
->get();
I have try this:
return self::with(array('regione', 'provincia'))->whereHas($relationName, function($q) use ($slug) {
$q->where('slug', $slug);
})->get();
and works!
I there a best way?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community