0
我想弄清楚如何將不同的CSS樣式應用於我的index.blade.php頁面,具體取決於$ category是否處於活動狀態。基於if語句的laravel css樣式
I.e如果沒有在url中指定的類別,那麼圖像將會很小,如果url中有一個類別,圖像將會很大。
Route.php
Route::get('blog/{category}', ['as' => 'blog.category', 'uses' => '[email protected]']);
blogcontroller.php
public function getCategory($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(function($query) use($category){
$query->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);
}
}
index.blade.php
@foreach ($posts as $post)
<h2>{{ $post->id }}</h2>
<h2>{{ $post->img }}</h2>
<p>{{ $post->name }}</p>
<p>{{ $post->category }}</p>
@endforeach
謝謝,我已經試過,但有語法錯誤,意想不到的 ':',期待 '('。 (is_null($ category) – 2014-10-17 17:40:12
@tomharrison缺少一個'')。我糾正了它 – 2014-10-17 18:17:33