doudun1029 2016-01-27 15:29
浏览 24
已采纳

雄辩的多表数据和关系

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!

EDITED: I had found a way, but I'm not sure if is the best way to do it. Can someone 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'));
  • 写回答

1条回答 默认 最新

  • drkenap147751 2016-01-27 16:26
    关注

    Try

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

    Rename

    companies

    relationship on the CompanyClients model to

    company

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据