0
データベースに2つのテーブル、Agency
とVehicle
があります。各車両は代理店に属し、代理店には多数の車両があります。2つのデータベーステーブルにlaravelの表現で結合する
Agency
モデル::
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Agency extends Model
{
protected $fillable = ['name', 'location'];
public function trains(){
return $this->hasMany('App\Vehicle');
}
}
Vehicle
モデル:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Vehicle extends Model
{
protected $fillable = [
'agency_id',
'registration_number',
'description'
];
public function agency(){
return $this->belongsTo('App\Agency');
}
}
今、私は私のコントローラからそれに対応する機関と各train
を含むでJSONオブジェクトを返すことができますどのようにここに私のモデルは? agency_idに基づいてテーブルに加わる必要がありますか?これを行う最善の方法は何ですか?任意の助け
感謝:)