“未定义的变量”搜索功能

时间:2019-10-18 09:39:48

标签: twitter-bootstrap laravel-5.8

我正在尝试为从数据库中获取并保存数据的表设置搜索功能。目前,我的表可以查看数据库中的数据,但无法搜索。 下面是我的显示数据的表

<table class="table">
                    <thead class=" text-primary">
                        <th>
                            Brand Name
                        </th>
                        <th>
                            Phone
                        </th>
                        <th>
                            Reg No.
                        </th>
                        <th>
                            Certificate
                        </th>
                        <th>
                            Email
                        </th>
                        <th>
                            User-Role
                        </th>
                        <th>
                            Edit
                        </th>
                        <th>
                            Delete
                        </th>
                    </thead>
                    <tbody>
                        @foreach($user as $receiver)
                        <tr>
                            <td>
                                {{$receiver->name}}
                            </td>
                            <td>
                                {{$receiver->phone}}
                            </td>
                            <td>
                                {{$receiver->regno}}
                            </td>
                            <td>
                                {{$receiver->certificate}}
                            </td>
                            <td>
                                {{$receiver->email}}
                            </td>
                            <td>
                                {{$receiver->usertype}}
                            </td>
                            <td>
                                <a href="/org-edit/{{$receiver->id}}" class="btn btn-success">EDIT</a>
                            </td>
                            <td>
                                <form class = "delete" action="org-delete/{{$receiver->id}}" method="post">
                                    {{csrf_field()}}
                                    {{method_field('DELETE')}}
                                    <button class="delete btn btn-danger" type="submit">Delete</button>
                                </form>
                            </td>
                        </tr>
                        @endforeach

                        <script>
                            $(".delete").on("submit", function(){
                                return confirm("Are you sure?");
                            });
                        </script>
                    </tbody>
                </table>

还有我用来搜索查询的表格。

<form action="{{ url('searchorg')}}" method="post">
{{csrf_field()}}
{{method_field('PUT')}}
<div class="input-group no-border">
    <input type="text" name="q1" class="form-control" placeholder="Search...">
    <div class="input-group-append">
        <div class="input-group-text">
            <i class="now-ui-icons ui-1_zoom-bold"></i>
        </div>
    </div>
</div>

控制器“ OrganizationController.php”

    public function search(Request $request)
{
   if($q = Input::get ( 'q1' )){
    $user1 = User::where('name','LIKE','%'.$q1.'%')->orWhere('email','LIKE','%'.$q1.'%')->orWhere('phone','LIKE','%'.$q1.'%')->orWhere('usertype','LIKE','%'.$q1.'%')->orWhere('regno','LIKE','%'.$q1.'%')->orWhere('lat','LIKE','%'.$q1.'%')->orWhere('long','LIKE','%'.$q1.'%')->get();
    if(count($user1) > 0)
        return view('admin.register')->withDetails('user',$user1)->withQuery ($q);
    else return view ('admin.register')->withMessage('No Details found. Try to search again !');
}else{ 
    $user = User::where('usertype', 'organization')->get();
    return view('admin/organization.organizations')->with('user',$user);
}
}

和路线

Route::any('/searchorg','Admin\OrganizationController@search');

每当我在搜索表单上搜索某些内容时,都会出现错误“未定义的变量:q1”。

我将不胜感激。谢谢

1 个答案:

答案 0 :(得分:0)

您应该在控制器中进行更改:

public function search(Request $request)
{
    $q1 = Input::get( 'q1' );

    if( !empty( $q1 ) ) {

        $user1 = User::where('name','LIKE','%'.$q1.'%')->orWhere('email','LIKE','%'.$q1.'%')->orWhere('phone','LIKE','%'.$q1.'%')->orWhere('usertype','LIKE','%'.$q1.'%')->orWhere('regno','LIKE','%'.$q1.'%')->orWhere('lat','LIKE','%'.$q1.'%')->orWhere('long','LIKE','%'.$q1.'%')->get();
        if(count($user1) > 0){
            return view('admin.register')->withDetails('user',$user1);
        } else { 
            return view ('admin.register')->withMessage('No Details found. Try to search again !');
        }
    } else { 
        $user = User::where('usertype', 'organization')->get();
        return view('admin/organization.organizations')->with('user',$user);
    }
}