[英]How to install vue js in laravel 8 without jetstream and Inertia?
[英]Laravel + Inertia + Vue Js -> cant accsess to hasMany attributes
我是 Laravel、Inertiy 和 Vue JS 的新手。 我必須和他們一起建立一個項目,這真的很酷。
現在我有一個問題。 我有幾款游戲,每款游戲都有很多廣告。 如果我通過以下方式獲得游戲:
$games = Game::where(['id' => auth()->user()->current_team_id])->orderBy('created_at', 'DESC')->with('theAds')->get();
在 Game.php 中:
/**
* @return HasMany
*/
public function theAds(): HasMany
{
return $this->hasMany(TheAds::class);
}
在 TheAds.php 中:
/**
* @return BelongsTo
*/
public function game()
{
return $this->belongsTo(Game::class);
}
現在,如果我 var_dump($games[x]['theAds']->count()) 它會轉儲每個游戲的正確計數。 但是如果我像這樣在 Game.vue 中使用它:
<tr v-for="(game, i) in $page.props.games" :key="game.id">
<td class="text-center">{{ game.name }}</td>
<td class="text-center">{{ game.theAds }}</td>
</tr>
它會引發錯誤,即 game.theAds 未定義,但 game.name 設置正確。
請問有人能幫幫我嗎?
問候米奇
是的,那是因為試圖從 js 中的關系中獲取數據已經太遲了。 您應該在“雄辯模型”轉換為 JSON 之前獲取此數據
$games = Game::where(['id' => auth()->user()->current_team_id])->orderBy('created_at', 'DESC')->with('theAds')
->get()
->map(function ($e) {
$e->theAds = $e->theAds;
return $e;
})
或者您可以創建“ResourceCollection”,幫助將“雄辯模型集合”轉換為 JSON,然后您就可以訪問相關數據https://laravel.com/docs/8.x/eloquent-resources#resource-collections
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.