1

下面的代码包括 roop 语句和分页。我也希望遵循 MVC 样式编码,但在这种情况下,我必须在下面的视图中使用 @if 语句。

当我尝试按@if(!deleted_at == null) 过滤时出现错误,但我无法显示正确的分页,因为这段代码也用“(deleted_at == null)”计数,所以结果是不正确的分页数显示。

通过此控制器后,如何修改此代码以获得正确的分页结果?

我认为错误来自将 Where() 与 Orwhere() 一起使用。
谢谢:)

按 user_name 和 customer_name 搜索框

在此处输入图像描述

索引.php

 <div style="overflow-x: auto; display: inline-flex;">
   {!! Form::open(array('class' => 'form', 'method' => 'get', 'url' => url('/stopjobs/job_finished_search_date'))) !!} 
   {{ Form::input('text', 'datepicker_from', null, ['placeholder' => 'Fra', 'id' => 'datepicker_from']) }}
    &nbsp;
   {{ Form::input('text', 'datepicker_to', null, ['placeholder' => 'Til', 'id' => 'datepicker_to']) }} 
        {!! Form::submit('Søke', ['class' => 'btn btn-success btn-sm']) !!}
   {!! Form::close() !!}
    &nbsp;
   {!! Form::open(array('class' => 'form', 'method' => 'get', 'url' => url('/stopjobs/job_finished_search_name'))) !!}
   {!! Form::text('name', null, ['placeholder' => 'Bruker eller Kunde']) !!}
   {!! Form::submit('Søke', ['class' => 'btn btn-success btn-sm']) !!} 
   {!! Form::close() !!}
 </div>

控制器

public function job_finished_search_name(Request $request) 
{ 
    $jobs = Job::onlyTrashed()
    ->where('user_name', 'like', '%'.$request->name.'%')
    ->orwhere('customer_name', 'like', '%'.$request->name.'%')
    ->orderBy('deleted_at', 'desc')
    ->paginate(15);

    return view('stopjobs.index', ['jobs' => $jobs]); 
}

看法

<tbody>
@foreach($jobs as $job)
    @if(!$job->deleted_at == null)
    <tr>
        <td>{!! $job->id !!}</td>
        <td>{!! $job->user_name !!}</td>
        <td>{!! $job->created_at !!}</td>
        <td>{!! $job->deleted_at !!}</td>
        <td>{!! $job->usedTime() !!}</td>
    </tr>
    @endif
@endforeach
</tbody>
</table>
</div>
{{ $jobs->links()}}
4

2 回答 2

0

where当你查询你的工作时,用语句来做。没有别的办法

$jobs = Job::whereNull('deleted_at')->get();
于 2017-11-02T09:27:14.600 回答
0

如果您只想显示带有分页的软删除数据,请尝试此操作

$jobs = Job::onlyTrashed()
                ->where('key', 'value')
                ->paginate();
于 2017-11-02T09:33:32.497 回答