Here's my category model:
public function products() {
return $this->hasMany('Product');
}
Then i am printing all products that each category has:
#foreach($category->products as $product)
{{ $product->title }}
#endforeach
However it prints every related product to this category. How can i use pagination in that case? I've tried to print pagination links at the bottom:
<div class="pager">
{{ $category->products()->paginate(12)->links() }}
</div>
It does print pagination links correctly, but when i change the page - content is not changing.
Before sending data to view first paginate the result like this.
$products = $category->products()->paginate(12);
now pass this value to the view and just iterate it.
#foreach($products as $product)
{{$product->title}}
#endforeach
To display links just call links method on the products in the view.
{{$products->links()}}
You want to call paginate your products first:
public function products() {
return $this->hasMany('Product');
}
public function recentProducts() {
return $this->products()->paginate(12);
}
Then in your view you loop through
#foreach($category->recentProducts() as $product)
{{ $product->title }}
#endforeach
Then your links
<div class="pager">
{{ $category->recentProducts()->links() }}
</div>
Pass paginated collection to the view:
// controller
$products = $category->products()->paginate(12);
// view
#foreach ($products as $product)
// do what you need
#endforeach
// links:
$products->links();
Related
hello I just moved from php native and want to try relationships in laravel, so I want to display a post and each comment and the name of user who comments so that when looped the post has a comment that has a relationship with it.
So this is my HomeController.php :
public function index() {
$posts = Post::all();
$comment = Comment::all();
return view('home', [
'posts' => $posts,
'comments' => $comment
]);
}
My Post.php :
public function comments() {
return $this->hasMany(Comment::class);
}
public function user() {
return $this->belongsTo(User::class);
}
My Comments.php :
public function post() {
return $this->belongsTo(Post::class);
}
public function user() {
return $this->belongsTo(User::class);
}
My User.php :
public function post() {
return $this->hasMany(Post::class);
}
My home.blade.php :
#foreach ($posts as $post)
<div class="box-post">
<p>{{ $post->user->name }}</p>
<p>{{ $post->post_content}}</p>
<button>Comment</button>
</div>
#foreach ($comments as $comment)
<div class="box-post">{{ $comment->comment_content }} </div>
#endforeach
#endforeach
the code above runs with the post displayed along with the author but all the comments also appear in all posts, I have searched for references and tried to change the comments value in Controller but the results are still the same so I made the comments appear in each post *$comment = Comment::all();.
what I want is to display posts and comments that relate to each post, like a twitter feature that can reply to people's tweets.
Thx you..
Your relations builded well, so just can simply use $post->comments can get all comments related on each post.
#foreach ($posts as $post)
<div class="box-post">
<p>{{ $post->user->name }}</p>
<p>{{ $post->post_content}}</p>
<button>Comment</button>
</div>
#foreach ($post->comments as $comment) // make changes here
<div class="box-post">{{ $comment->comment_content }} </div>
#endforeach
#endforeach
public function index() {
$posts = Post::with('comments')->get();
return view('home', [
'posts' => $posts,
]);
}
then in blade
#foreach ($posts as $post)
<div class="box-post">
<p>{{ $post->user->name }}</p>
<p>{{ $post->post_content}}</p>
<button>Comment</button>
</div>
#foreach ($post->comments as $comment)
<div class="box-post">{{ $comment->comment_content }} </div>
#endforeach
#endforeach
You have done fine job building models and their relationship.
I recommend you change how you call post query.
public function index(Request $request) {
// add eager loading comments for each post and user
$posts = Post::with('comments', 'user')->get();
// eager loading user is needed, cause in blade you call $post->user->name
// calling comments will no longer needed
// return only $posts
return view('home', compact('posts'));
}
And inside your blade, you can call comments from each post, like this.
#foreach ($posts as $post)
<div class="box-post">
<p>{{ $post->user->name }}</p>
<p>{{ $post->post_content}}</p>
<button>Comment</button>
</div>
#foreach ($post->comments as $comment)
<div class="box-post">{{ $comment->comment_content }} </div>
#endforeach
#endforeach
That's it and if you use barryvdh/laravel-debugbar , you will see there are only 3 queries run, each one from table posts, comments and users.
And for comments query and users query, not all rows will be fetched,
only related to fetched posts from the 1st query.
Have fun using Laravel Framework!
Your PHP native knowledge/expertise will be very useful and speed up your learning/mastering process.
how can I use pagination, where my eloquent query looks:
PageController - method show:
$page = Page::where('slug', $slug)->with(['subpages'=>function($q) {
$q->where('visible', 1)->orderBy('order', 'asc')->paginate(9);
}])->first();
I would like to paginate subpages on page's View blade (Page - one to many - Subpage relationship)
Here is part of my show.blade.php view:
{{ $page->subpages->links() }}
Error:
Method Illuminate\Database\Eloquent\Collection::links does not exist.
EDIT:
FULL FUNCTION SHOW:
public function show(PageRepository $pageRepo, $slug){
$page = Page::where('slug', $slug)->with(['subpages'=>function($q) {
$q->where('visible', 1)->orderBy('order', 'asc');
}])->first();
$subpages = $page->subpages()->paginate(2);
$sidebar = Navbar::where('type','sidebar')->orderBy('order')->with(['pages'])->get();
return view('pages.page.show', [
"page" => $page,
"subpages" => $subpages,
"sidebar" => $sidebar,
]);
}
VIEW:
<div class="row display-flex">
#foreach($page->subpages as $subpage)
<div class="col-sm-4">
<div class="card mb20">
<div class="card-block">
<h4 class="card-title font400">{{ $subpage->title }}</h4>
</div>
</div>
</div>
#endforeach
</div>
{{ $subpages->links() }}
If your goal is to paginate the relation from what I see, but you're querying the parent model. That's what's wrong. You have to completely change your logic since the subquery doesn't map the relation inside the Laravel Model. It's just used as a condition to retrieve that model.
You query should look like this:
$page = Page::where('slug', $slug)->with(['subpages'=>function($q) {
$q->where('visible', 1)->orderBy('order', 'asc') // ->paginate(9); This is not necessary
}])->first();
Now that you have the parent model, you can execute a new query for the relation, and there you have to call the paginate method:
I don't think that would be a good practice to do something like this in your blade view:
{{ $page->subpages()->paginate(9)->links() }}
But it's better to implement a new variable:
// Previous code that retrieves the page
$subpages = $page->subpages()->paginate(9);
return response()->view('your.blade.view', compact('page', 'subpages');
now you can use the subpages variable, that contains the paginated results, in your view
I want to display all user's profile into views and they posts. It's pretty simple:
#foreach($profiles as $profile)
{{ $profile->name }}
#foreach($profile->posts as $post)
{{$post->title}}
#endforeach
#endforeach
But I want to display only the latests posts (->orderBy('created_at', 'desc')->limit(4) ) and only accepted posts (->where('accept', 1)). How can I do that?
You already have what you need. You just need to put it all together.
#foreach($profile->posts()->where('accept', 1)->orderBy('created_at', 'desc')->limit(4)->get() as $post)
I would consider creating a query scope on your profile model. That way you can do something like $profile->latestPosts.
Or you can use the relationship to return exactly what you need.
public function latestPosts() {
return $this->posts()->where('accept', 1)->orderBy('created_at', 'desc')->limit(4)->get();
}
Then you could use it as:
#foreach($profile->latestPosts() as $post)
{{$post->title}}
#endforeach
A better solution would be to lazy load the posts you need when loading the profiles in the controller.
$profile = Profile::with(['posts' => function ($post) {
$post->where('accept', 1)->orderBy('created_at', 'desc')->limit(4);
}])->limit(10)->get();
then in the blade you can do the same as before
#foreach($profiles as $profile)
{{ $profile->name }}
#foreach($profile->posts as $post)
{{$post->title}}
#endforeach
#endforeach
if you want to use a scope for latestAccepted, you can on the Post model
class Post extends Model
{
public function scopeLatestAccepted($query)
{
return $query->where('accept', 1)->orderBy('created_at', 'desc')->limit(4)
}
}
Then lazy load it
$profile = Profile::with(['posts' => function ($post) {
$post->latestAccepted();
}])->limit(10)->get();
what you have to do is pretty simple.
create table users
create table post
create a relationships between the 2 tables
write you code in controller to join the table and query all the users post based on his/her username or password
see sample screenshot for table relationships
//Controller
Public function ViewPost(){
$data['data']=DB::table('useratable')
->where('useratable.username','=', $uname)
->where('useratable.password','<=',$pwd)
->leftjoin('posttable', 'useratable.idusertable', '=', 'posttable.userid')
->orderby('posttable.idposttable','desc')
->get();
}
//Route
Route::get('/view-post','YourController#ViewPost')
//view
#foreach($data as $r)
{{ $r->fullname}} <br>
{{ $r->email}} <br>
{{ $r->topic }} <br>
{{ $r->content }} <br>
{{ $r->datetime}} <br>
...
#endforeach
You need some codes like it on controller:
$profiles = Profile::with(['posts' => function ($query) {
$query->where('accept', 1)
->orderBy('created_at', 'desc');
}])->get();
And add this one to blade file:
#foreach($profiles as $profile)
{{ $profile->name }}
#foreach($profile->posts as $post)
{{$post->title}}
#if ($loop->iteration == 4)
#break
#endif
#endforeach
#endforeach
I have a code, where I am getting all the categories and its subcategories from the table, but i want to get a single category and it's subcategories from the categories table display in the laravel blade navigation file.
For example, I have shoes and shirts categories and their subcategories in the table, I want to display only shirts category and its subcategories show in the front end. please help me to achieve this.
I am getting all the categories and its subcategories from the table
Route:
//here i have added route url for link and well to display categories
Route::get('/products/{url}', 'ProductController#products');
Controller Method:
//this is function/method
public function products($url = null)
{
$countCategory = Category::where(['url'=>$url, 'status'=>1])->count();
if($countCategory==0){
abort(404);
}
//Get All Categories and Sub Categories
$categories = Category::with('categories')->where(['parent_id' => 0])->get();
$categoryDetails = Category::where(['url' => $url])->first();
if ($categoryDetails->parent_id==0) {
//if url is main category url
$sub_categories = Category::where(['parent_id'=>$categoryDetails->id])->get();
foreach ($sub_categories as $sub_cat) {
$cat_ids[] = $sub_cat->id;
}
// echo $cat_ids; die;
$productsAll = Product::whereIn('category_id',$cat_ids)->where('status', 1)->get();
} else {
$productsAll = Product::where(['category_id' => $categoryDetails->id])->where('status', 1)->get();
}
return view('products.listing')->with(compact('categories', 'categoryDetails', 'productsAll'));
}
Category Model:
public function categories()
{
return $this->hasMany('App\Category', 'parent_id');
}
Balde (View) file:
<div class="side-bar col-md-12">
<h3 class="agileits-sear-head">Category</h3>
<div class="panel-group" id="accordian">
{{-- this is the basic way to show the categores inthe table --}}
<?php //echo $categories_menu ?>
#foreach ($categories as $cat)
#if($cat->status=="1")
<div class="panel-heading" >
<h4 class="panel-title">
<a href="#{{ $cat->id }}" data-toggle="collapse" data-parent="#accordian">
<span class="badge pull-right"><i class="fa fa-plus"></i></span>
{{ $cat->name }}
</a>
</h4>
</div>
<div id="{{ $cat->id }}" class="panel-collapse collapse">
<div class="panel-body" >
<ul style="list-style: none; margin-left:30px;">
#foreach ($cat->categories as $sub_cat)
#if($sub_cat->status=="1")
<li>{{ $sub_cat->name }}</li>
#endif
#endforeach
</ul>
</div>
</div>
#endif
#endforeach
</div>
</div>
I appreciate the solution as earlier as possible
sir, I have a question that, I do not want to display my all categories in the sidebar, instead of I want only one category like shoe category and its subcategory I want to display, please guide me how to achieve this. and also i want different category sidebar for different categories,
example,
let's assume we have a shirts listing page where all the shirts, T-shirts and many more shirts related to Shirts Category only.
and i want only Shirts and its subcategory to display in the sidebar.
if i have shoes category and i want to display only shoes category and its subcategory in the sidebar.
please guide me how to achieve this sir,
It is very simple hasMany-belongsTo relationship, let me show you.
<?php
namespace App\models;
use App\pivotes\Catagory;
use Illuminate\Database\Eloquent\Model;
class Catagory extends Model
{
public function children()
{
return $this->hasMany(SELF::class, 'parent_id');
}
public function parentCategory()
{
return $this->hasMany(SELF::class, 'id', 'parent_id');
}
}
Migration file will be
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('category');
$table->unsignedInteger('parent_id')->nullable();
$table->timestamps();
$table->foreign('parent_id')->references('id')->on('comments');
});
for /categories/{category}
Controller method will be
CategoryController extends Controller {
public function show(Category $category)
{
return view('categories.show', $category);
}
}
In view
#foreach( $category->children as $subCategory )
{{ $subcategory->category }}
#endforeach
I have Images. Each image has comments, and each comment has likes. In my view I'm wanting to display the number of likes a comment has by doing something like this:
#foreach ($images as image)
<img href="$image->url">
<div class="comment-box">
#foreach ($image->comments as $comment)
<div class="comment">
<span class="comment-owner">{{ $comment->owner()->name }}</span> {{ $comment->body }}
<div class="likes">
{{ $comment->likes()->count() }} // ** HERE IS MY N+1 PROBLEM **
</div>
</div>
#endforeach
</div>
#endforeach
However now I'm fetching a query each time there is a comment. There could be hundreds of comments on a page, so id be getting hundreds of queries just to find the count() for likes.
I believe there is a way you can grab the count in a controller by doing something like
DB::raw('count(*) as like_count')
but I do not know how to implement this in my situation. Can I put this kind of thing in my model so that I can always call something like {{ $comment->likes()->like_count }}? How can I make $comment->likes->count() work without having the N+1 problem?
Here is some more code:
Media Model
class Media extends Eloquent {
public function comments()
{
return $this->hasMany('Comment');
}
}
Comment Model
class Comment extends Eloquent {
public function media()
{
return $this->belongsTo('Media');
}
public function likes()
{
return $this->hasMany('Like');
}
}
Like Model
class Like extends Eloquent {
public function comment()
{
return $this->belongsTo('Comment');
}
}
My controller
public function imageViewer($id)
{
$images = Media::with(['comments' => function($query) {
$query->with('likes');
}])->where('resource_id', $id)->simplePaginate(1);
return View::make('image-viewer', compact('images'));
}
Why don't you just use blade's count like this count($comment->likes) assuming that $comment->likes gets the list of likes for a comment
#foreach ($images as image)
<img href="$image->url">
<div class="comment-box">
#foreach ($image->comments as $comment)
<div class="comment">
<span class="comment-owner">{{ $comment->owner()->name }}</span> {{ $comment->body }}
<div class="likes">
{{ count($comment->likes) }}
</div>
</div>
#endforeach
</div>
#endforeach
Thanks to #lukasgeiter for pointing out a mistake in the comments