【问题标题】:Laravel NotFoundHttpException No query results for modelLaravel NotFoundHttpException 模型没有查询结果
【发布时间】: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 是什么?那些都是带有那个前缀的路由吗?
  • 请发布堆栈跟踪

标签: php laravel laravel-5


【解决方案1】:

我无法评论,但要转储你必须使用dd($request) 而不是dd($store)

编辑:我认为问题出在 show 方法中。您使用紧凑型,但它上面没有任何变量。所以你不能传递任何东西来查看。

【讨论】:

  • 这是一个字符串,所以没问题
  • 我用这个作show方法:Route::get('/threads/{thread}','ThreadForumController@show')->name('threads.show');,效果很好
  • 所有方法都有效,但我无法运行Route::post('/threads','ThreadForumController@store');
  • $thread 在方法签名中,它是参数...是一个变量
  • 清除您的路线并重试。目前找不到带有 post 的路线,这就是为什么它会使用 get 去路线
猜你喜欢
  • 2018-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
  • 1970-01-01
  • 2019-12-04
  • 1970-01-01
相关资源
最近更新 更多