编辑现有帖子时 ::find() 中的 Laravel 未定义变量
Lavavel Undefined Variable in ::find() when editing existing posts
我创建了一个功能来编辑我的 laravel 网站上现有的 post。但是,当我尝试提交时该网站出现错误。它声明了一个 Undefined 变量:slug 如果您在下面看到我的示例,它就没有意义,因为我的 Post 日志的响应显示了目标文件中的变量。
public function adminEditPostsPost(Request $request)
{
$user = Auth::user()->first();
// Log::info($request->all());
$update = false;
// $token = $request->_token;
$token = $request->session()->token();
$postId = $request->input('pxp-submit-comm-id');
$title = $request->input('pxp-submit-comm-title');
$overview = $request->input('pxp-submit-comm-overview');
$post_status = $request->input('pxp-submit-post-status');
//$images_array = $request->input('pxp-submit-comm-images-array');
$photo = $request->input('pxp-submit-comm-photo-featured');
// Log::info($slug);
$id = $postId;
$prop_id = 0;
if ($request->is('admin/*')) {
$posts = Posts::find($postId);
$posts->$title = $title;
Log::info($posts);
$posts->$slug = $request->input('pxp-submit-comm-slug');
$posts->$content = $overview;
$posts->$featured_url = $photo;
$posts->$post_status = $post_status;
$posts->save();
}
$photos = Photos::where('enabled', '<=', '4')->get()->sortByDesc("created_at");
$posts = Posts::orderBy('created_at', 'DESC')->where('post_status', '<=', '1')->get();
return view('admin.posts')->with('photos', $photos)->with('posts', $posts);
}
这是 post 变量的日志:
{
"id": 8,
"slug": "two-homes-now-under-construction-for-late-spring-move-in-dates",
"title": "Two Homes NOW Under Construction for Late Spring Move-in Dates",
"featured_url": "146",
"content": "<p></p>",
"excerpt": null,
"category": null,
"tags": null,
"created_at": "2021-02-16T15:49:44.000000Z",
"updated_at": "2021-02-16T15:49:44.000000Z",
"post_status": "1",
"Two Homes NOW Under Construction for Late Spring Move-in Dates": "Two Homes NOW Under Construction for Late Spring Move-in Dates"
}
在对象的底部,我注意到它有“两栋正在建设中的房屋,以备晚 Spring 入住日期”作为关键,这对我来说没有意义,我想知道是否我的代码中保存了一些本不应该保存的内容,或者由于它不在数据库中,所以一开始是如何保存的。
如果 $title
变量包含 notVariable
,则 $posts->$title
等同于 $posts->notVariable
。所以你只需要在 $posts
key/attribute.
上删除 $
改变
$posts->$title = $title;
Log::info($posts);
$posts->$slug = $request->input('pxp-submit-comm-slug');
$posts->$content = $overview;
$posts->$featured_url = $photo;
$posts->$post_status = $post_status;
到
$posts->title = $title;
Log::info($posts);
$posts->slug = $request->input('pxp-submit-comm-slug');
$posts->content = $overview;
$posts->featured_url = $photo;
$posts->post_status = $post_status;
PHP 允许使用 variable variables as well as using variables as object keys (see this post 以获得更多解释)。您似乎无意中采用了这一原则。
在您设置的 Log::info()
语句之前
$posts->$title = $title
在 PHP 中,这实际上是将键设置为 $title
s 值,这解释了对象中的“现在正在建设的两栋房屋,晚些 Spring 入住日期”键.您没有收到 Undefined variable 错误 $title
因为该变量存在。您收到 Undefined variable: slug 错误,因为您试图使用不存在的变量 $slug
、$content
等来设置其他键.您需要更新代码以删除箭头后的 $
。
$posts->title = $title;
$posts->slug = $request->input('pxp-submit-comm-slug');
$posts->content = $overview;
$posts->featured_url = $photo;
$posts->post_status = $post_status;
我创建了一个功能来编辑我的 laravel 网站上现有的 post。但是,当我尝试提交时该网站出现错误。它声明了一个 Undefined 变量:slug 如果您在下面看到我的示例,它就没有意义,因为我的 Post 日志的响应显示了目标文件中的变量。
public function adminEditPostsPost(Request $request)
{
$user = Auth::user()->first();
// Log::info($request->all());
$update = false;
// $token = $request->_token;
$token = $request->session()->token();
$postId = $request->input('pxp-submit-comm-id');
$title = $request->input('pxp-submit-comm-title');
$overview = $request->input('pxp-submit-comm-overview');
$post_status = $request->input('pxp-submit-post-status');
//$images_array = $request->input('pxp-submit-comm-images-array');
$photo = $request->input('pxp-submit-comm-photo-featured');
// Log::info($slug);
$id = $postId;
$prop_id = 0;
if ($request->is('admin/*')) {
$posts = Posts::find($postId);
$posts->$title = $title;
Log::info($posts);
$posts->$slug = $request->input('pxp-submit-comm-slug');
$posts->$content = $overview;
$posts->$featured_url = $photo;
$posts->$post_status = $post_status;
$posts->save();
}
$photos = Photos::where('enabled', '<=', '4')->get()->sortByDesc("created_at");
$posts = Posts::orderBy('created_at', 'DESC')->where('post_status', '<=', '1')->get();
return view('admin.posts')->with('photos', $photos)->with('posts', $posts);
}
这是 post 变量的日志:
{
"id": 8,
"slug": "two-homes-now-under-construction-for-late-spring-move-in-dates",
"title": "Two Homes NOW Under Construction for Late Spring Move-in Dates",
"featured_url": "146",
"content": "<p></p>",
"excerpt": null,
"category": null,
"tags": null,
"created_at": "2021-02-16T15:49:44.000000Z",
"updated_at": "2021-02-16T15:49:44.000000Z",
"post_status": "1",
"Two Homes NOW Under Construction for Late Spring Move-in Dates": "Two Homes NOW Under Construction for Late Spring Move-in Dates"
}
在对象的底部,我注意到它有“两栋正在建设中的房屋,以备晚 Spring 入住日期”作为关键,这对我来说没有意义,我想知道是否我的代码中保存了一些本不应该保存的内容,或者由于它不在数据库中,所以一开始是如何保存的。
如果 $title
变量包含 notVariable
,则 $posts->$title
等同于 $posts->notVariable
。所以你只需要在 $posts
key/attribute.
$
改变
$posts->$title = $title;
Log::info($posts);
$posts->$slug = $request->input('pxp-submit-comm-slug');
$posts->$content = $overview;
$posts->$featured_url = $photo;
$posts->$post_status = $post_status;
到
$posts->title = $title;
Log::info($posts);
$posts->slug = $request->input('pxp-submit-comm-slug');
$posts->content = $overview;
$posts->featured_url = $photo;
$posts->post_status = $post_status;
PHP 允许使用 variable variables as well as using variables as object keys (see this post 以获得更多解释)。您似乎无意中采用了这一原则。
在您设置的 Log::info()
语句之前
$posts->$title = $title
在 PHP 中,这实际上是将键设置为 $title
s 值,这解释了对象中的“现在正在建设的两栋房屋,晚些 Spring 入住日期”键.您没有收到 Undefined variable 错误 $title
因为该变量存在。您收到 Undefined variable: slug 错误,因为您试图使用不存在的变量 $slug
、$content
等来设置其他键.您需要更新代码以删除箭头后的 $
。
$posts->title = $title;
$posts->slug = $request->input('pxp-submit-comm-slug');
$posts->content = $overview;
$posts->featured_url = $photo;
$posts->post_status = $post_status;