【问题标题】:Laravel Searchbar Class 'products' not found / ProductController@?未找到 Laravel 搜索栏类“产品”/ProductController@?
【发布时间】: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);

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



}

【问题讨论】:

  • 您必须在路线中包含产品型号。你把它包括在内了吗?你做了产品模型吗?模型名称是否与数据库表相同?
  • 分享您的模型页面?
  • App/Product.php(模型页面)已添加。

标签: php mysql laravel


【解决方案1】:

您必须在控制器的开头添加(导入您的类产品)(适用于您的案例的 web.php)

use namespace\products

用你的命名空间(类产品的路径)替换命名空间示例:\app 最好使用控制器 ProductController 并重新定义您的功能

【讨论】:

  • 在我现在添加的 web.php 文件中:使用 app\Product。但是得到一个错误,可能不正确
【解决方案2】:

如果您的产品型号是Product,那么您的代码中有错字。

Route::get ( '/', function () {
$mydatabase = Products::paginate(25);
return view ( 'search' )->withproducts($mydatabase);
} );

您的代码中有错字,将products::paginate 更改为Product::paginate()

希望对你有帮助

【讨论】:

  • 产品型号页面已添加,您可以查看一下吗?我希望它是正确的页面。
【解决方案3】:

尝试将products::paginate 替换为\App\products::paginate

Laravel 是命名空间的,因此它不能仅通过正确的名称找到一个类。

【讨论】:

    【解决方案4】:

    Laravel 5 提倡在模型和控制器等事物中使用命名空间。您的模型位于 App 命名空间下,因此您的代码需要这样调用它:

    Route::get('/', function(){

        $myDatabase= \App\Products::paginate(25);
    

    });

    【讨论】:

      【解决方案5】:

      解决了!非常感谢你们的帮助!

      我刚刚必须添加

      使用应用\产品;

      到我的 web.php 文件,效果很好!

      【讨论】:

        猜你喜欢
        • 2014-12-31
        • 2021-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-06
        • 1970-01-01
        • 2014-01-02
        相关资源
        最近更新 更多