Laravel新闻编辑/删除页面空白问题排查:路由还是控制器?
Hey there, let's break down why your edit/delete pages are showing blank while create and index functions work fine. Since you mentioned you can fetch the ID from the database, let's start with the most likely culprits step by step.
1. 先确认路由配置是否完整
First, make sure your resource route for news is fully written (your shared code was cut off). It should look like this:
Route::get('/', function () { return view('welcome'); }); Route::resource('books','BookController'); Route::resource('news','NewsController');
Run php artisan route:list in your terminal to verify the auto-generated routes. You should see entries like:
GET|HEAD news/{news}/edit(maps toNewsController@edit)DELETE news/{news}(maps toNewsController@destroy)
If these routes are missing, double-check for typos in the controller name (it should beNewsControllerwith proper camel casing) or if you accidentally commented out the resource route.
2. 排查控制器逻辑问题
Since you can get the ID, let's focus on how you handle data in the controller:
Edit Method Common Mistakes
Your edit method needs to fetch the news record and pass it to the view. Here are easy-to-miss issues:
- Forgetting to return the view (yes, it happens!)
- Using the wrong variable name when passing data to the view
- Not properly fetching the record (if not using route model binding)
Example of a correct edit method:
public function edit($id) { // Manually fetch the record if not using route model binding $news = \App\Models\News::findOrFail($id); // Add this line to confirm you're getting valid data // dd($news); return view('news.edit', compact('news')); }
If you use route model binding, make sure your method accepts the correct model parameter (resource routes use {news} by default):
public function edit(\App\Models\News $news) { return view('news.edit', compact('news')); }
If dd($news) shows valid data but the view is still blank, the problem is likely in your view file.
Delete Method Common Mistake
The biggest cause of a blank delete page is not returning a redirect after deletion. Your destroy method should redirect back to the news list, like this:
public function destroy($id) { $news = \App\Models\News::findOrFail($id); $news->delete(); // Redirect with a success message to avoid blank page return redirect()->route('news.index')->with('success', 'News deleted successfully'); }
If you only run $news->delete() without returning a redirect, the browser gets an empty response, resulting in a blank page.
3. 检查视图文件细节
- Confirm your edit view exists at
resources/views/news/edit.blade.php(double-check folder and filename spelling) - In the view, make sure you're using the correct variable name (e.g.,
{{ $news->title }}not{{ $new->title }}) - If
APP_DEBUG=falsein your.envfile, Laravel won't show errors like missing views. Try enabling debug mode temporarily to see if it reveals an error message.
4. 查看Laravel日志
If all else fails, check your log file at storage/logs/laravel.log. It will almost certainly have error messages explaining why the page is blank (e.g., undefined variable, missing view, database query issue).
内容的提问来源于stack exchange,提问作者user9461267

