2014-10-16 2 views
0

Я новичок в Laravel поэтому, пожалуйста, простите меня, если это простой вопросDyanmic URL в Laravel

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

DB поля: идентификатор категории, имя 1 category1 тест

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

Route::get('/blog', '[email protected]'); 
Route::get('/', '[email protected]'); 
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 

Спасибо

ответ

2

Чтобы получить результаты, вам необходимо добавить ->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); 
}); 
+0

Легенда! работает сейчас –

+0

Рад, что я мог помочь. Подумайте о том, чтобы обозначить этот ответ как решение. ;) – Jerodev

+0

+1 Я не заметил недостатка '-> get()' –