Laravel allow links on message - laravel

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.

Related

How do I do pagination on my code in Laravel?

So my front-end Lists of Businesses are not in paginated style. But I do not know how to do it. Can anyone please help? The code I posted is in my BusinessListController.php
BusinessListController.php
`<?php
namespace App\Http\Controllers;
use App\Models\Business;
use App\Models\Category;
use App\Models\Location;
use Illuminate\Http\Request;
class BusinessListController extends Controller
{
public function index(Request $request)
{
$businesses = Business::query()
->with('location')
->whereFilters($request->only(
['search', 'category', 'location']
))
->get();d
return view('pages.business-list', [
'businesses' => $businesses,
'locations' => Location::all(),
'categories' => Category::all()
]);
}
}`
And then here is the code for my view blade front-end
Business-List.blade.php
<div class="row business-list-row mx-auto">
#foreach ($businesses as $business)
<div class="col-md-4">
<div class="card shadow border-light mb-3">
<img
src="https://cdn1.clickthecity.com/images/articles/content/5d6eba1f4795e0.58378778.jpg"
class="card-img-top" alt="...">
<div class="card-body">
<div class="d-flex justify-content-between">
<div>
<h4 class="card-title h6" style="font-weight: bold;">
{{Str::limit($business->name, 20, $end='...')}}
</h4>
<div class="">
<p class="card-text">
{{ $business->location?->name }}
</p>
<p class="card-text" style="color: #32a852;">
{{ $business->category?->name}}
</p>
</div>
</div>
<div class="align-self-center">
<a href="{{ route('store', $business->id) }}" class="btn btn-info stretched-link">
Visit
</a>
</div>
</div>
</div>
</div>
</div>
#endforeach
</div>
So you need to do three things.
In Controller:
$businesses = Business::query()
->with('location')
->whereFilters($request->only(
['search', 'category', 'location']
))
->paginate(15);
put the number of items you need on a single page. here I put 15.
Put this under the </div> of your list.
{{ $business->links() }}
Put this inside the App\Providers\AppServiceProvider boot method.
use Illuminate\Pagination\Paginator;
public function boot()
{
Paginator::useBootstrapFive(); // or
Paginator::useBootstrapFour();
}
This depends upon which version of Bootstrap you are using.
Still confused? Checkout Laravel Pagination Documentation
Just remove ->get();d and add paginate
example
ModelName()->paginate();

Laravel Pagination not working in Blade file with Tailwind CSS

I'm trying to use Laravel pagination. The issue is that when I use the links() method in the Blade file, it gives an error. For example, when I use the URL "http://127.0.0.1:8000/tournaments?page=3," it works fine, but it gives an error in the Blade file, as explained below.
Controller
public function index()
{
return view('front.tournaments', [
'seriesdata' => $this->tournamentList()
]);
}
public function tournamentList()
{
return Series::leftJoin('team_squads as ts', 'ts.id_series', 'series.id')
->leftJoin('series_team_squads as sts', 'sts.id_series', 'series.id')
->leftJoin('admins as a', 'a.adminable_id', 'series.id')
->where('series.lang', 'en')
->orderBy('series.id', 'asc')
->groupBy('series.id')
->select('series.*')
->paginate(10)
->append([
'logo_url',
'location'
]);
}
Blade
#foreach($seriesdata as $series)
<div class="flex flex-inline xs:flex-col sm:flex-row">
<div class="w-full border-b">
<div class="flex justify-center items-start">
<div class="py-2 mx-auto sm:bg-white xs:bg-white w-full">
<a href="{{ url('tournaments/').'/'.$series->url.'/'.$series->id }}" class="flex">
<div class="grid grid-rows-1 grid-flow-col gap-1">
<div class="row-span-1 col-span-2">
<img src="{{ $series->logo_url }}" alt="avatar" class="object-cover w-12 h-12 mx-4">
</div>
<div class="row-span-1 col-span-2">
<h1 class="font-bold text-lg">{{ $series->name }}</h1>
<p class="uppercase font-light text-sm text-grey">{{ $series->location->address }}</p>
</div>
</div>
</a>
</div>
</div>
</div>
</div>
#endforeach
{{ $seriesdata->links() }}
Error
It looks like your append() method is blocking pagination in blade. Try to remove it from tournamentList() in controller.
When you do ->append() after the ->paginate(), you are transforming what ->paginate() returns (basically a LenghtAwarePaginator) into a Collection, and Collection does not have a ->links() method. You can see this as the error shows you are trying to call links method in Illuminate\Database\Eloquent\Collection, that is how I know what ->append() is returning.

How to fix DELETE method not working in Laravel

My delete method is not deleting from the database and I can't seem to see what it is I'm missing in my code when I click on delete. I've provide my blade file, controller and wweb.php file below, any assistance will be highly appreciated.
Blade File
<div class="row max-inner">
#foreach ($tshirts as $tshirt)
#auth
<form action="{{ route('t-shirtsAdmin.destroy', $tshirt) }}" method="POST">
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger btn-lg">DELETE</button>
</form>
#endauth
<div class="columns col-6 collection-item">
<div class="row">
<a href="#">
<div class="slider">
#foreach (json_decode($tshirt->filenames) as $picture)
<div>
<img src="{{ asset('files/' . $picture) }}" alt="kids top" loading="lazy">
</div>
#endforeach
</div>
<div>
<div>
<div class="columns col"><i class="fas fa fa-child"></i> {{ $tshirt->gender }} top </div>
<div class="columns col"><i class="fas fa fa-calendar"></i> {{ $tshirt->age }} Years</div>
<div class="columns col"><i
class="fas fa fa-tag"></i>
Kshs 250</div>
</div>
<div class="row" style="display: flex; text-align: center; ">
<div class="columns col"><i class="fas fa fa-phone"></i> 0700 00000</div>
</div>
</div>
</a>
</div>
</div>
#endforeach
</div>
web.php
Route::resource('t-shirtsAdmin', 'App\Http\Controllers\TshirtController');
CONTROLLER
<?php
namespace App\Http\Controllers;
use App\Models\Tshirt;
use Illuminate\Http\Request;
class TshirtController extends Controller
{
public function destroy(Tshirt $tshirt)
{
$tshirt->delete();
return redirect(route('t-shirts'))->with('flash', 'T-shirt Deleted Successfully');
}
}
ok , this problem because model binding , you can solve this by 2 way first :-
public function destroy($id)
{
Tshirt::find($id)->delete();
// or Tshirt::destroy($id);
return redirect(route('t-shirts'))->with('flash', 'T-shirt Deleted Successfully');
}
second :- for route binding to work you should have type-hinted variable names match a route segment name, as the doc required : so your method will be like this :-
public function destroy(Tshirt $t-shirtsAdmin)
{
// $t-shirtsAdmin is the name of the router
$t-shirtsAdmin->delete();
return redirect(route('t-shirts'))->with('flash', 'T-shirt Deleted Successfully');
}
you can find the route name var , when you run
php artisan route:list , you will find the var like this : t-shirtsAdmin/{t-shirtsAdmin} so you should use it in controller to bind it.
You cant just call the $tshirt param and expect it to delete, try something like this:
$id = $tshirt->id;
Tshirt::destroy($id);
return redirect(route('t-shirts'))->with('flash', 'T-shirt Deleted Successfully');

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

Call to undefined method Illuminate\Database\Query\Builder::render()

Question like this are asked, i have searched a lot but found nothing which works for me.
Here is Controller function:
public function showHomePage() {
$communities = Community::all();
$ideas = Idea::paginate(8);
return view('publicPages.index', compact(['communities', 'ideas']));
}
Now the paginate method is working but when in view i call the method stated in documentation Docs
{!! $idea->render() !!}
It generates following error
Call to undefined method Illuminate\Database\Query\Builder::render()
I have also tried {!! $idea->render() !!} but the issue is same. Tried this also $ideas = DB::table('ideas')->paginate(8);
Here is view :
#foreach($ideas as $idea)
<div class="lst-popular-project clearfix">
<div class="grid_3">
<div class="project-short sml-thumb">
<div class="top-project-info">
<div class="content-info-short clearfix">
<a href="{{URL::to('showidea', array($idea->id))}}" class="thumb-img">
<img src="{{asset($idea->idea_image)}}" alt="$TITLE">
</a>
<div class="wrap-short-detail">
<h3 class="rs acticle-title">
<a class="be-fc-orange" href="project">
{{$idea->idea_title}}
</a>
</h3>
<p class="rs tiny-desc">
by
<a href="profile.html" class="fw-b fc-gray be-fc-orange">
{{$idea->User->name}}
</a>
</p>
<p class="rs title-description">
{{$idea->idea_info}}
</p>
<p class="rs project-location">
<i class="icon iLocation"></i>
{{$idea->idea_location}}
</p>
</div>
</div>
</div>
</div>
</div>
#endforeach
you must replace
{!! $idea->render() !!}
with
{!! $ideas->render() !!}
$ideas variable contains from,total,page etc
I guess it shoule be $ideas:
{!! $ideas->render() !!}

Resources