【发布时间】:2018-02-13 11:43:18
【问题描述】:
我是 Laravel 初学者,现在尝试在我的网站上构建一个简单的搜索栏。
但我得到这个错误:
找不到类“产品”
谁能告诉我我在控制器中忘记了什么?
索引上的搜索表单:
<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 ( '/', 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');
}
}
【问题讨论】:
-
您必须在路线中包含产品型号。你把它包括在内了吗?你做了产品模型吗?模型名称是否与数据库表相同?
-
分享您的模型页面?
-
App/Product.php(模型页面)已添加。