繁体   English   中英

laravel 出错:尝试获取非对象的属性“id”

[英]Error with laravel: Trying to get property 'id' of non-object

我对 Laravel 还很陌生,但我正在尝试为我的课程制作一个小的 API。 我有这条路线Route::get('films/search', 'App\Http\Controllers\FilmController@search'); 它将我的自定义 function 搜索称为请求参数。 这里是:

    public function search(Request $request)
    {
        $keyword = $request->input('keyword');
        $rating = $request->input('rating');
        $minLength = $request->input('minLength');
        $maxLength = $request->input('maxLength');

        if ($this->isParamExisting($keyword)) {
            return $this->getFromKeyword($keyword);
        } elseif ($this->isParamExisting($rating)) {
            return $this->getRatings($rating);
        } elseif ($this->isParamExisting($minLength)) {
            return $this->getMinLength($minLength);
        } elseif ($this->isParamExisting($maxLength)) {
            return $this->getMaxLength($maxLength);
        } else {
            return $this->getAllFilms();
        }
    }

起初它正在工作,但后来我在我的模型中添加了其他外键的连接,现在它让我Trying to get property 'id' of non-object并说它来自我的资源,它也被我的索引方法使用作品。 我看不出有什么理由让它不起作用。

它可能来自我的路线,因为当我放置@index而不是我的自定义方法时,它仍然给我错误消息。 而且我知道我的索引有效,因为我在另一条路线上使用它并且没有问题。

解决方案:

问题来自我的其他路线之一。 我添加了一个使用films/{id}的系统,系统认为搜索被用作{id} 我改变了我的路线Route::get('films/search/param', 'App\Http\Controllers\FilmController@search');

错误是因为您尝试访问数据库中不存在的 id。 你能告诉我你的model中的代码吗? 以及您在搜索方法中调用它的函数。

解决方案:

问题来自我的其他路线之一。 我添加了一个使用电影/{id} 的系统,系统认为搜索被用作 {id}。 我改变了我的路线 Route::get('films/search/param', 'App\Http\Controllers\FilmController@search');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM