这是我的观点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中确定请求值是否为null?然后,我需要将结果推送到laravel格式的位置。
在正常的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"}]
答案 0 :(得分:2)
我认为您需要将一个数组中的所有条件组合到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();
让我知道,它是否适合你...
答案 1 :(得分:1)
我想你忘了在你的地方添加[ ]
将您的功能更改为
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();
}
答案 2 :(得分:0)
我认为您的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();
}