Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Object Laravel 5:路由绑定返回空对象_Object_Laravel_Binding_Model_Routes - Fatal编程技术网

Object Laravel 5:路由绑定返回空对象

Object Laravel 5:路由绑定返回空对象,object,laravel,binding,model,routes,Object,Laravel,Binding,Model,Routes,我正在尝试让绑定正常工作(在youtube上发布了一条消息)。在这一点上,我基本上是复制粘贴代码;不过,这似乎并不奏效 下面是routes.php: Route::model('song', 'App\Song'); Route::get('songs', 'SongsController@index'); Route::get('songs/{slug}', 'SongsController@show'); Route::get('songs/{slug}/edit', 'SongsCont

我正在尝试让绑定正常工作(在youtube上发布了一条消息)。在这一点上,我基本上是复制粘贴代码;不过,这似乎并不奏效

下面是routes.php:

Route::model('song', 'App\Song');

Route::get('songs', 'SongsController@index');
Route::get('songs/{slug}', 'SongsController@show');
Route::get('songs/{slug}/edit', 'SongsController@edit');
Route::patch('songs/{slug}', 'SongsController@update');
Song.php:

<?php
namespace App;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Song extends Eloquent {

    protected $fillable = [
        'title', 'lyrics'
    ];
}
数据库中的条目存在;修补匠:

>>> App\Song::whereSlug('boyfriend')->first();
=> <App\Song #000000005ba18d590000000003230878> {
       id: 1,
       title: "Boyfriend",
(...)
routes.php
文件中(使用
$router->bind
语法也是一样)。我在PHP5.6.8中使用XAMPP,项目是通过
PHP artisan serve
启动的,
PHP artisan--version
Laravel Framework version 5.1.2(LTS)

我在Route::bind中尝试了dd($slug)——它什么也不做(好像它不在那里),与
findOrFail
firstOrFail
一样;然而,把
放进废话返回之前的code>会导致
FatalErrorException
,因此函数似乎确实被调用。我错过了什么


这是虫子吗?这是否正常工作?

看起来您正在绑定到
歌曲
,但在
routes.php
中,占位符为
slug
。您需要将其更改为
song

Route::get('songs', 'SongsController@index');
Route::get('songs/{song}', 'SongsController@show');
Route::get('songs/{song}/edit', 'SongsController@edit');
Route::patch('songs/{song}', 'SongsController@update');

这很有效,谢谢你——我正要朝自己脸上开枪。再次感谢!如此简单,却又如此容易被忽视。我被发现在这个方法中使用了{itemId}和$item。谢谢你。
>>> App\Song::whereSlug('boyfriend')->first();
=> <App\Song #000000005ba18d590000000003230878> {
       id: 1,
       title: "Boyfriend",
(...)
Route::bind('song', function($slug) {

    return App\Song::whereSlug($slug)->first();
});
Route::get('songs', 'SongsController@index');
Route::get('songs/{song}', 'SongsController@show');
Route::get('songs/{song}/edit', 'SongsController@edit');
Route::patch('songs/{song}', 'SongsController@update');