vendredi 8 juillet 2016

Conversion from query builder to eloquent model with 3 tables

How to convert the following query to eloquent model in Laravel 5?

    $city = DB::table('cities')
        ->join('provinces', 'cities.province_id', '=', 'provinces.id')
        ->join('countries', 'provinces.country_id', '=', 'countries.id')
        ->select('cities.name as City','provinces.name as Province','countries.name as Country')
        ->where('cities.isDelete', '=', '0')
        ->get();

My table structures are:

  1. Country: id, name

  2. Province: id, name, country_id

  3. City: id, name, province_id

I've done working with the eloquent model with 2 tables but I don't know how to manage 3 tables. P.S. I have properly defined my relationships in model.

Aucun commentaire:

Enregistrer un commentaire