how to not display duplicated data? - laravel

i want to show the categorie list without showing duplicated ones
my controller code :
public function showActualite(){
$actualites=DB::table('actualites')->distinct('categorie')->orderBy('created_at', 'DESC')->paginate(6);
return view ('AllActualite',['actualites' => $actualites]);
}
My view :
#foreach ($actualites as $act)
<span style="text-transform: capitalize;" > {{$act->categorie}} </span>
<span style="text-transform: capitalize;"> </span>
#endforeach
</div>
<div class="row">
#foreach($actualites as $act)
<div class="col-lg-6">
<div class="box wow fadeInLeft">
<div class="icon"><i class="fa fa-newspaper-o"></i></div>
<h4 class="title">{{$act->categorie}}</h4>
<p class="description">{{$act->titre}}</p>
</div>
</div>
#endforeach

I believe your code should work, but in general i would not use DB logic. A more Laravel version of this would be. Latest is a syntax sugar for the same order by logic. See of this solves the problem.
$actualites = Actualites::query()->distinct('categorie')->latest()->paginate(6);

All your answer can be found in this post
$actualites= Actuality::distinct()->get(['categorie']);
Eloquent is used.

Related

how to paginate read notifications in Laravel

I got an error related to the pagination of read notifications. Thank you for your help.
This is my controller code that gives this error when it runs
public function NotificationsRead(User $user)
{
$no = Auth::user()->readNotifications()->paginate(7)->latest()->get();
return view('user-profile.notifications-read', compact('user', 'no'));
}
And when I delete latest(), he gives an error to get()
I do not know how to paginate.
and A have two types notifications
You should do
$no = Auth::user()->readNotifications()->latest()->paginate(7);
In this way you are ordering descending before pagination.
Your code orders the result collection created by pagination.
https://laravel.com/docs/8.x/queries#latest-oldest
#jack this is my Blade
<div class="card-header text-black text-bold">
اعلان ها
</div>
<div class="card-body">
<blockquote class="blockquote mb-0">
<div class="card">
<div class="card-body">
<h5 class="card-title">موضوع:</h5>
#foreach(auth()->user()->readnotifications->where('type','App\Notifications\NewReplySubmittedRoyal') as $notification)
<p class="card-text">{{$notification->data['name']}}
یک پاسخ برای پرسش شما با محتوای:
{{$notification->data['thread_title']}}
در {{$notification->data['time']}}<br>ثبت کرد.
</p>
<form>
<button type="submit" class=" btn btn-outline-success mb-2 ml-2">لینک پرسش</button>
</form>
<hr>
#endforeach
{{--*********** نوع دوم نوتیفیکیشن--}}
#foreach (auth()->user()->readNotifications->where('type','App\Notifications\FollowerNotifications') as $notification)
<p class="card-text">{{$notification->data['name']}} یک پرسش با محتوای:
{{$notification->data['thread_title']}}
ایجاد کرد.
</p>
لینک پرسش
<hr>
#endforeach
{{$no->render()}}
I have two types notifications

making pagination in laravel for image

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

shoping cart data display in laravel

I want to display the order details form database. I store cart data into database using serialize method. When i want to display data i can not properly show it. only can print order object. And there give that a error.That is
(1/1) FatalErrorException syntax error, unexpected 'endforeach'
(T_ENDFOREACH) in 9ad30ab1db6ca668d8a75c428e65858dc800d045.php line 27
.
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>User Profile</h1>
<hr>
<h2>My Oders</h2>
#foreach($orders as $order)
<div class="panel panel-default">
<div class="panel-body">
<ul class="list-group">
#forecah($order->cart->items as $product)
<li class="list-group-item">
<span class="badge">{{$product['product_price']}}
</span>
{{$product['item']['product_name']}} |
{{$product['qty']}} Units
</li>
#endforeach
</ul>
</div>
<div class="panel-footer">
<strong>Total Price : {{$order->cart->totalPrice}}</strong>
</div>
</div>
#endforeach
</div>
</div>
You have typo in your code:
#forecah($order->cart->items as $product)
Try to change into:
#foreach($order->cart->items as $product)e
Hope it works..as it does not detect the foreach loop before.
You have wrong spelling of forecah. you should correct the spelling.
Look at the second foreach and change the spelling.
You can copy code below as i have changed the spelling.
<div class="row">
<div class="col-md-8 col-md-offset-2">
<h1>User Profile</h1>
<hr>
<h2>My Oders</h2>
#foreach($orders as $order)
<div class="panel panel-default">
<div class="panel-body">
<ul class="list-group">
#foreach($order->cart->items as $product)
<li class="list-group-item">
<span class="badge">{{$product['product_price']}}
</span>
{{$product['item']['product_name']}} |
{{$product['qty']}} Units
</li>
#endforeach
</ul>
</div>
<div class="panel-footer">
<strong>Total Price : {{$order->cart->totalPrice}}</strong>
</div>
</div>
#endforeach
</div>

How do I get the second last record of a table in laravel

I want the second last record from my table and search already and
I tried this:
$news2 = News::orderBy('created_at', 'desc')->skip(1)->take(1)->get();
but nothing works.
I get this error:
Property [datum] does not exist on this collection instance. (View: C:\xampp\htdocs\j4ylara\resources\views\user\start.blade.php)
If I want the last one everything works it perfectly.
$news1 = News::all()->last();
and my view is the same
<div class="col-12 col-md-6 news">
<div class="news-content">
<div class="date"><i class="far fa-calendar-alt"></i> {{ $news1->datum }}</div>
<h4 class="news-title">{{ $news1->newstitel }}</h4>
<p class="news-text">{{ $news1->newsbeschreibung }}</p>
<a href="#" class="btn btn-primary">
mehr lesen »
</a>
</div>
</div>
</div>
<div class="row no-gutters news-wrapper">
<div class="col-12 col-md-6 news-image">
<img class="img-fluid" src="https://picsum.photos/385/370/?random">
</div>
<div class="col-12 col-md-6 news">
<div class="news-content">
<div class="date"><i class="far fa-calendar-alt"></i>{{ $news2->datum }}</div>
<h4 class="news-title">{{ $news2->newstitel }}</h4>
<p class="news-text">{{ $news2->newsbeschreibung }}</p>
<a href="#" class="btn btn-primary">
mehr lesen »
</a>
</div>
</div>
</div>
try this
$news2 = News::orderBy('created_at', 'desc')->skip(1)->take(1)->first();
this return only one result if you need more you need used foreach inside blade
The error you are receiving is from the view, you are probably calling to a "datum" property for an object and one of them does not exist:
(View: C:\xampp\htdocs\j4ylara\resources\views\user\start.blade.php)
The query looks good to me
try to use getModel() instead of get() to get query result
Try it:
$news2 = News::orderBy('created_at', 'desc')->offset(1)->take(1)->first();
get() returns collection of News. You must use first()
I GET IIIIIIIIIIIT AHHHHHHHHHH :)
$row = News::count();
$newsid = $row -2;
$news1 = News::all()->last();
$news2 = News::orderBy('created_at', 'desc')->skip($newsid)->take($newsid)->first();
return view('user/start', compact('news1', 'news2', 'newsid'));

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.

Resources