我在Laravel的某些路线有问题。这是我在web.php文件中的代码:
Route::group(['namespace' => 'Admin', 'middleware' => ['auth:web']], function () {
Route::get('/admin/audio/create/{audio?}', 'AdminAudioController@create')->name('admin.audioCreate');
Route::get('/admin/article/create/{article?}', 'AdminArticleController@create')->name('admin.articleCreate');
}
这是我刀片中的链接
<a href="{{ route('admin.audioCreate' , ['audio' => $audio->audioId]) }}"><i class="fa fa-edit"></i></a>
<a href="{{ route('admin.articleCreate' , ['article' => $article->articleId]) }}"><i class="fa fa-edit"></i></a>
,这是我的控制器: AdminAudioController
<?php
namespace App\Http\Controllers\Admin;
use App\Article;
use App\Http\Requests\ArticleRequest;
class AdminArticleController extends AdminController
{
public function index()
{
$articleList = Article::where('removed', false)->latest()->paginate(10);
return view('admin.article.archive', compact('articleList'));
}
public function create(Article $article = null)
{
return view('admin.article.create', compact('article'));
}
}
AdminArticleController
<?php
namespace App\Http\Controllers\Admin;
use App\Article;
use App\Http\Requests\ArticleRequest;
class AdminArticleController extends AdminController
{
public function index()
{
$articleList = Article::where('removed', false)->latest()->paginate(10);
return view('admin.article.archive', compact('articleList'));
}
public function create(Article $article = null)
{
return view('admin.article.create', compact('article'));
}
}
但是我的第二个名为“ admin.articleCreate”的链接不起作用,并显示“ 404 not found”,我该怎么办?
这是我的文章模型
class Article extends Model
{
protected $primaryKey = 'articleId';
use Sluggable;
protected $fillable = [
'title',
'subTitle1', 'subTitle2',
'image',
'description',
'body',
'enable',
];
protected $casts = [
'image' => 'array'
];
/**
* Return the sluggable configuration array for this model.
*
* @return array
*/
public function sluggable(): array
{
return [
'slug' => [
'source' => 'title'
]
];
}
public function getRouteKeyName()
{
return 'slug';
}
}
答案 0 :(得分:1)
当您在控制器上调用方法create(Article $article = null)
时,Laravel使用Model Binding来解析模型,模型绑定使用您已添加到模型中的方法
public function getRouteKeyName()
{
return 'slug'; // by default it will be $primaryKey which is 'id'
}
简而言之,Laravel在给他slug
articleId
来查找您的模型
要解决此问题,您几乎没有选择
// blade.php
<a href="{{ route('admin.articleCreate' , ['article' => $article->slug]) }}"><i class="fa fa-edit"></i></a>
// blade.php
<a href="{{ route('admin.articleCreate' , ['article' => $article->articleId]) }}"><i class="fa fa-edit"></i></a>
// Article.php.php
public function getRouteKeyName()
{
return 'articleId';
}
// blade.php
<a href="{{ route('admin.articleCreate' , ['article' => $article->YOUR_FIELD]) }}"><i class="fa fa-edit"></i></a>
//Controller.php
public function create($article = null)
{
$article = Article::where('YOUR_FIELD', $article)->firstOrFail();
return view('admin.article.create', compact('article'));
}
答案 1 :(得分:0)
您有代码
return view('admin.article.create', compact('$article'));
但需要
return view('admin.article.create', compact('article'));
答案 2 :(得分:0)
我可以看到您在侧面契约中提到了$ article。
能否请您检查一次,我认为创建方法应如下所示:
public function create(Article $article = null)
{
return view('admin.article.create', compact('article'));
}