如何在Laravel中编写雄辩的联接

时间:2017-08-26 09:05:28

标签: php mysql laravel

我正在尝试在laravel中编写雄辩的连接

工作表

id
job_title
sector_id

部门表

id
Sector

行业模式

class sector extends Model
{
    protected $fillable = ['sector_name'];


    public function newapaper(){
        return $this->hasMany('App\Models\newspaper_jobad');
    }
}

工作模式

class job extends Model
{
    public function sector(){
        return $this->belongsTo('App\Models\newspaper_jobad');
    }
}

执行以下查询时

$var = newspaper_jobad::with('sector')->get();;

dd($var);

它给了我这个结果

[
        "id" => 1
        "job_title" => 1
        "sector_id" => 1
        "created_at" => "2017-08-19 16:46:43"
        "updated_at" => "2017-08-20 13:14:02"
        ]

但我想要使用的是以雄辩的方式加入而不是直接查询构建器类

 [
            "id" => 1
            "job_title" => 1
            "sector_id" => 1
            "sector" => Governamnet
            "created_at" => "2017-08-19 16:46:43"
            "updated_at" => "2017-08-20 13:14:02"
            ]

1 个答案:

答案 0 :(得分:0)

将字段添加到with()参数:

$var = newspaper_jobad::with('sector.Sector')->get();
dd($var);