I am creating an basic pagination with Laravel5.1 , I receive the following PHP code.
public function postFindUsers(){
$name= \Request::input('name');
$findUserByNombre = User::where('name', 'LIKE', '%'.$name.'%')->paginate(6);
return view('users.findUsers')->with('users',$findUserByNombre);
}
This code returns the list of users correctly but in the view I don't know how to solve this error , I have the following code.
<div class="hotel-list listing-style3 hotel">
#foreach($users as $usuario)
<article class="box">
<figure class="col-sm-5 col-md-4">
<img width="270" height="160" alt="" src="{{$usuario->foto}}">
</figure>
<div class="details col-sm-7 col-md-8">
<a href="{{ URL::asset('detalle') }}">
<div>
<div>
<h4 class="box-title">{{$usuario->nombre}} {{$usuario->primer_apellido}} {{$usuario->segundo_apellido}}<small><i class="soap-icon-departure yellow-color"></i> {{$usuario->fecha_nacimiento}}</small></h4>
</div>
</div>
<div>
<p>{{$usuario->descripcion}}</p>
<div>
<a class="button btn-small full-width text-center" title="" href="detalle">CONSULTAR</a>
</div>
</div>
</a>
</div>
</article>
#endforeach
{{$users->render()}}
</div>
However , when I put this url in the page public/search?page=2 , this url return a typical error MethodNotAllowedHttpException in RouteCollection.php line 219:
Could anyone help to me ?
/UPDATE/
Route::get('/search-users', 'UserController#getUsers');
Route::post('/search', 'UserController#postFindUsers');
Get request
routes
Route::get('/search', 'UserController#getFindUsers');
controller
public function getFindUsers(){
$name= \Request::input('name');
$findUserByNombre = User::where('name', 'LIKE', '%'.$name.'%')->paginate(6);
return view('users.findUsers')->with('users',$findUserByNombre);
}
HTML
{!! Form::input ('search' , 's' , null , ['class' => 'form-control'] , ['placeholder' => 'Search...']) !!}
You can return both $findUserByNombre and $links in the function postFindUsers()
public function postFindUsers(){
$name= \Request::input('name');
$findUserByNombre = User::where('name', 'LIKE', '%'.$name.'%')->paginate(6);
$links = $findUserByNombre->render();
return view('users.findUsers', compact('findUserByNombre', 'links'));
}
And in the view findUsers.blade.php, just add
<div>{!! $links !!}</div>
And change the request method from post to get.
Route::get('/search', 'UserController#postFindUsers');
To get the 2nd page, just call the following url
public/search?page=2
Related
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();
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');
}
I want to show user orders and use this code:
#foreach($orderHistory as $order)
<div class="with-spacing three-col orders" id="works-grid">
<div class="work-item branding ui-ux">
<div class="work-detail">
<a href="#">
<img src="images/portfolio-1.jpg" alt="">
<div class="work-info">
<div class="centrize">
<div class="v-center">
<h3>{{ $order->product_name }}</h3>
<p> {{ $order->product_price }}</p>
</div>
</div>
</div>
</a>
</div>
</div>
</div>
#endforeach
controller:
$orderHistory = DB::table('cart')
->where('user_id', auth()->id())
->join('singleproducts', 'singleproducts.id', '=', 'cart.product_id')
->select('singleproducts.name', 'cart.qty', 'cart.product_price')
->get();
return view('UI.UserProfile.profile' , compact('orderHistory'));
and the error is: " Undefined property: stdClass::$product_name "
what is the problem?
Typo, you are selecting the column "name" from the database.
#foreach($orderHistory as $order)
<div class="with-spacing three-col orders" id="works-grid">
<div class="work-item branding ui-ux">
<div class="work-detail">
<a href="#">
<img src="images/portfolio-1.jpg" alt="">
<div class="work-info">
<div class="centrize">
<div class="v-center">
<h3>{{ $order->name }}</h3>
<p> {{ $order->product_price }}</p>
</div>
</div>
</div>
</a>
</div>
</div>
</div>
#endforeach
or change the query
$orderHistory = DB::table('cart')
->where('user_id', auth()->id())
->join('singleproducts', 'singleproducts.id', '=', 'cart.product_id')
->select('singleproducts.name as product_name', 'cart.qty', 'cart.product_price')
->get();
return view('UI.UserProfile.profile' , compact('orderHistory'));
The problem is that $orderis not an object and you're trying to access it like an object. Perhaps it's an array - try accessing it as such.
We can't be sure unless we see where $order is defined in your controller maybe. One quick way is to place a #dd($order) just after initializing the for loop. That dumps the variable so you can see it's format.
Your query should be like this:
$orderHistory = DB::table('cart')
->where('user_id', auth()->id())
->join('singleproducts', 'singleproducts.id', '=', 'cart.product_id')
->select('singleproducts.name as product_name', 'cart.qty as quantity', 'cart.product_price as product_price')
->get();
return view('UI.UserProfile.profile' , compact('orderHistory'));
So you end up with an array of objects in this format:
[
{
product_name: 'name',
product_price: 555.00,
quantity: 2
}
...
{}
]
so i am trying to implement search bar, in the blog i'm working on and when i search it gives me undefined variable post in search.blade.php and i have tried all i can . please help
heres my search.blade file
#extends('layouts.main')
#section('content')
<div class="col-lg-8">
#foreach($posts as $post)
<h2>
{!! $post->title !!}
</h2>
<p class="lead">
by {!! $post->author !!}
</p>
<p><span class="glyphicon glyphicon-time"></span> Posted on {!! $post->created_at !!}</p>
<hr>
{!! Html::image('/img/posts/'. $post->image, $post->title, array('style'=>'width: 675px; height:225px;')) !!}{
<hr>
<p>{!! $post->short_desc !!}</p>
<a class="btn btn-primary" href="/store/view/{{ $post->id }}">Read More <span class="glyphicon glyphicon-chevron-right"></span></a>
<hr>
#endforeach
</div>
#endsection
And the controller function
public function getSearch(Request $request) {
$keyword = $request->input('keyword');
$categories = Category::all();
if( $keyword != ""){
return view('store.search')->with('posts', Post::where('title', 'LIKE', '%'.$keyword.'%')->get())
->with('keyword', $keyword)
->with('categories', $categories);
}
else {
return redirect('/') ;
}
}
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);