php - eloquent with multiples tables data and relationship -


i have these 3 tables:

  schema::create('companies', function (blueprint $table) {             $table->increments('id');             $table->integer('city_id')->unsigned();             $table->string('name');             $table->string('address');             $table->float('lat', 10,6);             $table->float('lng', 10,6);             $table->timestamps();              $table->foreign('city_id')->references('id')->on('cities');         });  schema::create('company_clients', function (blueprint $table) {     $table->increments('id');     $table->integer('company_id')->unsigned();     $table->integer('client_id')->unsigned();      $table->foreign('company_id')->references('id')->on('companies');     $table->foreign('client_id')->references('id')->on('companies'); });  schema::create('cities', function (blueprint $table) {     $table->increments('id');     $table->string('name'); }); 

now, want have eloquent query, return array (not 1 item) of companies, (for example)company_id=1 on company_clients table. also, city_id suppose return name , not id, using cities table. cannot imagine how right now. made:

class city extends model {     protected $table = 'cities';      public $timestamps = false;      protected $fillable = [         'name',     ];       public function companies()     {         return $this->hasmany('app\company', 'city_id', 'id');     } }   class companyclients extends model {     protected $table = 'company_clients';      public $timestamps = false;      protected $fillable = [         'company_id', 'client_id',     ];      public function companies()     {         return $this->belongsto('app\company', 'company_id', 'id');     }      public function clients()     {         return $this->belongsto('app\company', 'company_id', 'id');     } }  class company extends model {     protected $table = 'companies';      protected $fillable = [         'name', 'address', 'lat', 'lng', 'city_id',     ];      protected $hidden = [         'clients', 'created_at', 'updated_at',     ];      public function city()     {         return $this->belongsto('app\city', 'city_id', 'id');     }      public function companies()     {         return $this->hasmany('app\companyclients', 'company_id', 'id');     }      public function clients()     {         return $this->hasmany('app\companyclients', 'client_id', 'id');     } } 

but, i'm missing code in controller. tried:

$result = company::leftjoin('company_clients', function($join) {             $join->on('companies.id', '=', 'company_clients.company_id');         })->where('company_clients.company_id', '=', 1 )->get(); 

or

    $result = company::with(['clients' => function($q){         $q->where('company_id', 1);     }])->get(); 

but not returning correct result. i'm missing?

thanks!

edited: had found way, i'm not sure if best way it. can please confirm?

    $result = company::join('company_clients', function($join) {         $user = auth::guard('api')->user();         $join->on('companies.id', '=', 'company_clients.client_id')->where('company_clients.company_id', '=', $user->company_id );     })->join('cities', 'cities.id', '=', 'companies.city_id')->get(array('companies.*', 'cities.name')); 

try

companyclients::with('company.city', 'clients')->where('company_id', 1)->get(); 

rename

companies

relationship on companyclients model

company


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -