Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

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);
Last updated 2 years ago.
0

Tnx ChrisRM, but I would like only certain fields

Last updated 2 years ago.
0

That's what get(array()) does. You can define which columns to retrieve in the same way as you do select(array()).

Last updated 2 years ago.
0

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();
Last updated 2 years ago.
0

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?

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

matiux matiux Joined 28 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.