Dyanmic URL в Laravel

Я новичок в Laravel, поэтому извините, если это простой вопрос

Я пытаюсь передать переменную БД через мой URL, например, блог? = Категория
Он отображает все результаты, но когда я изменяю URL на blog / category1, он ничего не отображает.

Поля БД: идентификатор, категория, имя
1 категория 1 тест

Это в моих маршрутах:

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);
});

BlogController

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

Спасибо

0

Решение

Вам нужно добавить ->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);
});
2

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector