gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:27:25 25 4
gpt4 key购买 nike

我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com