I have a blog module in my laravel with multiple categories.In other categories pagination works just fine but with this category method links() always show the first page.This is my code:
$page = Categoriepage::where('identifiant', 'orientation')->first();
$idArticle = Articleliaison::select(array('idArticle'))->where('idPage',$page->id)->pluck('idArticle');
$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->get()->take(6);
$links = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);
return View::make('pages.orientation')->with('articles',$articles)->with('links',$links);
and in my view:
<?php $i = 1 ?>
<div class="masonry ">
#foreach($articles as $article)
#include('components.article-or')
<?php $i++ ?>
#endforeach()
</div>
<div class="text-center" style="margin-top: 5%">
{{ $links->links() }}
</div>
You always select the first 6 articles because the articles displayed come from:
$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->get()->take(6);
and not from a paginate request.
There is no need to separate the query for the links and the query for the actual articles. Instead of:
$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->get()->take(6);
$links = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);
You can do:
$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);
and in your view:
<div class="masonry ">
#foreach($articles as $article)
#include('components.article-or')
#endforeach
</div>
<div class="text-center" style="margin-top: 5%">
{{ $articles->links() }}
</div>
I think you got this one a bit mixed up.
You retrieve all articles (->get()) and take the first six (->take(6)). And then you retrieve $links using ->paginator(6).
This can and should all be done using just the ->paginator(6), this way Laravel retrieves the correct 6 articles for the ?page=X you are on and renders the links based on the total amount of articles found by the query.
Try it like this:
$page = Categoriepage::where('identifiant', 'orientation')->first();
$idArticle = Articleliaison::select(array('idArticle'))->where('idPage',$page->id)->pluck('idArticle');
$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);
return View::make('pages.orientation')->with('articles',$articles)
<?php $i = 1 ?>
<div class="masonry ">
#foreach($articles as $article)
#include('components.article-or')
<?php $i++ ?>
#endforeach
</div>
<div class="text-center" style="margin-top: 5%">
{{ $articles->links() }}
</div>
you do not have to make two requests but only one
$page = Categoriepage::where('identifiant', 'orientation')->first();
$idArticle = Articleliaison::select(array('idArticle'))->where('idPage',$page->id)->pluck('idArticle');
$articles = Article::whereIn('id',$idArticle)->where('status','enable')->orderby('created_at','desc')->paginate(6);
return View::make('pages.orientation')->with('articles',$articles);
In your view.
<div class="masonry ">
#foreach($articles as $article)
#include('components.article-or')
<?php $i++ ?>
#endforeach()
</div>
<div class="text-center" style="margin-top: 5%">
{{ $articles->links() }}
</div>
Related
I am stucked at point where i got comment count for posts but it shows count of all comments of all posts on every post. I would like to know how to output in blade comment count for post ID
here is controller:
$posts = $posts->orderBy("posted_at", "desc")
->paginate(config("blogetc.per_page", 10));
$comments = BlogEtcComment::all();
return view("blogetc::index", [
'posts' => $posts,
'title' => $title,
'comments' => $comments,
]);
blade:
#foreach($posts as $post)
<section class="blog_area p_120">
<div class="container">
<div class="row">
<div class="col-lg-8">
<div class="blog_left_sidebar">
<article class="blog_style1">
<div class="blog_img">
<img class="img-fluid" src="blog_images/{{$post->image_large}}" alt="">
</div>
<div class="blog_text">
<div class="blog_text_inner">
<div class="cat">
<a class="cat_btn" href="{{$post->url()}}">{{$post->slug}}</a>
<i class="fa fa-calendar" aria-hidden="true"></i>{{$post->created_at}}
<i class="fa fa-comments-o" aria-hidden="true"></i> {{count($comments)}}
</div>
<h4>{{$post->title}}</h4>
<p>{!! $post->generate_introduction(400) !!}</p>
<a class="blog_btn" href="{{$post->url()}}">Lasīt vairāk</a>
</div>
</div>
</article>
</div>
</div>
</div>
</div>
</section>
#endforeach
//Quickfix:
//assuming your posts table is called posts and that in your blogetccomments table //you have a post_id column pointing to the original post. Try something like
$posts = DB::table('posts')
->leftJoin('blogetccomments', 'posts.id', '=', 'blogetccomments.post_id')
->selectRaw('posts.*, count(blogetccomments.post_id) as commentcount')
->groupBy('posts.id')
->get();
In your blade template, Access the comments count for each post, as follows..
#foreach($posts as $post)
...
{{$post->title}}...
{{$post->commentcount}}
...
#endforeach
You can use withCount() to get count of comments for a specific post :
$posts = $posts->withCount('comments')
->orderBy("posted_at", "desc")
->paginate(config("blogetc.per_page", 10));
Above would require you to have comments relation on your Post Model :
Post Model :
public function comments()
{
return $this->hasMany(BlogEtcComment::class);
}
BlogEtcComment Model :
public function post()
{
return $this->belongsTo(Post::class);
}
And then in blade :
#foreach($posts as $post)
<p>Post : $post->id</p>
<p>Comments : $post->comments_count</p>
#endforeach
Usually when I loop through a database table records, I put them in 1 div, however, I have been wondering whether it is possible to create 3 divs and then put one record in each div, then start from the first div again and rinse and repeat.
Example of how I've done it so far:
<div class="container">
#foreach($albumImages as $albumImage)
<div class="centeredImage stickyContainer" style="background-image: url('/storage/uploads/albums/{{$albumName}}/{{$albumImage->file_name}}')">
<a class='specialA' href=''></a>
</div>
#endforeach
</div>
As you can see in this case, all the records are in the container div.
Example of what I've been thinking about:
<div class="flex-grid">
<div class="col-l"></div>
<div class="col-c"></div>
<div class="col-r"></div>
</div>
and have the first record go in col-l, the second in col-c, the third in col-r and then start from col-l again.
Try this
<div class="flex-grid">
#php($count = 0)
#foreach($albumImages as $albumImage)
#if ($count % 3 == 0)
<div class="col-l"></div>
#elseif($count % 3 == 1)
<div class="col-c"></div>
#else
<div class="col-r"></div>
#endif
#php($count++)
#endforeach
</div>
You can use this code but I later will update my answer with more good solution
#php($count = 0)
#foreach($albumImages as $albumImage)
#if ($count % 3 == 0)
#php($albumImages1[] = $albumImage)
#elseif($count % 3 == 1)
#php($albumImages2[] = $albumImage)
#else
#php($albumImages3[] = $albumImage)
#endif
#php($count++)
#endforeach
#if (!empty($albumImages1))
#foreach($albumImages1 as $albumImage)
// your logic here
#endforeach
#endif
#if (!empty($albumImages2))
#foreach($albumImages2 as $albumImage)
// your logic here
#endforeach
#endif
#if (!empty($albumImages3))
#foreach($albumImages3 as $albumImage)
// your logic here
#endforeach
#endif
Also you can split three part your initial array make global helper functions.
define global function
function split_sequence_by_count ($array, $count) {
$result = [];
for ($i = 0; $i < $count; $i++) {
$result[$i] = [];
}
$_count = 0;
foreach ($array as $current) {
$index = $_count % 3;
$result[$index][] = $current;
$_count++;
}
return $result;
}
usage in blade
#php(list ($albumImages1, $albumImages2, $albumImages3) = split_sequence_by_count($albumImages, 3))
#foreach($albumImages1 as $albumImage)
// your logic here
#endforeach
#foreach($albumImages2 as $albumImage)
// your logic here
#endforeach
#foreach($albumImages3 as $albumImage)
// your logic here
#endforeach
Another way would be array_chunk then just run through the array with 2 nested loops. Should be self-explaining.
<?php
$people =
[
'John',
'Paul',
'Ringo',
'George'
];
$i=0;
$classes = ['col-a','col-b','col-c'];
foreach($people as $name) {
$class = $classes[$i++%3];
?>
<div class='<?=$class?>'>
<?=$name?>
</div>
<?php
}
Formatted output:
<div class='col-a'>
John
</div>
<div class='col-b'>
Paul
</div>
<div class='col-c'>
Ringo
</div>
<div class='col-a'>
George
</div>
Regarding placing each name in each column (as per your comment), we could wrangle the array format:
$classes = ['col-a','col-b','col-c'];
$i=0;
foreach($people as $name)
$columns[$classes[$i++%3]][] = $name;
?>
<?php foreach($columns as $class=>$column) { ?>
<div class='<?=$class?>'>
<?php foreach($column as $name) { ?>
<div class="name">
<?=$name?>
</div>
<?php } ?>
</div>
<?php } ?>
Formatted output:
<div class='col-a'>
<div class="name">
John
</div>
<div class="name">
George
</div>
</div>
<div class='col-b'>
<div class="name">
Paul
</div>
</div>
<div class='col-c'>
<div class="name">
Ringo
</div>
</div>
My model code
how we can call this function in blade.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BasicModel extends Model
{
public static function get_product_count($id){
$query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
print_r($query);
return $query->row_array();
}
}
My view.blade.php code
count in foreach loop or show in all category
#foreach ($r as $row)
<li class="grid-item type-rent">
<div class="property-block">
<img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> -->
<div class="property-info">
<h4>{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</h4>
<span class="location">NYC</span>
<div class="price"><strong>Items</strong><span>
<!-- start count code from here -->
$data = $this->BasicModel->count {{ ($row->sub_id) }}
echo $data['count'];
</span></div>
</div>
<!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
</div>
</li>
#endforeach
My BasicController Code
public function grid(Request $request, $id)
{
if ($id == 1) {
$r = DB::table('sub_category')->select('*')->where('cat_id', $id)
->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();
$name = DB::table('category')->where('cat_id', $id)->get();
return view('buy-and-sell/grid', compact('r','name','count'));
}
image for your help
image for your help
problem in this image please solve the problem
Although its no good Practice Accessing the DB in Blade (better do this in the controller and pass the data) you can do:
<div class="price"><strong>Products</strong>
<span>
{{ BasicModel::where('sub_id', $row->sub_id)->count() }}
</span>
</div>
Its not tested, but have a look at the Eloquent docs, the count() method is explained there.
Update: I am not shure if laravel will find the class BasicModel (I never would access Models directly in blade, as stated do this in the controller and pass the data.) So maybe you need to write it with the full Namespace most likely {{ \App\BasicModel::where() }}.
My model code
how we can call this function in blade.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BasicModel extends Model
{
public static function get_product_count($id){
$query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
print_r($query);
return $query->row_array();
}
}
My view.blade.php code
count in foreach loop or show in all category
#foreach ($r as $row)
<li class="grid-item type-rent">
<div class="property-block">
<img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> -->
<div class="property-info">
<h4>{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</h4>
<span class="location">NYC</span>
<div class="price"><strong>Items</strong><span>
<!-- start count code from here -->
$data = $this->BasicModel->count {{ ($row->sub_id) }}
echo $data['count'];
</span></div>
</div>
<!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
</div>
</li>
#endforeach
My BasicController Code
public function grid(Request $request, $id)
{
if ($id == 1) {
$r = DB::table('sub_category')->select('*')->where('cat_id', $id)
->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();
$name = DB::table('category')->where('cat_id', $id)->get();
return view('buy-and-sell/grid', compact('r','name','count'));
}
image for your help
image for your help
problem in this image please solve the problem
I think you are closing the PHP tag in the wrong place. Change to this:
<?php print_r($count);
$data = $this->BasicController->count {{ ($row->sub_id) }}
echo $data['count'];//here is the rightplace to close your PHP ?>
</span></div>
All PHP code needs to stay between the <?php and ?>
Looking to your image, apparently, it's printing the $count and then it's printing the plain text , so you have 80$data = ...
If you counted your rows in controller by the code below
$count = DB::table('products')->where('sub_id', $id)->count();
then why wrote so many code in html for counting?
Remove all code form counting form your html and just use this {{ $count }}
I have this search engine with some advanced search options but I get errors when I use advance options.
When I go to the next pages I see errors.
Here is my code:
Controller:
$Input = Input::all();
$makethis = Input::flash();
$items = Gamefarm::where('roost_hen', '=', Input::get('sex'))
->paginate(6);
return View::make('gamefarms/index', compact('items', 'makethis'));
For my views
#foreach(array_chunk($items->all(), 3) as $row)
<div class="row">
#foreach ($row as $item)
<div class="col-md-4">
<h2>{{ $item->bname}}</h2>
<img src="{{$item->img_loc}}">
<div>{{$item->desc}}</div>
</div>
#endforeach
</div>
#endforeach
{{ $items->appends(Request::except('page'))->links() }}
return View::make('gamefarms/index',compact('items','makethis'))->with('items',$items);