假设你有一个NewsController获取所有的新闻像
class NewsController extends Controller
{
public function index()
{
$news = News::all(); //you have to create News model
return view('news.index', compact('news')); //use to pass data in view
}
public function show($id)
{
$news_detail=News::find($id); //to fetch detail of news from database
return view('news.show', compact('news_detail'));
}
}
创建视图的index.php和show.php /新闻文件夹。在使用的index.php
@foreach($news as $news_item)
<div>
<a href="/news/{{$news_item->id}}">{{ $news_item->title }}</a>
</div>
@endforeach
这里 “/新闻/ {{$ news_item-> ID}}”,你可以通过特定的消息ID为路由文件。 在show.php
<h1>news</h1>
<h1>
{{ $news_detail->title }}
</h1>
<ul class="list-group">
@foreach($news_detail->detail as $details)
<li class="list-group-item">{{$details}}</li>
@endforeach
</ul>
在路由文件
Route::get('/news/{news}', '[email protected]');
现在你必须创建秀($ ID)在NewsController.php函数的参数是ID。
但是是可以告诉routes.php文件,在网址/足球新闻情况下,/它应该使用NewsController @指数(1),以防/ tennis-新闻/它应该使用索引(2)等? –
我已经添加了关于您的评论的一些编辑。 –