Php laravel中的动态url
我是一个初学者,所以请原谅我,如果这是一个简单的问题 我试图通过我的url传递一个db变量,例如blog?=category 它显示所有结果,但当我将url更改为blog/category1时,它不会显示任何内容 数据库字段:id、类别、名称 1类别1试验 这是我的路线:Php laravel中的动态url,php,url,dynamic,laravel,url-routing,Php,Url,Dynamic,Laravel,Url Routing,我是一个初学者,所以请原谅我,如果这是一个简单的问题 我试图通过我的url传递一个db变量,例如blog?=category 它显示所有结果,但当我将url更改为blog/category1时,它不会显示任何内容 数据库字段:id、类别、名称 1类别1试验 这是我的路线: Route::get( '/blog', 'BlogController@index' ); Route::get('/', 'HomeController@index'); Route::get('blog/{catego
Route::get( '/blog', 'BlogController@index' );
Route::get('/', 'HomeController@index');
Route::get('blog/{category}', function($category = null)
{
// get all the blog stuff from database
// if a category was passed, use that
// if no category, get all posts
if ($category)
$posts = Post::where('category', '=', $category);
else
$posts = Post::all();
// show the view with blog posts (app/views/blog.blade.php)
return View::make('blog.index')
->with('posts', $posts);
});
博客控制器
class BlogController extends BaseController {
public function index()
{
// get the posts from the database by asking the Active Record for "all"
$posts = Post::all();
// and create a view which we return - note dot syntax to go into folder
return View::make('blog.index', array('posts' => $posts));
}
}
这是我的视图布局:
@extends('base')
@section('content')
@foreach ($posts as $post)
<h2>{{ $post->id }}</h2>
<p>{{ $post->name }}</p>
@endforeach
@stop
@extends('base'))
@节(“内容”)
@foreach($posts作为$post)
{{$post->id}
{{$post->name}
@endforeach
@停止
谢谢您需要在雄辩的查询之后添加
->get()
,以获取结果
Route::get('blog/{category}', function($category = null)
{
// get all the blog stuff from database
// if a category was passed, use that
// if no category, get all posts
if ($category)
$posts = Post::where('category', '=', $category)->get();
else
$posts = Post::all();
// show the view with blog posts (app/views/blog.blade.php)
return View::make('blog.index')->with('posts', $posts);
});
很高兴我能帮忙。把这个答案标记为解决方案。