I'm trying to figure out how I can apply different css styling to my index.blade.php page depending on whether the $category is active.
I.e If there is no category specified in the url then the image would be small and if there was a category in the url the image would be large.
Route.php
Route::get('blog/{category}', ['as' => 'blog.category', 'uses' => 'BlogController#getCategory']);
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
You can change:
return View::make('blog.index')
->with('posts', $posts);
into:
return View::make('blog.index')
->with('posts', $posts)
->with('category', $category);
And now in Blade:
#foreach ($posts as $post)
<h2>{{ $post->id }}</h2>
<h2>
#if (is_null($category))
{{ $post->img }} - here somehow you need to display small image using image width/height or thumbnail
#else
{{ $post->img }}
#endif
</h2>
<p>{{ $post->name }}</p>
<p>{{ $post->category }}</p>
#endforeach
If your css is internal css so you can use the below method:
<style>
#if(your condition)
.container-wizard {
width:100%;
position: relative;
margin: 0 auto;
}
#else
.container-wizard {
width:83%;
position: relative;
margin: 20 auto;
}
#endif
</style>
If your css is external you can use the same method by reference links:
#if(your condition)
<link href="/assets/aos.css" rel="stylesheet">
#else
<link href="/assets/vendor/style.css" rel="stylesheet">
#endif
Moreover for your category you can add flag.
Related
I have a page of a separate article to which we go by slug
php.blade
#extends('layouts.app')
#section('content')
<div class="article-wrapper">
<div class="container">
<h1>{{ $article->title }}</h1>
<h3>{{ $article->subtitle }}</h3>
<img src="{{ $article->main_image }}" alt="">
<h3>{{ date('d F Y', strtotime($article->published_at)) }}</h3>
<h3>{{ $article->views }} Views</h3>
#foreach($article_blocks as $article_block)
<div class="text-image">
<h2>{{ $article_block->title_1 }}</h2>
<h3>{{ $article_block->text_1 }}</h3>
<img src="{{ $article_block->main_image }}" alt="">
</div>
#endforeach
#endsection
controller
public function index(Request $request, $slug)
{
$article = Article::where('slug', $slug)->first();
$article_blocks = ArticleBlock::where('article_id', 1)->get();
return view('article', compact('article', 'article_blocks'));
}
Next, I have another model for blocks, each article has its own blocks, as you can see in my controller, I manually write the article ID to get the blocks specific for it.
How to make it itself determine which article we are on, and display blocks only for it?
Try :
public function index(Request $request, $slug)
{
$article = Article::firstWhere('slug', $slug);
$article_blocks = ArticleBlock::where('article_id', $article->id)->get();
return view('article', compact('article', 'article_blocks'));
}
If you have created relationships between your models, you can use it as follows:
public function index(Request $request, $slug)
{
$article = Article::with('articleBlock')->where('slug', $slug)->get();
return view('article', compact('article'));
}
I have a laravel-application where I have a Blog section. The posts are split up into categories so that the user can switch between each categories. So far so good but since I added the categorization and I click the "All"-tab, the orderBy()-method does not work anymore. Instead, the posts appear under their respective category, ignoring the orderBy-method.
Here is my controller:
$blogs = QueryBuilder::for(Blog::class)
->allowedFilters([
AllowedFilter::exact('category', 'blog_category_id'),
])
->orderBy('publishes_on', 'desc')
->where("publishes_on", "<=", Carbon::now())
->where('blog_status_id', '=', '2')
->with('blog_category', 'employee')
->get();
$blog_categories = BlogCategory::get();
return view('blog.index', compact('blogs', 'blog_categories'));
then in my view:
// the tab menu - click events handled with Javascript
<ul>
<li data-tab-id="all" class="tab all active">All</li>
#foreach ($blog_categories as $category)
<li data-tab-id="{{ strtolower($category->name) }}" class="tab {{ strtolower($category->name) }}">{{ $category->name }}</li>
#endforeach
</ul>
// the posts loop
#foreach($blog_categories as $category)
#foreach($category->blog_post as $blog)
<div class="col-md-4 post {{ strtolower($category->name) }}">
<p>{!! $blog->text !!}</p>
</div>
#endforeach
#endforeach
so, how can I achieve that the orderBy works again as intented?
thanks in advance!
Template:
<div class="item-wrapper" style="text-align:justify; top: 100px;">
<legend>Reviews</legend>
#if (count($reviews)>0)
#forelse($reviews as $review)
#if( $review->counselor_id == $cprofile->id)
{{$review->review}}
{{$review->rating}}
<star-rating :rating="{{$review->rating}}">
</star-rating>
#endif #empty
#endforelse
#endif
</div>
Controller:
$relations = [
'viewCprofile' => counselor::all(),
'reviews' => DB::table('counselor_reviews')
->select('*')
->get()
];
return view('viewCprofile', $relations);
Route:
Route::get('/viewCprofile', 'ProfilesController#getCprofiles');
Should I pass id to controller to get data?
you can use the following and complete your code
#if (count($reviews)>0)
#php $total_ratings = 0 #endphp
#forelse($reviews as $review)
#if( $review->counselor_id == $cprofile->id)
#php $total_ratings += $review->rating #endphp
{{$review->review}} {{$review->rating}}
<star-rating :rating="{{$review->rating}}"> </star-rating>
{{$total_ratings}}
#endif
I'm trying to list articles from database then show details of the article when the user clicks on it.
How can I pass the article id or name to the route/controller to display the details? I tried butting the id in the href but I don't know how to handle it from routes!
this is the articles list:
#foreach ($articles as $article)
<div class="panel-body">
{{$article->name}}
</div>
#endforeach
where do I go from here?
Your View file for listing all the articles:
#foreach ($articles as $article)
<div class="panel-body">
{{$article->name}}
</div>
#endforeach
routes.php
Route::get('/article/{id}', 'ArticlesController#show');
ArticlesController.php
/**
* Show the article of the given id.
*
* #param int $id The id of the article
* #return \Illuminate\View\View
*/
public function show($id)
{
$article = Article::findOrFail($id);
return view('articles.show', compact('article'));
}
show.blade.php
<h3>{{ $article->name }}</h3>
<p>{{ $article->body }}</p>
If you want to use route() you can pass parameters in second argument
#foreach ($articles as $article)
<div class="panel-body">
{{ $article->name }}
^^^ or whatever is the name of your route
</div>
#endforeach
Naturally this assumes you have your route defined in app\Http\routes.php. For example
Route::get('/articles/{id}', ['as' => 'article.show', 'uses' => 'ArticleController#show']);
Alternatively you can use url()
#foreach ($articles as $article)
<div class="panel-body">
{{ $article->name }}
</div>
#endforeach
I have problem with displayin one picture in my home page.
Currently, I loaded some photos. I created a column in the database 'thumbnail'. Wants to do so, to load the photo - thumbnail where the column 'thumbnail
have record = 1.
My Homecontroller:
public function getHome(){
return View::make('pages.home')
->with('offers', Offer::take(8)->orderBy('created_at', 'DESC')->get());
}
my view:
#foreach($offers as $offer)
<div class="col-sm-3" style="border: 1px solid #AADFFF; height:350px; ">
#foreach($offer->photosofoffers as $photosofoffer)
<a href="{{ route('pages.view', $offer->id) }}">
<!--<a href="/pages/view/{{ $offer->id }}">-->
{{ HTML::image($photosofoffer->file, $photosofoffer->title, array( 'class'=>'feature', 'width'=>'240', 'height'=>'127')) }}
</a>
#endforeach
<h3><a href="{{ route('pages.view', $offer->id) }}">{{ $offer->title }}</h3>
<p> {{ $offer->description }} </p>
<h5>Availability: <span class="{{ Availability::displayClass($offer->availability) }}">
{{ Availability::display($offer->availability) }}
</span>
</h5>
<p>Cena <span>{{ $offer->price }}</span>
</p>
#endforeach
Where should I add something?
At the moment loads all the images of the announcement. He wants to load one image, where the 'thumbnail' = 1.
You can eager load the relations and apply a constraint. Something like:
Offer::with(array('photosofoffers' => function($q) {
$q->where('thumbnail', 1)->take(1);
}))->take(8)->orderBy('created_at', 'DESC')->get();
http://laravel.com/docs/5.0/eloquent
If you want to load only those offers that have a photo/thumbnail
Offer::with('photosoffers')->whereHas('photosofoffers', function($q) {
$q->where('thumbnail', 1)->take(1);
})->take(8)->orderBy('created_at', 'DESC')->get();