Fel*_*lan -1 binding routes model object laravel
我正在尝试使绑定工作(遵循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');
Run Code Online (Sandbox Code Playgroud)
Song.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model as Eloquent;
class Song extends Eloquent {
protected $fillable = [
'title', 'lyrics'
];
}
Run Code Online (Sandbox Code Playgroud)
和SongsController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Song;
class SongsController extends Controller
{
public function index(Song $song) {
$songs = $song->get();
return view('songs.index', compact('songs'));
}
public function show(Song $song) {
return view('songs.show', compact('song'));
}
public function edit($slug) {
$song = Song::whereSlug($slug)->first();
return view('songs.edit', compact('song'));
}
public function update($slug, Request $request) {
$song = Song::whereSlug($slug)->first();
$song->fill($request->input())->save();
return redirect('songs');
}
}
Run Code Online (Sandbox Code Playgroud)
我特意留下的$slug,而不是Song $song在edit和update,因为这是问题的所在.如果我理解正确,我应该能够在所有4种方法中访问$ song对象(假设第一个参数都Song $song在所有方法中); 但是,只有在index方法的情况下,我才能从数据库中获取任何内容.其他人只给我一个(某种)空对象.这是函数中的一个dd($song)调用show(URI是http://localhost:8000/songs/boyfriend或者http://localhost:8000/songs/1- 无论是否使用Route::bind而导致相同的输出Route::model,如下所述):
Song {#144 ?
#fillable: array:2 [?]
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: []
#original: []
#relations: []
#hidden: []
#visible: []
#appends: []
#guarded: array:1 [?]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: false
}
Run Code Online (Sandbox Code Playgroud)
数据库中的条目存在; 修补匠:
>>> App\Song::whereSlug('boyfriend')->first();
=> <App\Song #000000005ba18d590000000003230878> {
id: 1,
title: "Boyfriend",
(...)
Run Code Online (Sandbox Code Playgroud)
方法edit和update工作完美无缺(除非我尝试删除$slug并放入Song $song第一个参数).
我曾尝试更新$ compiledPath,clear-compiled,cache:clear,重新开始一切十亿倍; 我也试过更换Route::model线
Route::bind('song', function($slug) {
return App\Song::whereSlug($slug)->first();
});
Run Code Online (Sandbox Code Playgroud)
在routes.php文件中(和使用$router->bind语法相同的东西).我正在使用XAMPP和PHP 5.6.8一起开始项目php artisan serve,而且php artisan --version是 Laravel Framework version 5.1.2 (LTS).
我试图在Route ::绑定DD($塞) -什么也不做(因为如果它不在那里),同样既findOrFail和firstOrFail; 然而,blah!之前放置return导致a FatalErrorException,所以函数似乎确实被调用.我在这里错过了什么?
这是一个错误吗?这应该正常吗?
小智 6
它看起来像你有约束力,song但在你的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');
Run Code Online (Sandbox Code Playgroud)