我正在嘗試從 url 獲取 id 查詢引數。之后,我想比較 url 中的 ID 和資料庫中的藝術品 ID 是否相同,然后進行其他操作。
但是,where 子句中的 $artworks()->id 被視為未定義變數。問題是未定義的變數:藝術品。我可以知道我做錯了什么嗎?
public function show($id)
{
if(filled($id)){
$artworks = Artwork::all()->where($id,'=',$artworks()->id);
$categories = Category::all();
$artist = User::all();
return view('pages.artwork-detail', compact('categories', 'artworks','artist'));
}else{
echo("The id does not exist.");
}
}
uj5u.com熱心網友回復:
你可以這樣做:
public function show($id)
{
if(filled($id)){
$artworks = Artwork::find($id);
$categories = Category::all();
$artist = User::all();
return view('pages.artwork-detail', compact('categories',
'artworks','artist'));
}else{
echo("The id does not exist.");
}
}
uj5u.com熱心網友回復:
我不知道我的答案是否有效,但我可以明確地解決您代碼中的一些問題。
public function show($id)
{
if(!filled($id)) return echo("The id does not exist.");
// I don't know $id belongs to what. If it's artworks' id:
$artworks = Artwork::findOrFail($id);
// Otherwise, let's say it's user_id on the artworks table:
$artworks = Artwork::where('user_id', $id)->get();
// But if it's, use the following line instead of the other one.
$categories = Category::all();
$artist = User::all();
return view(
'pages.artwork-detail',
compact('categories', 'artworks', 'artist')
);
}
uj5u.com熱心網友回復:
試試這些 where 條件
$artworks = Artwork::where('id','=', $id)->get();
$artworks = Artwork::where('id', $id)->get();
$artworks = Artwork::find($id);
如果您只需要一張記錄,請嘗試這些 where 條件
$artworks = Artwork::where('id','=', $id)->first();
$artworks = Artwork::where('id', $id)->first();
$artworks = Artwork::find($id);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/452478.html
