Opening one post instead of multiple posts - laravel

I've written a blog that's supposed to fetch data from the database. But when I click on the read more link, it keeps opening all the posts that are in the db instead of selecting only one specific post that I've clicked. Can some kindly assist me with this issue?
View
#foreach($data as $aboutus)
<div class="col-lg">
<img src="{{ asset('storage/'.$aboutus->image) }}" class="img-fluid shadow-lg" style="">
<p class="mt-3 text-success fw-bold fs-5 text-lg-start">{{$aboutus->title}}</p>
<p class="fs-6 text-lg-start"> {{$aboutus->description}}</p>
<p class="text-success fw-bold text-lg-start">{{$aboutus->button}}</p>
<!--<img src="images/hr-2.png" class="img-fluid float-start my-2">-->
</div>
#endforeach
Controller
public function show($id)
{
//
$data = AboutUs::find($id)->get();
return view('about.show')->with(compact('data'));
}
Route
Route::get('/about.show/{id:title}','App\Http\Controllers\WelcomeController#show')->name('about.show');

Related

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

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

How to use pagination on ajax data

I want to use default pagination in laravel in view fetched by ajax function. put I get an error.
ajax function
public function get_products($id){
$products = Product::where('category_id',$id)->where('is_hidden','0')->paginate(9);
$category = Category::find($id);
$returnHTML = view('products_ajax')->with('products', $products)->with('category', $category)->render();
return response()->json(array('success' => true, 'html'=>$returnHTML));
}
returned view by ajax
<h3>{{$category->name}}</h1>
<hr>
<div class="cards">
#foreach($products as $product)
<div class="card" data-item-id="{{$product->id}}">
<img style="width:50%;" src="{{asset('storages/images/products/'.$product->image)}}">
<div class="card-details">
<p class="card-brand">{{$product->brand->name}}</p>
<p class="card-name" title="Food Name Here Food Name Here Food Name Here Food Name Here">
{{$product->code}}
</p>
<p class="card-price" hidden> {{$product->price}}</p>
<p hidden class="card-full-des">
{{strip_tags(html_entity_decode($product->description))}}
</p>
<p class="card-packing">
<span>{{$product->packing}}</span>
</p>
{{-- <p class="card-packing">
<span>Packing: 12-8 oz (225g)</span>
</p> --}}
<div class="card-foot">
<button class="mbtn5" onclick="CardAddToCartOrDetails(this, true)">Add to Cart</button>
<button class="mbtn4" onclick="CardAddToCartOrDetails(this, false)">Details</button>
</div>
</div>
</div>
#endforeach
{{$products->links()}}
</div>
</div>
but I get this error:
so, can anyone help me?
The MethodNotAllowedHttpException is caused by a calling a route with a wrong method.
i.e. defining a GET route for /users and calling it with POST would result in this error.

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.

How to make Highlight for search results by Laravel 5.2?

in view:
#if($posts)
#foreach($posts as$post)
<h2>
{{$post->title}}
</h2>
<p class="lead">
by {{$post->user->name}}
</p>
<p><span class="glyphicon glyphicon-time"></span> Posted {{$post->created_at->diffForHumans()}}</p>
<hr>
<img class="img-responsive" src="{{$post->photo->file}}" alt="">
<hr>
{!! str_limit($post->body,35 )!!}
<a class="btn btn-primary" href="{{route('home.post',$post->id)}}">Read More <span class="glyphicon glyphicon-chevron-right"></span></a>
<hr>
#endforeach
#endif
in controller:
public function searchmulti(){
$keyword=Input::get('title');
$posts = Post::where('title', 'like', "%$keyword%")->get();
return view('Admin.comments.postsauthor',compact('posts'));
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
convert search result to array with toArray() method (see here) and then
iterate through results and use str_replace to replace searched word with <span class="highlight">your word</span>
after this you will have $post['title'] (instead of $post->title) with highlighted span inside
of course you must add .highlight with some styling in your css
$posts = Post::where('title', 'like', "%$keyword%")->get()->toArray();
foreach($posts as &$post){
$post['title']=str_replace($keyword,"<span class='highlight'>$keyword</span>",$post['title']);
}
PS if you don't want to convert results to array you can edit tile in eloquent model directly but I prefer arrays to be certain that I don't save changes accidently

Resources