Counting All articles using withCount() - php

I am using withCount() to count the number of articles in a category, everything works well.
The question is how to count and display the number of articles for All categories?
Controller
$categories = BlogCategory::withCount('articles')->get();
blade.php
<div class="blog-filter">
<div class="blog-filter__item active" data-filter="all">All</div>
#foreach($categories as $category)
<div class="blog-filter__item" data-filter=".category_{{$category->id}}" value="{{ $category->title }} ({{ $category->articles_count }})">{{ $category->title }} ({{ $category->articles_count }})</div>
#endforeach
</div>
({{ $category->articles_count }}) responsible for counting articles

you can use count method:
$allArticlesCount=Article::query()->count();
and do not forget to send it to your view as well.
a better approach is to get the count directly form the result you already got from db like #Apuv Bhavsar in his comment:
$allArticlesCount = $categories->sum('articles_count');
unless you have some conditions to apply this approach reduce the trips count to DB.

Related

Laravel, how to show a related table's associated name in my home.blade.php?

I know the question might be unclear, but let me elaborate. I have a home page for my website (home.blade.php), where I list the items like this.
#foreach ($products as $product)
<div class="m-auto text-center">
<h2>
<a href="/products/{{ $product->id }}">
{{ $product->name }}
</a>
</h2>
<p>
Manufacturer: {{ $product->manufacturer }}
</p>
<p>
Price: {{ $product->price }}
</p>
<p>
In stock: {{ $product->stock }}
</p>
<a href="seller/{{ $product->sellerID }}">
Seller's site
</a>
<hr>
</div>
#endforeach
I want to show the name attribute of my seller table instead of the Seller's site. With the product table, it's in a many-many relationship. And I store a sellerID attribute in my product table as a foreign key, which references the seller's table id attribute. As for my HomeController.php, the function currently passing the values looks like this.
public function index()
{
$products = Product::all();
return view('home', [
'products' => $products
]);
}
I know that I shouldn't do a query in any view, but I can't figure out how I should do the query in the controller. Can someone help me with this?
Also, please ignore the bad-looking site, I'm currently trying to do the backend first, and the frontend comes later.
I'm not sure if I understood you entirely, because I got confused.
If you have many-to-many relationship, you can't have sellerID. Foreing id can be used in one-to-many or one-to-one relationships.
In order to have many-to-many relationship, you need to have a pivot table.
product_seller
id, product_id, seller_id
In conttoller
Product::with('sellers')->get();
In view, there is another problem. You will have many sellers, but you want to display only one, which one? I assume you either have a wrong relationship, or wrong view, or misexplained problem. But if I go based on many to many:
#foreach ($product->sellers as $seller)
Seller's site
#endforeach

Add a block field from a specific article

I have an article, each article has article_blocks, the blocks have a video_link field
And I need to display this field in the list of articles
This is how the article list output is roughly implemented
<div class="blog-list">
#foreach($articles as $article)
<div class="blog-article">
<div class="video-button video-modal-button-blog" data-video="https://www.youtube.com/embed/{{ $article_block->video_link }}">
<span>Watch video</span>
</div>
<h2 class="blog-article__title">{{ $article->title }}</h2>
<span>{{ date('d F Y', strtotime($article->published_at)) }}</span>
<span>{{ $article->getTotalViews() }} Views</span>
</div>
#endforeach
</div>
Each article has a button to open a video, but this video field itself is not in the article itself, but in the article blocks
I now take this field from the block and output it, it turns out like this
<?php
use App\Models\ArticleBlock;
$article_block = ArticleBlock::whereNotNull('video_link')->first();
?>
<div class="blog-list">
#foreach($articles as $article)
<div class="blog-article">
#if ($article_block->video_link !== 'null')
<div class="video-button video-modal-button-blog" data-video="https://www.youtube.com/embed/{{ $article_block->video_link }}">
<span>Watch video</span>
</div>
#endif
<h2 class="blog-article__title">{{ $article->title }}</h2>
<span>{{ date('d F Y', strtotime($article->published_at)) }}</span>
<span>{{ $article->getTotalViews() }} Views</span>
</div>
#endforeach
</div>
As a result, I get this field with video and it is displayed, but the same video is displayed for all articles in the list, and each article should have its own field and its own video. How can this be fixed?
I probably need to look for something like blocks by article id
$article_block = ArticleBlock::where('article_id', $article->id)->whereNotNull('video_link')->first();
But I can't get id
Cant write comment, under 25 rep :(
data-video="https://www.youtube.com/embed/{{ $article_block->video_link }}"
This looks wrong. You should eager load article blocks (eager loading is generally faster than lazy loading) for all articles with a with statement in your eloquent query see: https://laravel.com/docs/8.x/eloquent-relationships#eager-loading
Then if everything is correct you should be able to do just {{ $article->block->video_link }} or something similar.
Above is right if article has only one article block (one to one relationship).
But it seems that article has multiple blocks (one to many relationship) so you would need to iterate over each of the article block as well with another #foreach statement.
#foreach($articles as $article)
#foreach($article->article_blocks as $block)
<p>URL of video is: https://www.youtube.com/embed/{{ $block->video_link }}</p>
#endforeach
#endforeach
Now lets answer your question why video is same everywhere? Because you've coded it as so. You should NEVER write custom queries in your blade files.
Here i am referring to this part of your code:
<?php
use App\Models\ArticleBlock;
$article_block = ArticleBlock::whereNotNull('video_link')->first();
?>
Queries should be either in controllers/services or repositories.
$article_block = ArticleBlock::whereNotNull('video_link')->first();
will just give you first article block defined in ur database where video_link is not null. It won't guarantee that ArticleBlock belongs to given Article.
Lets hope that i put you on the right path ^.^

How can I access the data of a PHP Laravel Collection

402/5000
I have this collection of data, from a table called "Actividades", that has a many to many relationship with another table called "Estrategias".
My data collection has 3 Actividades that share a Estrategia.
So my problem is how I can show in the html the attribute "descripcion" of the strategy that is in the three Actividades.
the image shows the data collection
This is what I have in the controller
$actividades = Actividad::orderby('id', 'ASC')->with('estrategias')->where('evidencia_id', $evidenciaid)->get();
dd($actividades);
This is what I tried to do in the html
#foreach($actividades as $actividad)
<div>
{{$actividad->estrategias->descripcion}}}
</div>
#endforeach
estrategias is an array of instances of Estrategia
#foreach($actividades as $actividad)
<div>
#if(isset($actividad->estrategias[0]){{$actividad->estrategias[0]->descripcion}}}#endif
</div>
#endforeach
Looking at the picture you have posted, it looks like you need to loop over the estragias collection as well
#foreach($actividades as $actividad)
<div>
#foreach($actividad->estrategias as $estragias)
{{ estrategias->descripcion }}
#endforeach
</div>
#endforeach
This should fix it for you
If has one relationship
#foreach($actividades as $actividad)
<div>
{{$actividad->estrategias->descripcion }}
</div>
#endforeach
If has many relationship
#foreach($actividades as $actividad)
<div>
#foreach($actividad->estrategias as $estragias)
{{ estrategias->descripcion }}
#endforeach
</div>
#endforeach

How to display latest('updated_at') records in blade file of laravel

I am doing project in laravel. In my project there are two tables categories and subcategories. subcategories table has categoryid as foreign key.
subcategoryModel.php has following function
public function category(){
return $this->belongsTo('App\Category');
}
In my blade file I am displaying my subcategories using category object as follows,
blade.php
<div class="col-sm-6 col-align-left">
#foreach($category as $c)
<div class="form-group clearfix">
#if($c->subcategories->count() > 0)
<h4><b>Subcategories: </b></h4>
<div class="form-group clearfix">
#foreach($c->subcategories as $subcategory)
<h4>{{$subcategory->subcategoryname}}</h4>
#endforeach
</div>
#endif
<h4>Add Subcategories</h4>
</div>
#endforeach
</div>
This display correct data,but now I want to display data in the order they are updated/created. Is there any way to display data as per the requirement?
I want to something into the blade file itself. How to do this? Please give suggestions.
You shouldn't do something like that in a blade. Blade should be kept as dumb as possible. Main things should be done in the controller and then passed to the blade. you can do this in your controller:
$categories = Category::with(array('subcategories' => function($query) {
$query->orderBy('created_at', 'DESC');
}))->get();
now the subcategories in the category object will be ordered.
#foreach($c->subcategories()->orderBy('updated_at','asc')->get() as $subcategory)
<h4>{{$subcategory->subcategoryname}}</h4>
#endforeach
Edit: I do like the other answer however OP asked for within the blade file itself

Laravel: Need to solve foreach loop

What i'm trying to do is basically have the "latest" episodes show for a series that is has a status of "ongoing" below is the code i have so far.
The problem i a facing is that I can't seem to make the foreach loop for episodes work for the series. Wit hthe current code what it does is shows the same variables. Rather what i think is happening is that it loops the same query for each series so that the same variable pops up for each series.
Can anyone help me out here?
Also the way the episodes are linked is by using the title_id for the titles so in the table for episodes, they are liked by 'title_id', I wouldn't know what to do with that in this sequence though.
<?php $titles = DB::table('titles')->whereNotNull('poster')->where('status', '=', 'ongoing')->orderBy('updated_at', 'desc')->limit(12)->get(); ?>
#foreach ($titles as $title)
<?php $episodes = DB::table('episodes')->orderBy('created_at', 'desc')->limit(1)->get(); ?>
#foreach ($episodes as $episode)
<figure class="col-lg-2 col-md-3 col-sm-4 pretty-figure">
<div class="home-episode-number">
{{ $episode->episode_number }}
</div>
<div class="flip-containerw">
<div class="flipper">
<img src="{{ $episode->poster ? $episode->poster : '/assets/images/noimageepisode.png' }}" alt="" class="img-responsive">
</div>
</div>
<div class="home-anime-name">
{{ str_limit($title->title, 23, '...') }}
</div>
</figure>
#endforeach
#endforeach
You are not following some basic design patterns, like, for instance, the Model-View-Controller structure.
MVC
It's not good practice to have DB calls inside your view, wich you are doing. You should do it inside your model, or in a repository. And pass it trought the controller.
You would avoid a lot of headache if you start using eloquent properly.
Eloquent
Now, answering your question:
If you want to get the episode for the title in the loop, try using a where:
$episodes = DB::table('episodes')->where('title_id,'=',$title->id)->orderBy('created_at', 'desc')->limit(1)->get();
That query will retrieve just one episode (limit(1)).

Categories