【发布时间】:2017-12-06 23:47:26
【问题描述】:
我有错误 NotFoundHttpException No query results for model [App\ThreadForum]
我的web.php:
Route::get('/threads','ThreadForumController@index');
Route::post('/threads','ThreadForumController@store');
Route::get('/threads/create','ThreadForumController@create');
Route::get('/threads/{thread}','ThreadForumController@show')->name('threads.show');
我的模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class ThreadForum extends Model
{
protected $fillable = [
'user_id','title','body'
];
public function path(){
return route('threads.show',$this->id);
}
public function replies(){
return $this->hasMany('App\Reply');
}
public function creator(){
return $this->belongsTo('App\User', 'user_id');
}
public function addReply($reply){
$this->replies()->create($reply);
}
}
我的控制者:
namespace App\Http\Controllers;
use App\ThreadForum;
use Illuminate\Http\Request;
class ThreadForumController extends Controller
{
public function __construct()
{
$this->middleware('auth')->only('store');
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$threads = ThreadForum::all();
return view('threads.index', compact('threads'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('threads.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dd('store');
/*$thread = ThreadForum::create([
'user_id'=>auth()->id(),
'title'=>$request['title'],
'body'=>$request['body']
]);
redirect($thread->path());*/
}
/**
* Display the specified resource.
*
* @param \App\ThreadForum $threadForum
* @return \Illuminate\Http\Response
*/
public function show(ThreadForum $thread)
{
return view('threads.show',compact('thread'));
}
/**
* Show the form for editing the specified resource.
*
* @param \App\ThreadForum $threadForum
* @return \Illuminate\Http\Response
*/
public function edit(ThreadForum $threadForum)
{
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\ThreadForum $threadForum
* @return \Illuminate\Http\Response
*/
public function update(Request $request, ThreadForum $threadForum)
{
}
/**
* Remove the specified resource from storage.
*
* @param \App\ThreadForum $threadForum
* @return \Illuminate\Http\Response
*/
public function destroy(ThreadForum $threadForum)
{
//
}
}
所有功能都运行良好,但是当我运行 post('/threads') 存储新记录时出现错误。 我尝试使用 dd('store') 进行调试,但我没有看到这个文本,只有错误。 我该如何解决? 谢谢。
【问题讨论】:
-
你是不是打开了路由缓存,然后写了路由?如果是这种情况,您可能需要运行
php artisan route:clear来破坏缓存。 -
显示你的视图,你有帖子表单的地方
-
收到此错误的请求的 URL 是什么?那些都是带有那个前缀的路由吗?
-
请发布堆栈跟踪