Laravel 加上 Ajax 实现搜索栏

Laravel plus Ajax to implement search bar

这是我的观点ajax部分

function getSearch(){
            var sr_s_id= $('#sr_s_id').val();
            var sr_name= $('#sr_name').val();
            var sr_department= $('#sr_department').val();
            var sr_semester= $('#sr_semester').val();
            var request = new XMLHttpRequest();
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $.ajax({
                type    :"POST",
                url     :"{{url('/management/sutdent')}}",
                dataType:"json",
                data    :{ s_id:sr_s_id,name:sr_name,department:sr_department,semester:sr_semester },
                success :function(response) {
                    alert(response);
                },
                error: function(response) {
                    alert(response);
                }
            });

        }

这是我的控制器

public function getSearch(Request $request){
        $s_id =$request->s_id;
        $name =$request->name;
        $department = $request->department;
        $semester =$request->semester;
        $student_list=$this->UserRepository->getSearchdata($s_id,$name,$department,$semester);  
        //echo $student_list;

        return response()->json($student_list);

    }

这里是存储库

public function getSearchdata($s_id=null,$name=null,$department=null,$semester=null){


        return $this->user
            ->select('s_id','name','email','user_info.updated_at as editDate','semester','USN','phone','address','Department')
            ->leftjoin('user_info','s_id','user_info.school_id')
            ->where(['r_id','=','1'],['s_id','like',$s_id],['name','like',$name],['department','like',$department],['semester','like',$semester])
            ->orderBy('s_id','asc')->get();
    }

我有两个关于我的代码的问题。 首先,当我点击搜索栏按钮时,它会显示

message: "Array to string conversion", exception: "ErrorException",…}

我认为这是查询问题,但我不知道怎么回事。

其次,有没有人知道在 repository.php 中识别请求值是否为空?然后,我需要将结果以 laravel 格式推送到 where 。

通常在php文件中,我们会使用String组合。喜欢

$Where ="something=".$a

if($something!=null)
{
    $Where.=",something=".$something;
}

但我不知道如何使用 laravel 语言。 有谁能帮我解决这两个问题吗?


新问题:Json 输出。 再次显示第一个查询是否正常? 我是说....

{s_id: "ym1234", name: "one", email: "test@test.com", editDate: "2018-04-12 05:54:32",…},…]
0
:
{s_id: "ym1234", name: "one", email: "test@test.com", editDate: "2018-04-12 05:54:32",…}
Department
:
"test"
USN
:
"student"
address
:
"CS123"
editDate
:
"2018-04-12 05:54:32"
email
:
"test@test.com"
name
:
"one"
phone
:
null
s_id
:
"ym1234"
semester
:
"105"
1
:
{s_id: "ym321", name: "two", email: "test@test.com", editDate: "2018-04-07 23:53:29",…}
Department
:
"test"
USN
:
"student"
address
:
"CS123"
editDate
:
"2018-04-07 23:53:29"
email
:
"test@test.com"
name
:
"two"
phone
:
null
s_id
:
"ym321"
semester
:
"106"}]

我想你忘了在你的 where 条件中添加 [ ] 将您的功能更改为

public function getSearchdata($s_id=null,$name=null,$department=null,$semester=null){


    return $this->user
        ->select('s_id','name','email','user_info.updated_at as editDate','semester','USN','phone','address','Department')
        ->leftjoin('user_info','s_id','user_info.school_id')
        ->where([['r_id','=','1'],['s_id','like',$s_id],['name','like',$name],['department','like',$department],['semester','like',$semester]])
        ->orderBy('s_id','asc')->get();
}

我认为您需要将所有条件合并到一个数组中,以便像 where([['r_id','=','1'],['s_id','like',$s_id],['name','like',$name],['department','like',$department],['semester','like',$semester]])

这样的条件

其次是您的客户端代码有错误,并且可能会产生相同的错误 - 因为您要提醒 json 格式的对象。您指定 dataType : json 表示响应以 JSON 格式转换。如果您提醒 JSON 对象,那么错误就很明显了。

现在,第二个问题是-

这就是我在我的代码片段中使用的内容 - 假设您应该在 getSearchdata 函数中包含以下代码

$query = $this->user
         ->select('s_id','name','email','user_info.updated_at as editDate','semester','USN','phone','address','Department')
        ->leftjoin('user_info','s_id','user_info.school_id')
        ->where('r_id','=','1');

if($s_id)
{
   $query->where('s_id','=',$s_id);
}
if($name)
{
   $query->where('name','like','%'.$name.'%');
}
if($department)
{
   $query->where('department','like','%'.$department.'%');
}
if($semester)
{
   $query->where('semester','like','%'.$semester.'%');
}
return $query->orderBy('s_id','asc')->get();

让我知道它是否适合你...

我认为你的 getSearchdata 函数不正确:

    public function getSearchdata($s_id = null, $name = null, $department = null, $semester = null)
    {

        return $this->user
            ->select('s_id', 'name', 'email', 'user_info.updated_at as editDate', 'semester', 'USN', 'phone', 'address', 'Department')
            ->leftJoin('user_info', 's_id', 'user_info.school_id')
            ->where('r_id', '=', '1')
            ->where('s_id', 'like', $s_id)
            ->where('name', 'like', $name)
            ->where('department', 'like', $department)
            ->where('semester', 'like', $semester)
            ->orderBy('s_id', 'asc')->get();

   }