How to limit my laravel posts just on home page - laravel-5

So I was wondering if I could limit the number of posts just for my homepage
Homepage
Here I check there are any posts and if so I display the latest one full width
#if(count($posts) > 0)
{{-- Get first post --}}
<div class="col-sm-12" style="padding: 0px;">
<a href="posts/{{$posts->first()->id}}">
<div class="post-icon">
<img style="width: 100%;" src="storage/cover_images/{{$posts->first()->cover_image }}">
</div>
<div class="bottom-left">
<button class="btn bg-dark text-light" type="submit">
<h5>
{{$posts->first()->title}};
</h5>
</button>
</div>
<div class="top-right-categorie">
<div class="categorie" style="background-color: {{$posts->first()->categorie['hex']}};">
{{$posts->first()->categorie['title']}}
</div>
</div>
</a>
</div>
<div class="w-100">
<br>
</div>
#endif
PostController
public function index()
{
$posts = Post::all();
return view('posts.index')->with('posts', $posts);
}
Page

You can easily find this in the Laravel's model documentation:
You can try:
$posts = Post::->take(2)->get();
return view('posts.index')->with('posts', $posts);
This will return the first 2 posts, replace "2" with the number of posts that you need

You can use:
$post = Post::all()->limit(5);
return view('posts.index')->with('posts', $posts);
Or use pagination:
$post = Post::all()->paginate(5);
return view('posts.index')->with('posts', $posts);

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();

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

i want to hide html code when product is_featured column status all equal to zero using laravel

i am trying to hide html code by if condition when all product featured is zero, html code should not be shown in front page please help me how can i do that ?
controller
public function index()
{
$data = [
'products' => Product::with('productColorGallary')->where('is_featured', 1)->first(),
];
return view('home', $data);
}
html view
#if($products->is_featured == 0)
<div class="col-md-6">
<div class="new-wall-image">
<img src="{{config('wall_master_furishing.file_url').$products->productColorGallary->featured_image}}" alt="">
</div>
</div>
<div class="col-md-6">
<div class="new-wall-descp">
<h2 class="theme-title">New Walls
</h2>
<p>{!!$products->description!!}</p>
<a class="blue-btn-a" href="#">Read More
<i class="fa fa-angle-right">
</i>
</a>
</div>
</div>
#endif
You are getting your products in your database with is_featured = 1. It means that your if condition in your blade will be always false.
Plus, are you trying to get all products or just one ?
If it's many, then :
Your controller
public function index()
{
$products = Product::with('productColorGallary')->get();
return view('home', compact('products');
}
and your blade
#foreach($products as $product)
#if($product->is_featured == 0)
<div class="col-md-6">
<div class="new-wall-image">
<img src="{{config('wall_master_furishing.file_url').$product->productColorGallary->featured_image}}" alt="">
</div>
</div>
<div class="col-md-6">
<div class="new-wall-descp">
<h2 class="theme-title">New Walls
</h2>
<p>{!!$products->description!!}</p>
<a class="blue-btn-a" href="#">Read More
<i class="fa fa-angle-right">
</i>
</a>
</div>
</div>
#else
SOMETHING HERE IF PRODUCT IS FEATURED
#endif
SOMETHING HERE COMMONS TO BOTH FEATURED AND NOT FEATURED
#endforeach
Products is an array key, so you can use like $data['products'] etc..
But if you create a collection/object like the following should be work:
public function index()
{
// first will return the first matched item from db
// and you will get only is_featured = 1
$products = Product::with('productColorGallary')->where('is_featured', 1)->first();
return view('home', compact('products');
}

How to increment the count value in DB when button is clicked in laravel?

I have Home.blade.php when I click the vote button in that page, the count value should increment in the candidate table.
Here my Home.blade.php
#foreach ($file as $show)
<div class="col-md-3">
<div class="card">
<img src="p3.png" alt="John" style="width:100%">
<div class="card-body">
<h5 class="title">President</h5>
<p class="card-text">Name : {{$show->name}}</p>
<!-- <p><button>VOTE</button></p> -->
<button type="button" class="vote">VOTE</button>
</div>
</div>
</div>
#endforeach
I have table name called the candidate, when vote button is clicked, the count should be incremented in database. And i have controller called HomeController pls help me.
Thank you
class Homecontroller extends Controller
{
public function pre()
{
$file = DB::table('candidate')
->select('name','branch')
->where('candidate.post_id', '=', 1)
->get();
return view('/home')->with('file',$file);
}
}
#foreach ($file as $show)
<div class="col-md-3" style="margin-bottom: 20px;">
<div class="card">
<img src="p3.png" alt="John" style="width:100%">
<div class="card-body">
<h5 class="title">President</h5>
<p class="card-text">Name : {{$show->name}}</p>
<p><button>VOTE</button></p>
</div>
</div>
</div>
#endforeach
i have used funtion redirect to controller
in controller i have used this function
public function Count(){
DB::table('candidate')->increment('count',1);
return view('/home');
}
still its not working
this is my Route
Route::get('/home','Homecontroller#count');

POST data from input within loop

I have a lot of products from a database. I display them with a loop. When a user clicks "more", I want to display its details. The problem is, for example: I have four products A, B, C, and D. When a user clicks A's details, it displays D's details or randomly. How can I fix this? This is my code:
<form action="details" method="post" enctype="multipart/form-data">
#foreach($images as $image)
<div class="card" >
<div class="card-inside1" >
<div class="center">
<img class="card-img-top img-fluid" src="<?php echo asset('images').'/'.$image->image1 ?>" alt="Card image" >
</div>
<div class="card-body">
<input type="text" name="imagename" value="{{$image->product_name}}"></input>
<button type="submit" value="Submit" name="more" class="btn btn-primary ">More</button>
</div>
</div>
</div>
#endforeach
</form>
route
Route::post("details",'UserController#detail');
controller
public function detail(Request $req)
{
$productname = $req->input('imagename');
return $productname;
}
There is no need of using a form to accomplish what you want.
#foreach($images as $image)
<div class="card" >
<div class="card-inside1" >
<div class="center">
<img clas="card-img-top img-fluid" src="<?php echo asset('images').'/'.$image->image1 ?>" alt="Card image" >
</div>
<div class="card-body">
<input type="text" name="imagename" value="{{$image->product_name}}"></input>
More
</div>
</div>
</div>
#endforeach
Route:
Route::get("details/{imagename}",'UserController#detail');
Controller:
public function detail(Request $req)
{
$productname = $req->imagename;
return $productname;
}
UPDATE
The reason why is taking the last product is because you are sending all products through the form. and it takes the last one that sends, not the one you clicked.
view
#foreach($images as $image)
{{ $product->name }}
#endforeach
web.php
Route::get("details/{id}",'ProductController#show')->name('product.show');
ProductController.php
public function show($id)
{
$product = Product::find($id);
return $product
}

Resources