Reputation: 71
Here is my view ajax part
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);
}
});
}
Here is my controller
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);
}
here is repository
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();
}
I have two question about my code. First, when i clicked search bar button, it will show
message: "Array to string conversion", exception: "ErrorException",…}
I think that is the query problem, but i have no idea how happened.
Second, does anyone have good idea to identify the request value is null or not in the repository.php? Then, I need to push the result to the where with the laravel format.
In normally php file, we will use String combining. Like
$Where ="something=".$a
if($something!=null)
{
$Where.=",something=".$something;
}
but i have no idea how to do in the laravel language. Have anyone can help me to solve these two problem?
New question : Json output. Is it normal to show the first query one more time? I mean ....
{s_id: "ym1234", name: "one", email: "[email protected]", editDate: "2018-04-12 05:54:32",…},…]
0
:
{s_id: "ym1234", name: "one", email: "[email protected]", editDate: "2018-04-12 05:54:32",…}
Department
:
"test"
USN
:
"student"
address
:
"CS123"
editDate
:
"2018-04-12 05:54:32"
email
:
"[email protected]"
name
:
"one"
phone
:
null
s_id
:
"ym1234"
semester
:
"105"
1
:
{s_id: "ym321", name: "two", email: "[email protected]", editDate: "2018-04-07 23:53:29",…}
Department
:
"test"
USN
:
"student"
address
:
"CS123"
editDate
:
"2018-04-07 23:53:29"
email
:
"[email protected]"
name
:
"two"
phone
:
null
s_id
:
"ym321"
semester
:
"106"}]
Upvotes: 2
Views: 186
Reputation: 3579
I think you need to combine all condition in one array for where condition like where([['r_id','=','1'],['s_id','like',$s_id],['name','like',$name],['department','like',$department],['semester','like',$semester]])
And second is you have error in your client side code and may generate same error - because you are going to alert the object that is in json format. You specified dataType : json
means the response is converted in JSON format. If you alert your JSON object then the error is obvious.
Now, second question is -
This is what I am using in my code snippet - supposed you should have following code inside getSearchdata
function
$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();
Let me know, whether it works for you or not...
Upvotes: 2
Reputation: 3150
I think your getSearchdata
function is incorrect:
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();
}
Upvotes: 0
Reputation: 610
I think you forgot to add [ ]
in your where condition
Change your function to
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();
}
Upvotes: 1