我是Laravel的初学者,现在尝试在我的网站上构建一个简单的搜索栏。
但是我收到了这个错误:
Class'产品'找不到
有人可以告诉我,我在控制器中忘记了什么吗?
在索引上搜索表单
<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 !' );
} );
错误来自:
Route :: get(&#39; /&#39;,function(){ $ mydatabase = products :: paginate(25);
如何products
或Product::paginate
定义或必须在web.php
ProductController@...
使用?是的,我发现它不是我的数据库表products
;)我认为Product
而不是products
是正确的,对吗?
/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');
}
}
答案 0 :(得分:0)
如果您的产品型号为Product
,那么您的代码中就会出现拼写错误。
Route::get ( '/', function () {
$mydatabase = Products::paginate(25);
return view ( 'search' )->withproducts($mydatabase);
} );
您的代码更改为products::paginate
至Product::paginate()
希望这有帮助
答案 1 :(得分:0)
尝试将products::paginate
替换为\App\products::paginate
?
Laravel是命名空间,因此无法通过正确的名称找到类。
答案 2 :(得分:0)
您必须在控制器的开头添加(导入您的类产品)(适用于您的情况的web.php)
Object_id | term-taxonomy_id
20145 | 113
20145 | 132
20651 | 113
20365 | 114
用命名空间(类产品的路径)替换命名空间例如:\ app 最好使用控制器ProductController并重新定义你的函数
答案 3 :(得分:0)
Laravel 5促进了模型和控制器等名称空间的使用。您的模型位于App命名空间下,因此您的代码需要像下面这样调用它:
Route :: get('/',function(){
$myDatabase= \App\Products::paginate(25);
});
答案 4 :(得分:0)
解决!非常感谢你的帮助!
我必须添加
使用App \ Product;
到我的web.php文件,效果很好!