method\support\collection::orwhere不存在

tyg4sfes  于 2021-07-27  发布在  Java
关注(0)|答案(1)|浏览(366)

我正在尝试连接两个表,并在其中搜索,并显示从他们的结果在表上的看法。我在这条线上有个错误: $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');
dbf7pr2w

dbf7pr2w1#

改变

$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 示例而不是生成器示例。错误是因为集合类没有 orWhere 方法。这个 Builder 有那个方法。因此,在查询完成时调用get()方法。

相关问题