私はdeliveryForecastモデルを持っており、2カラムに基づいて複数のテーブルからデータを取得するには雄弁を作成する必要があります。Laravel Eloquent複数の関連テーブルからデータを取得
これは私がモデルに雄弁を作成することができている質問私delivery_forecastsテーブル
Schema::create('delivery_forecasts', function (Blueprint $table) {
$table->increments('id');
$table->enum('type',array('Quotation','Demo','Service Unit','Freebie','Back Job','Site Visit'));
$table->string('type_id');
$table->date('transaction_date');
$table->time('transaction_time')->nullable();
$table->enum('status',array('Pending','Approved','Cancelled'))->default('Pending');
$table->boolean('queue')->default(0);
$table->timestamps();
});
のですか?どのように状態を作るか?例えば
:
class DeliveryForecast extends Model
{
public function documents(){
if(DeliveryForecast->type == 'Quotation'){
return $this->belongsTo('App\Quotation','type_id','id');
}
if(DeliveryForecast->type == 'Demo'){
return $this->belongsTo('App\Demo','type_id','id');
}
if(DeliveryForecast->type == 'Service Unit'){
return $this->belongsTo('App\ServiceUnit','type_id','id');
}
and so on .....
}
}
私は雄弁に条件を作成するためのアイデアを持っていないと、私のクエリは次のようにする必要があります:
$delivery_forecast = DeliveryForecast::with('documents')
->get();
任意のアイデアの男?前もって感謝します。
あなたのシナリオでEloquentのドキュメント – Scopey
の "polymorphic"セクションを見てみると、個々の関係ごとに 'DeliveryForecast'モデル内に複数の関数を作成する必要があります。 –