【问题标题】:Method Illuminate\Support\Collection::orWhere does not exist方法 Illuminate\Support\Collection::orWhere 不存在
【发布时间】:2020-11-20 11:33:19
【问题描述】:

我正在尝试连接 2 个表并在其中搜索并在视图中的表中显示它们的结果。 我在这条线上有这个错误:$query->orWhere($field, 'like', '%'.$search.'%');

在我的控制器的这个搜索方法中:

   public function search3(Request $request)
    {
    $query = DB::table('users')
            ->join('proforms', 'users.id', '=', 'proforms.user_id')
            ->get();
        
    $search = $request->get('search');
    $requestData = ['showname'];
        
    /* $query = Proform::query(); */
    foreach ($requestData as $field){
    $query->orWhere($field, 'like', '%'.$search.'%');
    }
    $data2=$query->paginate(5);
    return view('proforms.index', ['proforms' => $data2])->with('i', ($request->input('page', 1) - 1) * 5);
    }

单击此视图上的搜索按钮后出现此错误:

@extends('layouts.app')


@section('content')
    <div class="row">
        <div class="col-lg-12 margin-tb">
            <div class="pull-left">
                <h2>Zarządzanie proformami</h2>
            </div>


 <div class="col-md-4">
<form action="/search3" method="get">
<div class="input-group">
<input type="search" name="search" class="form-control">
<span class="input-group-prepend">
<button type="submit" class="btn btn-primary">Wyszukaj</button>
</span>
</div>
</form>
</div>           

            <div class="pull-right">
                @can('product-create')
                <a class="btn btn-success" href="{{ route('proforms.create') }}"> Utwórz nową proformę</a>
                @endcan
            </div>
        </div>
    </div>


    @if ($message = Session::get('success'))
        <div class="alert alert-success">
            <p>{{ $message }}</p>
        </div>
    @endif


    <table class="table table-bordered">
        <tr>

            <th scope="col">@sortablelink('id', 'Numer')</th>
            <th scope="col">@sortablelink('proformnumber', 'Numer proformy')</th>
            <th scope="col">@sortablelink('user_id', 'Kontrachent')</th>
            <th scope="col">@sortablelink('proformdate', 'Data wystawienia')</th>
            <th scope="col">@sortablelink('selldate', 'Data sprzedaży')</th>
            <th scope="col">@sortablelink('paymentdate', 'Termin płatności')</th> 
            <th scope="col">@sortablelink('status', 'Status')</th> 
            <th scope="col">@sortablelink('nettotal', 'Netto razem')</th>
            <th scope="col">@sortablelink('grosstotal', 'Brutto razem')</th>              
            <th width="280px">Akcja</th>
        </tr>
        @foreach ($proforms as $proform)
        <tr>
            <td>{{ ++$i }}</td>
            <td>{{ $proform->proformnumber }}</td>
            <td>{{ $proform->user->showname }}</td>
            <td>{{ $proform->proformdate }}</td>
            <td>{{ $proform->selldate }}</td>
            <td>{{ $proform->paymentdate }}</td>
            <td>{{ $proform->status }}</td>
            <td>{{ $proform->nettotal }}</td>
            <td>{{ $proform->grosstotal }}</td>

            <td>
                <form action="{{ route('proforms.destroy',$proform->id) }}" method="POST">
                    <a class="btn btn-info" href="{{ route('proforms.show',$proform->id) }}">Więcej</a>
                    @can('product-edit')
                    <a class="btn btn-primary" href="{{ route('proforms.edit',$proform->id) }}">Edytu</a>
                    @endcan


                    @csrf
                    @method('DELETE')
                    @can('product-delete')
                    <button type="submit" class="btn btn-danger">Usuń</button>
                    @endcan
                </form>
            </td>
        </tr>
        @endforeach
    </table>

{!! $proforms ?? ''->appends(request()->except('page'))->render() !!}
 


<p class="text-center text-primary"><small>ARTplus 2020</small></p>
@endsection

这是我的路线:

Auth::routes();
   
Route::get('/home', 'HomeController@index')->name('home');
   
Route::group(['middleware' => ['auth']], function () {
Route::resource('roles', 'RoleController');
Route::resource('users', 'UserController');
Route::resource('permissions', 'PermissionController');
Route::resource('products', 'ProductController');
Route::resource('invoices', 'InvoiceController');

Route::resource('category', 'CategoryController');
Route::resource('invoices', 'InvoiceController');
Route::resource('proforms', 'ProformController');
Route::get('/search', 'UserController@search');
Route::get('/search2', 'ProductController@search2');
Route::get('/search3', 'ProformController@search3');
Route::get('data', 'UserController@index');
Route::get('posts', 'PostController@index');
Route::get('/prodview', 'TestController@prodfunct');

【问题讨论】:

  • 完成-&gt;get() 后,您将无法对其使用 Eloquent 查询。它们需要在获得结果之前应用于查询。

标签: php sql laravel eloquent frameworks


【解决方案1】:

改变

$query = DB::table('users')
        ->join('proforms', 'users.id', '=', 'proforms.user_id')
        ->get();

到

$query = DB::table('users')
        ->join('proforms', 'users.id', '=', 'proforms.user_id');

当您在查询上调用 get() 方法时,查询将被执行并返回 Collection 实例而不是构建器实例。错误是因为 Collection 类没有 orWhere 方法。 Builder 有这个方法。所以查询完成后调用get()方法。

【讨论】:

  • 这是否解决了您的问题?
猜你喜欢
  • 2019-05-24
  • 2022-01-22
  • 2020-06-09
  • 2021-04-27
  • 2019-05-25
  • 2020-08-16
  • 2020-08-29
  • 2021-05-27
  • 2021-09-27
相关资源
最近更新 更多