php - Laravel Searchbar 类 'products' 未找到/ProductController@?

标签 php mysql laravel

我是 Laravel 初学者,现在尝试在我的网站上构建一个简单的搜索栏。

但我收到此错误:

Class 'products' not found

有人可以告诉我我在 Controller 中忘记了什么吗?

索引搜索表单:

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

productsProduct::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');
    }



}

最佳答案

您必须在 Controller 的开头添加(导入您的类(class)产品)(针对您的情况的 web.php)

use namespace\products

将命名空间替换为您的命名空间(类产品的路径)例如:\app 最好使用 Controller ProductController 并重新定义您的功能

关于php - Laravel Searchbar 类 'products' 未找到/ProductController@?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46050547/

相关文章:

MySQL给用户添加权限。现在用户无法访问数据库

php - 如何使用我的注册帐户在 php 中输入登录页面

mysql - 将邮件同步到 MySQL

php - 依赖注入(inject)如何知道要传递哪些变量?

php - 如何用 wamp 运行 Glassfish 服务器?

javascript - Dropzone 在本地主机上正常工作,在远程服务器上抛出错误

php - Laravel DataTables Yajrabox 不显示数据

html - 使用 Laravel 在表内 Bootstrap 折叠

php - MySQL 使用 mysqli_real_escape_string 转义单引号

php - 将 id 从一个表格带到另一个表格并插入另一个表格