I am an Laravel beginnner and try now to build an Simple Searchbar on my site.
But I get this error:
Class 'products' not found
Can someone tell me please what I have forget in my Controllers?
Search form on Index:
<ul class="searchbar">
<form action="/search" class="light" method="POST" role="search">
{{ csrf_field() }}
<input type="text" class="form-control" name="q" placeholder="Find your item" />
<input type="submit" value="Search" />
</form>
search.blade.php:
@extends('master.main')
@if(Auth::check())
@section('main-content')
@component('master.notification')
@slot('size')
col-md-8 col-md-offset-2
@endslot
@slot('title')
Product Search
@endslot
<div class="container">
@if(isset($products))
<h2>Product Search</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Product</th>
<th>Description</th>
</tr>
</thead>
<tbody>
@foreach($products as $dummy)
<tr>
<td>{{$dummy->name}}</td>
<td>{{$dummy->description}}</td>
</tr>
@endforeach
</tbody>
</table>
{!! $products->render() !!}@endif
</div>
<div class="container">
@if(isset($details))
<p> The Search results for <b> {{ $query }} </b> are :</p>
<h2>Product Search</h2>
<table class="table table-striped">
<thead>
<tr>
<th>Product</th>
<th>Description</th>
</tr>
</thead>
<tbody>
@foreach($details as $products)
<tr>
<td>{{$products->name}}</td>
<td>{{$products->description}}</td>
</tr>
@endforeach
</tbody>
</table>
@if($details){!! $details->render() !!}@endif
@elseif(isset($message))
<p>{{ $message }}</p>
@endif
</div>
@endcomponent
@stop
@endif
web.php:
Route::get ( '/', function () {
$mydatabase = products::paginate(25);
return view ( 'search' )->withproducts($mydatabase);
} );
Route::any ( '/search', function () {
$q = Input::get ( 'q' );
if($q != ""){
$products = products::where ( 'name', 'LIKE', '%' . $q . '%' )->orWhere ( 'description', 'LIKE', '%' . $q . '%' )->paginate (5)->setPath ( '' );
$pagination = $products->appends ( array (
'q' => Input::get ( 'q' )
) );
if (count ( $products ) > 0)
return view ( 'search' )->withDetails ( $products )->withQuery ( $q );
}
return view ( 'search' )->withMessage ( 'No Products found. Try to search again !' );
} );
The error comes from:
Route::get ( '/', function () { $mydatabase = products::paginate(25);
How is products
or Product::paginate
defined or must I use in web.php
the ProductController@...
? Yes, I have found out its not my database table products
;) I think Product
instead of products
are correct, right?
/App/Product.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Storage;
class Product extends Model
{
public function category(){
return $this->belongsTo('App\Category');
}
public function seller(){
return $this->belongsTo('App\User');
}
public function buyer(){
return $this->belongsTo('App\User');
}
public function bids(){
return $this->hasMany('App\Bid');
}
public function purchases(){
return $this->hasMany('App\Purchase');
}
}
If your products model is Product
then you have typo in your code.
Route::get ( '/', function () {
$mydatabase = Products::paginate(25);
return view ( 'search' )->withproducts($mydatabase);
} );
You have a typo in your code change products::paginate
to Product::paginate()
Hope this helps
Try replacing products::paginate
with \\App\\products::paginate
?
Laravel is namespaced, therefore it cannot find a class just by the correct name.
you must add (import your class products) in the begin of your controller (web.php for your case)
use namespace\products
replace namespace by your namespace(the path of the class products) exemple: \\app is better to use a controller ProductController and redefined yours functions
Laravel 5 promotes the use of namespaces for things like Models and Controllers. Your Model is under the App namespace, so your code needs to call it like this:
Route::get('/', function(){
$myDatabase= \App\Products::paginate(25);
});
Solved! Big Thanks guys for your Help!
I have just must added
use App\\Product;
to my web.php file and works great!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.