how to create pagination to change html in laravel - laravel

<div class="row clearfix">
#foreach($distric->area_list as $area_list)
#foreach($area_list->directory as $directory)
#foreach($directory->dir_category_sub_set as $dir_category_sub_set)
#if($dir_category_sub_set->sub_category_id == '557057')
<div class="col-sm-4 col-xs-6">
<div class="single-product">
<figure>
<img src="/img/content/post-img-12.jpg" alt="">
<div class="rating">
<ul class="list-inline">
<li><i class="fa fa-star"></i></li>
<li><i class="fa fa-star"></i></li>
<li><i class="fa fa-star"></i></li>
<li><i class="fa fa-star-half-o"></i></li>
<li><i class="fa fa-star-o"></i></li>
</ul>
<p>Featured</p>
</div> <!-- end .rating -->
<figcaption>
<div class="bookmark">
<i class="fa fa-bookmark-o"></i> Bookmark
</div>
<div class="read-more">
<i class="fa fa-angle-right"></i> Read More
</div>
</figcaption>
</figure>
<h4>{{$directory->name_of_the_business_place}}</h4>
<h5>{{$directory->address_no_of_the_business_place}},{{$directory->address_street_of_the_business_place}}, {{$area_list->name}},{{$distric->name}}</h5>
<p>T.P:
#if($directory->phone_of_the_business_place>0)
{{$directory->phone_of_the_business_place}}/
#endif
{{$directory->mobile_of_the_business_place}} <br> {{$directory->created_date}}</p>
<a class="read-more" href="https://hanuma.lk/{{$directory->field_link}}"><i class="fa fa-angle-right"></i>Read More</a>
</div> <!-- end .single-product -->
</div> <!-- end .grid-layout -->
#endif
#endforeach
#endforeach
#endforeach
I have this code and I want to create pagination to this how to do this can I do this without any page refreshing? I can not use laravel pagination method because I do not get data from database directly
I searched different methods to add pagination but I could not fined a suitable method for my project

Laravel has provided paginate method that you can use laravel pagination
you only need to retrieve the data and add the method like:
$documents = District::find($id)->area_list()->directory()->get();
return response()->json($documents);
and show the link of pagination in your view like:
{{$documents->links()}}
#foreach($documents as $document)
{{$document->name}}
#endforeach
finish, this is the example how to use pagination.

Related

Laravel blade problems with foreach array|object, null given

First of all, forgive me for my English, it's not my native language and I don't know how to speak/write/express myself very well.
I have a problem with my view. I get the following error:
foreach() argument must be of type array|object, null given (View: /resources/views/admin/stages/index.blade.php)I get the following
in the query result, I see the stage array
Blade code:
#foreach($events as $event)
<div class="box box-default">
<div class="box-header with-border">
<i class="fa fa-road"></i>
<h3 class="box-title">{{ $event->name }}</h3>
</div>
<div class="box-body">
#foreach($event->stages as $stage)
<div class="col-lg-3 col-xs-6">
<!-- small box -->
<div class="small-box bg-light-blue-gradient">
<div class="inner">
<h3>SS{{ $stage->stage_num }}</h3>
<p>{{ $stage->name }}<br>{{ $stage->date }}</p>
</div>
<div class="icon">
<i class="fa fa-map-signs"></i>
</div>
{{ __(ucfirst($stage->state)) }} <i class="fa fa-arrow-circle-right"></i>
</div>
</div>
<!-- ./col -->
#endforeach
</div>
<!-- /.row -->
</div>
<!-- /.box -->
#endforeach
I update from Laravel 5.8 to 8.45.1
Thank you for all

I want to show assigned users using laravel

I am trying to show assigned users to the user I have made relationship b/w user and user_permission but I am facing an error.how to fix it thanks. please check this error https://flareapp.io/share/q5Y12O5X#F52 Can anyone provide me a solution that where I am wrong.
Database table
user_permission table
please see here https://ibb.co/7486n95
user table
please see here https://ibb.co/5jmqb0H
Controller
public function index()
{
$users=User::with('permission')->where('id' ,'!=' ,Auth::id())->paginate(6);
return view('index',compact('users'));
}
User Model
class User extends Authenticatable
{
public function permission()
{
return $this-
>hasMany('App\Users_permissions','user_Access_id','id');
}
}
Users_permissions Model
class Users_permissions extends Model
{
protected $table = 'user_permission';
protected $fillable = ['id','user_id', 'user_Access_id'];
public function user()
{
return $this->belongsTo('App\User','id');
}
}
Html View
<!-- Default box -->
<div class="card card-solid">
<div class="card-body pb-0">
<div class="row d-flex align-items-stretch">
#foreach($users->permission as $user)
<div class="col-12 col-sm-6 col-md-4 ">
<div class="card bg-light">
<div class="card-header text-muted border-bottom-0">
<div class="card-tools"> <span data-toggle="tooltip" title="3 New Messages"
class="badge badge-primary"></span>
</div>
</div>
<div class="card-body pt-0">
<div class="row">
<div class="col-7">
<h2 class="lead"><b>{{$user->name}}</b></h2>
<p class="text-muted text-sm"><b>Job: </b> {{$user->job}}</p>
<ul class="ml-4 mb-0 fa-ul text-muted">
<li class="small"><span class="fa-li"><i class="fas fa-lg fa-phone"></i></span> Phone
#: {{$user->phone_number}}</li>
</ul>
</div>
<div class="col-5 text-center">
<img src="{{url('public/assets/dist/img/user1-128x128.jpg')}}" alt="" class="img-
circle img-fluid">
</div>
</div>
</div>
<div class="card-footer">
<div class="text-left">
#if($user->isOnline())
<i class="fa fa-circle text-success"></i> Online#else
<i class="fa fa-circle text-default"></i> offline#endif</div>
<div class="text-right">
<a href="{{url('/chat',$user->id)}}" class="btn btn-sm bg-teal"> <i class="fas fa-
comments"></i><b> Chat</b>
</a>
</div>
</div>
</div>
</div>#endforeach</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<nav aria-label="Contacts Page Navigation">
<ul style="list-style-type:none;">
<li>{{$users->links()}}</li>
</ul>
</nav>
</div>
<!-- /.card-footer -->
</div>
<!-- /.card -->
Here, what you are trying to do this is you are directly accessing permission relationship from collection, this is why it is showing error. First you have to loop through users and inside that particular user permission relationship exist. So you have to loop through users model first.
Hope this helps:)
You are applying permissions to the collection of users, but only an individual user has permissions. You need to loop through all of your users, then access the users permissions:
#foreach($users as $user)
...
#foreach($user->permission as $permission)
{{$permission->name}}
#endforeach
#endforeach

How not to show image if there is none?

I have three table. Category, PostAd and PostImage.
To show post i have to show it from category table.
My controller Code.
$data['category'] = Category::with(['child','children','parent','postads.postimage','postads'=>function($q) use ($asc){
$q->orderBy('created_at',$asc);
}])->where('id',$id)->get();
To display image i have to use nested relationship in postads.postimage.
Blade Code
#foreach($category as $cat)
#foreach($cat->postads as $c)
<a href="{{route('particular',['id'=>$c->id])}}">
<li>
#foreach($c->postimage as $pi)
<img src="{{asset('thumbnail/'.$pi->image)}}" alt="No image" style="margin-top: 5px" >
#endforeach
<section class="list-left">
<h5 class="title">{{$c->adtitle}}</h5>
<span class="adprice">Rs. {{$c->price}}</span>
<p class="catpath">{{$cat->categoryname}} ยป {{$cat->categoryname}}</p>
</section>
<section class="list-right">
#auth
<div class="like1">
<i class="fas fa-heart" pid="{{$c->id}}" uid="{{auth()->user()->id}}"></i>
</div>
#endauth
<span class="date">{{date('D',strtotime($c->created_at))}}-{{date('M',strtotime($c->created_at))}}-{{date('Y',strtotime($c->created_at))}}</span>
<span class="cityname">{{$c->address}}</span>
</section>
<div class="clearfix"></div>
</li>
</a>
#endforeach
#endforeach
because of foreach loop my design gets damaged.
I want to show image alt but because of this nothing is shown and everything gets damaged.
You can use #forelse
#forelse($c->postimage as $pi)
<img src="{{asset('thumbnail/'.$pi->image)}}" alt="No image" style="margin-top: 5px" >
#empty
<!-- some HTML, default image or something, whatever you need -->
#endforelse
https://laravel.com/docs/5.8/blade#loops

Laravel validation on array of radio inputs

I'm developing a module that have multiple radio inputs and has numeric index,
I have 15 radio input groups and each group has 7 radio input members with the same index.
I want to check each input if empty.
HTML output
I have tried dynamic requests validation on laravel it failed to validate.
I tried to hard code the validation of 15 inputs = failed,
Then I tried to test it individually and adding next index on success and it only validates up to 12th item/index.
my valdation code
$rules = [];
foreach($this->request->get('ratingsd') as $key => $val)
{
$rules["ratings[$val]"] = 'required';
}
<div class="card mb-3 bg-info">
<ul class="list-group list-group-flush bg-light">
#foreach($items as $index=>$group)
<li class="list-inline">
<h5 class="card-header text-white bg-info">
{{$group->group_name}}
</h5>
</li>
#foreach($group->items as $items=>$item)
<li class="list-group-item">
<span class="font-weight-bold">
{{ $item->number }} {{ $item->entry }}
</span>
<div class="float-md-right form-inline mt-2">
#foreach($ratings as $rate_value=>$rate_text)
{!!Form::hidden("ratingsd[".$item->item_id."]", $item->item_id)!!}
{!!Form::radio("ratings[$item->item_id]", $rate_text, "$group->group_id*$item->item_id*$rate_value")->inline()!!}
#endforeach
{!!Form::textarea("remarks[".$item->item_id."]", '')->placeholder('Remarks')->inline()!!}
</div>
</li>
#endforeach
#endforeach
<li class="list-inline bg-info text-white">
<h5 class="card-header">
Overall Remarks
</h5>
</li>
<li class="list-inline-item p-1">
{!!Form::textarea("overall_remark", '')->placeholder('Remarks')->inline()!!}
</li>
<li class="list-inline-item text-center p-2">
<button class="btn btn-primary btn-block" type="submit">
Submit
</button>
</li>
</ul>
</div>
I expect to validate properly each "ratings" radio inputs

Laravel 5: How to get posts by specific categories and display in tabs

I'm building a news site. I want to get 6 latest posts by 3 specific categories, lets say id 2, 3, 4, and display them in 3 tabs on homepage.
Each tab is divided in 2 column, col-6. one col-6 display the latest record, the other display a list of another 5 records after the latest one.
Here's my controller, I get 6 latest records from database
$post_tabs = Post::with('categories')->latest()->limit(6)->get();
this I get 3 categories that have id 2, 3, 4
$category_tabs = Category::with('posts')->whereIn('id', [2, 3, 4])->get();
In blade view, I managed to display that 3 category tabs, like so
<div class="collapse navbar-collapse justify-content-between" id="navbarToggler1">
<ul class="nav nav-sm navbar-nav mr-md-auto mr-lg-0 mt-2 mt-lg-0 align-self-end" role="tablist">
#foreach($category_tabs as $tab_category)
<li class="nav-item">
<a class="nav-link bg-color-tech active" id="nav-outdoor-tab-{{$tab_category->id}}" data-toggle="tab" href="#nav-outdoor-{{$tab_category->id}}" role="tab" aria-selected="true">
{{$tab_category->title}}
</a>
</li>
#endforeach
</ul></div>
Now I don't know how to display posts of those categories by tabs, this is what i 've got so far, which only display 6 latest post and I don't know how to make tabs work either
#foreach($post_tabs as $key => $post_tab)
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-outdoor-{{-- {{$tab_category->id}} --}}" role="tabpanel">
<div class="row clearfix">
<div class="col-lg-6">
#if ($loop->first)
<article>
<div class="entry-image mb-3">
<img src="{{asset($post_tab->thumbnail)}}" alt="{{$post_tab->title}}">
#foreach($post_tab->categories as $category)
<div class="entry-categories">{{$category->title}}</div>
#endforeach
</div>
<div class="entry-title">
<h3><a target="_blank" href="{{route('post.show',[$post_tab->slug])}}">{{$post_tab->title}}</a></h3>
</div>
<div class="entry-content clearfix">
<p>{{$post_tab->description}}</p>
</div>
</article>
</div>
<div class="col-lg-6">
#foreach($post_tabs as $key => $post_tab)
#if($key > 0)
<article>
<div class="entry-image">
<img src="{{asset($post_tab->thumbnail)}}" alt="">
</div>
<div class="entry-c">
<div class="entry-title">
<h4>{{$post_tab->title}}</h4>
</div>
</div>
</article>
#endif
#endforeach
#endif
</div>
</div>
</div>
</div>
#endforeach
Thank you so much.
You don't need to fetch category and post separately. You can fetch both using eager loading. For that you can define extra relationship in Category model like this
Category Model
public function latestPosts(){
return $this->hasMany('App/Post')->latest()->take(6);
}
public function beforeLatestPosts(){
return $this->hasMany('App/Post')->latest()->slice(6)->take(5);
}
Fetch in controller
$category_tabs = Category::with('latestPosts', 'beforeLatestPosts')->whereIn('id', [2, 3, 4])->get();
In view
Print Tab header
<div class="collapse navbar-collapse justify-content-between" id="navbarToggler1">
<ul class="nav nav-sm navbar-nav mr-md-auto mr-lg-0 mt-2 mt-lg-0 align-self-end" role="tablist">
#foreach($category_tabs as $tab_category)
<li class="nav-item">
<a class="nav-link bg-color-tech active" id="nav-outdoor-tab-{{$tab_category->id}}" data-toggle="tab" href="#nav-outdoor-{{$tab_category->id}}" role="tab" aria-selected="true">
{{$tab_category->title}}
</a>
</li>
#endforeach
</ul>
</div>
Print Tab Content
#foreach($category_tabs as $tab_category)
<div class="tab-content" id="nav-tabContent">
<div class="tab-pane fade show active" id="nav-outdoor-{{$tab_category->id}}" role="tabpanel">
<div class="row clearfix">
<div class="col-lg-6">
#foreach($tab_category->latestPosts as $latestPost)
<article>
<div class="entry-image mb-3">
<img src="{{asset($latestPost->thumbnail)}}" alt="{{$latestPost->title}}">
</div>
<div class="entry-title">
<h3><a target="_blank" href="{{route('post.show',[$latestPost->slug])}}">{{$latestPost->title}}</a></h3>
</div>
<div class="entry-content clearfix">
<p>{{$latestPost->description}}</p>
</div>
</article>
#endforeach
</div>
<div class="col-lg-6">
#foreach($tab_category->beforeLatestPosts as $beforeLatestPost)
<article>
<div class="entry-image">
<img src="{{asset($beforeLatestPost->thumbnail)}}" alt="">
</div>
<div class="entry-c">
<div class="entry-title">
<h4>{{$beforeLatestPost->title}}</h4>
</div>
</div>
</article>
#endforeach
</div>
</div>
</div>
</div>
#endforeach

Resources