mercredi 27 janvier 2016

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, I want to have an eloquent query, where it will return an array (Not only one item) of companies, where the (for example)company_id=1 on company_clients table. Also, the city_id is suppose to return the name and not the id, using the cities table. I cannot imagine how to do it right now. I 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 the code in the controller. I 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 is not returning the correct result. What I'm missing?

Thanks!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire