2017-08-26 56 views
-1

我試圖寫在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" 
      ] 

回答

0

將字段添加到with()參數:

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

不worknig 調用未定義的關係[sector_name]型號[應用程序\型號\部門。 –