making pagination in laravel for image - laravel

Hello for everyone I have a problem in laravel that about the showing images in one view, the problem is: I have a page that just show the images but it show all the images stored in table with specific id but I want to show some of that and have a button (show more) and when click on button it show the other images I don't know hove can I do this if someone can help me please!
This is my controller code:
public function listHostel()
{
$hostels = Hostel::all();
return view('front/khabgah_list',compact('hostels'));
}
And this is my blade code:
#foreach($hostels as $hostel)
#foreach($hostel->attachments as $photo)
<div class=" col-12 col-sm-6 col-md-6 col-lg-3 px-1 mt-3">
<div class="card card-shadow custom-height-1 " style="border-radius: 0%">
<a href="">
<img src="/assets-/app/media/img/blog/hostels-img/{{$photo->file_name}}"
class="card-img-top card-img custom-card-img-height" alt="">
</a>
<div class="car-body">
<div class="card-footer">
<div class="custom-circle">
<p class="custom-circle-text card-text">
<b>
#if($hostel->type == 1)
{{ 'ذکور'}}
#else
{{ 'اناث' }}
#endif
</b>
</p>
</div>
<div class="custom-prices card-text text-left"> کرایه فی ماه
: {{$hostel->remark }}
</div>
</div>
</div>
</div>
</div>
#endforeach
#endforeach

Pagination is likely what you're looking for. Change your controller to:
public function listHostel()
{
$hostels = Hostel::all()->paginate(5);
return view('front/khabgah_list',compact('hostels'));
}
Then you can get the links in your Blade template:
{{ $hostels->links() }}
You might need to change it a bit to work for your specific situation. The documentation gives some more info: https://laravel.com/docs/5.8/pagination

Related

Foreach loop returns all results

I have a column of album covers. I want to take me to the album details after click on the cover. Actually it takes me to the album details but it shows all results, not only this one which was clicked.
AlbumDetailsController
public function index(): View
{
return View("details.index", [
'albums' => Album::paginate(1)
]);
}
AlbumDetails Blade view
#extends('layouts.app')
#section('content')
<div class="container">
#foreach ($albums as $album)
<h1 class="my-4">{{ $album->name }}</h1>
<div class="row">
<div class="col-lg-6 mb-4">
<div class="card h-100">
<div class="card-body">
<h4 class="card-title">
</h4>
{{ $album->artist }}
</div>
</div>
</div>
<div class="col-lg-6 mb-4">
<div class="card h-100">
<img src="{{asset('storage/' . $album->image_path)}}" alt="" class="img-fluid card-img-top">
<div class="card-body">
<h4 class="card-title">
{{ $album->artist }}
</h4>
{{ $album->description }}
</div>
</div>
</div>
</div>
#endforeach
#endsection
I've tried to solve this in differents ways but everything is not working. I think problem is with my controller but I'm really don't know how to modify it to show only this item which was clicked.

After Click Add to Cart Button then Button Change to Go to Cart in Laravel 8

Please help me, i'm right now try to change Add to Cart Button to Go to Cart after click, how possible I can do that?, please help me, I have trying but didn't work, here my condition code in Controller:
if (Temporary::where('course_id','=', $request->IDCourse)->exists()) {
Session::flash('failed-session','Go to card');
}
else
{
Session::flash('failed-session','Add to cart');
}
and below my blade for looping product on cart box
#foreach($carts as $cart)
<ul class="list-group list-group-flush">
<li class="list-group-item bg-light">
<div class="row">
<div class="col">
<input type="text" name="IDCourse" value="{{ $cart->course_id }}">
<a href="{{ route('course.detail', $cart->course->id) }}" class="text-body">
<div class="d-flex">
#if($cart->course->image == '')
<img src="{{ asset('storage/courses/Default_Photo.png') }}" alt="" class="avatar-md square" />
#else
<img src="{{ asset('storage/courses/'.$cart->course->image) }}" alt="" class="avatar-md square" />
#endif
<div class="ms-3">
<h5 class="fw-bold mb-1">
{{ $cart->course->title }}
</h5>
<span class="fs-6 text-muted">
<span>{{ __('Oleh ') }}{{ $cart->course->user->name }}</span>
</span>
</div>
</div>
</a>
</div>
</div>
</li>
</ul>
<hr class="my-0">
#endforeach
And the last code to display the result from condition in controller
#if(Session::has('failed-session'))
<div class="alert alert-danger">
{{Session::get('failed-session')}}
</div>
#endif
I try dd($request->course_id) and displaying null. My planning is to check if customer have add to cart that item then the button change to Go to Cart. Please help me.Thank u so much for any help.
simply just check this item in your controller then initialize the variable and check if variable is empty or not , then you can change the action of the form or change the src of the button ,
for example : if user->cart->count() > 0
redirect to the page
you can add another button for continue shopping as well too .

laravel blade limiting the showing images proplem

everyone, I have a problem in laravel blade I have a page for my website that show all the image because my website is a sale website but when I load the page it shows all the images but I don't want to show all the images I want to hide a images after three or four columns if someone can help me please.
thanks for everyone.
this is my blade code:
<section class="container custom-margin ">
<h5 class="text-center" style="text-shadow: 1px 1px 2px black; color: #1b1e21">مجموعه ما با ۲۰۰۰ خوابگا در سطح کشور</h5>
<div class="justify-content-center thin-underline-1"></div>
<div class="row justify-content-center">
#foreach($hostels as $hostel)
#foreach($hostel->hostelDetails->attachments as $photo)
<div class=" col-12 col-sm-6 col-md-6 col-lg-3 px-1 mt-4 small-device-hid ">
<div class="card card-shadow custom-height-1 " style="border-radius: 0%">
<a href="{{route('khabgah_detailes.goToDetails',$hostel->id)}}"> <img src="/images/{{ $photo->file_name }}"
class="card-img-top card-img custom-card-img-height" alt=""></a>
<div class="car-body">
<div class="card-footer">
<div class="custom-circle"><p class="custom-circle-text card-text"><b>
#if($hostel->type == 1)
{{ 'ذکور'}}
#else {{ 'اناث' }}
#endif
</b></p></div>
<div class="custom-prices card-text text-left"> کرایه فی ماه : {{$hostel->hostelDetails->remark }} </div>
<div class="row mt-3">
<div class="col-12 col-sm-12 col-md-12 mb-2 ">
<span class="card-text">آدرس : {{$hostel->addresses->state }} {{$hostel->addresses->rood}}
{{$hostel->addresses->station }} {{$hostel->addresses->alley}}
</span>
</div>
</div>
</div>
</div>
</div>
</div>
#endforeach
#endforeach
</div>
</section>
Since every Laravel relation return a collection instance, you could as well use the take method to obtain the first N objects in your collection.
For example:
#foreach($hostel->hostelDetails->attachments->take(3) as $photos)
Another way would be to use the query builder of the attachments relation to limit the results at query level:
#foreach($hostel->hostelDetails->attachments()->limit(3)->get() as $photos)
The latter way would love preferred performance-wise as it would only fetch the needed models from the database. The former would fetch all the models from the database and would only display a few of them.

Laravel allow links on message

I need allow links in messages, when user send message. How I can do this?
#foreach($mess as $message)
<li data-mid="{{ $message->id }}">
<div class="row margin-left-none margin-right-none">
<div class="col-md-2 padding-right-none">
<div class="avatar-user text-center">
<img src="{{ $message->sender->avatar }}" alt="$message->sender->name">
</div>
</div>
<div class="col-md-10 padding-left-none">
<div class="user-name">
<span>{{ $message->sender->name }}
#if(Helper::getOnlineUser($message->sender->id))
<i data-toggle="tooltip" title="Онлайн" class="material-icons online">fiber_manual_record</i>
#else
<i data-toggle="tooltip" title="Оффлайн" class="material-icons offline">fiber_manual_record</i>
#endif
<span class="date float-right">{{ $message->created_at->formatLocalized('%H:%m') }}</span></span>
</div>
<div class="message">
{{ $message->body }}
</div>
</div>
</div>
</li>
#endforeach
This is view of messages.
How I can allow links?
Example: http://example.com in messages =>
http://example.com
I need {!! $message->body !!} ? Or How?
In controller:
$mess = Chat::conversations($conversation)->for($user)->getMessages($limit, $request->page);
I use this package: https://github.com/musonza/chat
There are built in PHP functions to handle this:
<div class="message">
{!! strip_tags($message->body, '<a>') !!}
</div>
Where the second argument is your whitelist.
This has the following advantages:
You're not messing with regex
You're still using blade {!!!!}
You're using well tested code dev'ed by the core team
You can do it by using following php function in your blade,
<?php
function getDataWithURL($text){
// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {
// make the urls hyper links
echo preg_replace($reg_exUrl, "".$url[0]." ", $text);
} else {
// if no urls in the text just return the text
echo $text;
}
}
?>
You just need to add this code in your blade file and then you can check and replace the links in your text like this,
<div class="message">
<?php getDataWithURL($message->body);?>
</div>
I hope you will understand.

Show a collection in laravel from controller on different columns in view

got this problem:
I want to show the info from a collection where a publication has a "featured" column in true.
$pub_destacadas = Publicaciones::take(3)->where('destacado', '=', 1)->get();
The problem is that my layout has 3 elements to fill with this information that have different sizes so i wasn't able to solve this using the chunk() method in the foreach in my blade template, so I tried the following:
$individual_destacada = $pub_destacadas->take(1);
$grupo_destacada = $pub_destacadas->take(2);
and show them in the view as follows:
<div class="publicaciones-destacadas">
<div class="container">
<h2>Publicaciones Destacadas</h2>
<div class="row">
#foreach($individual_destacada as $destacada)
<div class="col-md-6">
<div class="item-destacado size-2">
<img class="img-responsive" src="{{ asset('img/publicaciones/' . $destacada->imagen ) }}">
{{ $destacada->titulo }}
{{ $destacada->descripcion }}
</div>
</div>
#endforeach
<div class="col-md-6">
#foreach($grupo_destacada as $destacada)
<div class="row doble-publicacion">
<div class="col-md-12">
<div class="item-destacado size-1">
<img class="img-responsive" src="{{ asset('img/publicaciones/' . $destacada->imagen ) }}">
{{ $destacada->titulo }}
{{ $destacada->descripcion }}
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
</div>
So... now it shows it correctly in the layout but the first item that the query gets is shown 2 times in this layout. How can I exclude this first element in the second variable where I get the featured publications?
Hope I explained the problem correctly.
Here's the layout:
You can use splice.
$individual_destacada = $pub_destacadas->splice(1);
$grupo_destacada = $pub_destacadas->splice(2);

Resources