1
私はフライトを示すアプリを開発中です。フライト選択ワークフローでは、まず目的地を選択する必要があります。私の "目的地ビューを選ぶ"では、データベースにすべての国を表示します。その中には飛行機がないものもあります。 「私はどここれらのキーがわからないLaravelは関連するモデルをチェックします(より良い解決法ですか?)
public function getDestinationsWithFlights()
{
$destinations = Destination::all();
$destinationsWithFlights = [];
for ($i = 0; $i < count($destinations) ; $i++)
{
if ($destinations[$i]->hasMany(Flight::class)->count() != 0)
{
$destinationsWithFlights[$i] = $destinations[$i];
}
}
return $destinationsWithFlights;
}
応答が
{
"0": {
"id": 1,
"country": "argentina",
"created_at": "2016-08-03 13:56:36",
"updated_at": "2016-08-03 13:56:36"
},
"2": {
"id": 3,
"country": "spain",
"created_at": "2016-08-03 13:56:36",
"updated_at": "2016-08-03 13:56:36"
}
}
です:私は、私はこの方法を書いて、それを達成するために、航空券を持つだけで国を表示しようとしています0 "と" 2 "は、から来た。そして、私はそこで最も良い解決策があると思います。また、私はこのような応答を好むでしょう:
{
"id": 1,
"country": "argentina",
"created_at": "2016-08-03 13:56:36",
"updated_at": "2016-08-03 13:56:36"
},
{
"id": 3,
"country": "spain",
"created_at": "2016-08-03 13:56:36",
"updated_at": "2016-08-03 13:56:36"
}
あなたはどんな考えですか?それとも私がやったようにうまくいくと思いますか?
:
あなた
getDestinationsWithFlights
機能のようなものになります正確に私が必要としたもの。私はすでに関係を設定していましたが、クエリにメソッドを追加できるかどうかはわかりませんでした。ありがとう!たくさん! –