我想在搜索表单中保留条件

时间:2019-09-10 05:25:31

标签: php laravel laravel-5.8

我想在搜索时保留搜索条件

我写了

project.index

<form action="{{ route('project.post.index') }}" method="post">
@csrf
<input type="search" name="project_name" placeholder="project_name"
@if($param->project_name != null)
    value="{{$param->project_name}}"
@else
    value="{!!null!!}"
@endif
>

项目控制器

$relevantKeys = $param = [
        'project_name' => null,
        'requester' => null,
        'user_name' => null,
        'status' => null,
        'requester_tell' => null,
        'division' => null,
    ];
foreach ($relevantKeys as $value) {
        if (!empty($request->{$value})) {
            $query = $query->where($value, 'like', '%' . $request->{$value} . '%');
            $param = $request->{$value};
            $a = 1;
        }
    }
    if ($a === 0) {
        $view = Project::where('status', '!=', 'completed')->get();
        return view('project.index', compact('view', 'cats', 'users', 'param'));
    }
    $view = $query->get();
    return view('project.index', compact('view', 'cats', 'users', 'param'));
}

我遇到此错误

试图获取非对象的属性“ project_name”

2 个答案:

答案 0 :(得分:2)

您可以尝试!空,或者您不需要调用其他部分。如果“如果” 部分未执行,它将自动为空。

<input type="search" name="project_name" placeholder="project_name" value="@if(!empty($param->project_name)) {{$param->project_name}} @endif">

或者您也可以使用三元运算符

<input type="search" name="project_name" placeholder="project_name" value="{{ ($param->project_name) ? $param->project_name : '' }}">

<input type="search" name="project_name" placeholder="project_name" value="{{  $param['project_name'] ?? '' }}">

答案 1 :(得分:0)

这是因为您正在发送

  

$ param-> project_name为null

尝试在该变量中发送其他数据,例如“-”,或忽略该变量以继续。