I have the ff code :
$query = $this->db->query("SELECT fname AS firstname, lname AS lastname FROM table");
$query_fields = $query->list_fields();
Now the value of $query_fields will be :
array(2) {
[0]=> "firstname",
[1]=> "lastname"
}
Is there a way or solution that i can get the fname and lname?
My prefer output would be:
array(2) {
['fname']=> "firstname",
['lname']=> "lastname"
}
Any solution or comment?
Thanks
$query = $this->db->query("SELECT fname AS firstname, lname AS lastname FROM table");
to
$query = $this->db->query("SELECT fname , lname FROM table");
You will have fname and lname instead of firstname and lastname.
You can iterate over the result set to make your own array.
Related
I have this technical difficulty when want to use laravel yajra datatable to write custom global search query
Reference
https://yajrabox.com/docs/laravel-datatables/master/filter-column
when I do some search by passing in calendar date and searchbox value, I'm stuck at correct way to write it
Only the main ->filter got triggered
The ->filterColumn skipped through.... not sure why...
In sql vanila, it is
where firstname like %search% OR
lastname like %search% OR
fullname like %search% OR
email like %search% OR
sponsorby like %search% OR
createddate like %date%
Here is my code for reference. filterCustom is not triggered...
if ($request->ajax()) {
$users = \DB::select('
SELECT id, usercode as membercode, concat(firstname, " ", lastname) as fullname, email, created_at as createddate, (select concat(firstname, " ", lastname) from users where id= m.parentid) sponsorby
FROM users m
ORDER BY m.created_at ASC');
Update: I realize yajra doesn't support DB::select with full raw sql, however it support DB::table
So I change my sql to below,
$users = \DB::table('users')
->select('id', 'usercode as membercode', \DB::raw('concat(firstname, " ", lastname) as fullname'), 'email', 'created_at as createddate', \DB::raw('(select concat(firstname, " ", lastname) from users as b where b.id= users.parentid) as sponsorby'));
//end
return Datatables::of($users)
->addColumn('membercode', function($row){
$content= $row->membercode;
return $content;
})
->addColumn('fullname', function($row){
$content= $row->fullname;
return $content;
})
->addColumn('email', function($row){
$content= $row->email;
return $content;
})
//etc
->filter(function ($sql) use ($request) {
$search = $request->search;
if (!empty($search))
{
$sql->Where('email', 'LIKE', "%$search%");
}
})
->filterColumn('fullname', function($query, $request) {
$search = $request->search;
$sql = 'concat(firstname, " ", lastname) like ?';
$query->whereRaw($sql, ["%{$search}%"]);
})
->filterColumn('membercode', function($query, $request) {
$search = $request->search;
$sql = 'usercode as membercode like ?';
$query->whereRaw($sql, ["%{$search}%"]);
})
->filterColumn('createddate', function($query, $request) {
$search = $request->date;
$sql = 'created_at as createddate like ?';
$query->whereRaw($sql, ["%{$search}%"]);
})
->filterColumn('sponsorby', function($query, $request) {
$search = $request->search;
$sql = '(select concat(firstname, " ", lastname) from users as b where b.id= users.parentid) like ?';
$query->whereRaw($sql, ["%{$search}%"]);
})
->make(true);
}
I m new in laravel
Actually I have a auth table which is link to the other table based on the user type. I want user info from corresponding tables so i use raw query now i want to convert it into query builder, Please help
$data = DB::select("SELECT srr.id,srr.created_at,srr.fromid,srr.toid,srr.from_usertype,au.firstname_admin,au.lastname_admin,cd.name as to_compayname,COALESCE(unionSub1.firstname,NULL) as from_firstname, unionSub1.lastname as from_lastname
from service_request_reviews as srr
left join (
(select authid, firstname, lastname from userdetails)
union (select authid, firstname, lastname from yachtdetail)
union (select authid, firstname, lastname from talentdetails)
union (select authid, name as firstname, COALESCE(NULL,NULL) as lastname from companydetails)
) unionSub1 on unionSub1.authid = srr.fromid
left join auths as au on au.id = srr.fromid
LEFT JOIN companydetails as cd ON cd.authid = srr.toid WHERE srr.isdeleted = '0' AND srr.parent_id1 = '0' " );
I have tried this and its working fine without union. I don't know how to use multiple unions inside left join.
$data = DB::table('service_request_reviews as srr')
->select('srr.id','srr.created_at','srr.fromid','srr.toid','srr.from_usertype','au.firstname_admin','au.lastname_admin','cd.name as to_compayname')
->leftjoin('auths as au', 'au.id', '=' ,'srr.fromid')
->leftjoin('companydetails as cd', 'cd.authid', '=', 'srr.toid')
->where('srr.isdeleted', '0')
->where('srr.parent_id', '0');
You can define union as query builder for that table Like:
$yachtdetail = DB::table("yachtdetail")
->select('authid', 'firstname', 'lastname');
$talentdetails = DB::table('talentdetails')
->select('authid', 'firstname', 'lastname');
Now you can use like:
$data = DB::table('service_request_reviews as srr')
->select('srr.id','srr.created_at','srr.fromid','srr.toid','srr.from_usertype','au.firstname_admin','au.lastname_admin','cd.name as to_compayname')
->leftjoin('auths as au', 'au.id', '=' ,'srr.fromid')
->leftjoin('companydetails as cd', 'cd.authid', '=', 'srr.toid')
->where('srr.isdeleted', '0')
->where('srr.parent_id', '0')
->union($yachtdetail)
->union($talentdetails)
->get();
Here is the link for the documentation. https://laravel.com/docs/5.8/queries#unions
Edited:
In your case you can try something like this:
$queryBuilder = DB::table('service_request_reviews as srr')
->select('srr.id','srr.created_at','srr.fromid','srr.toid','srr.from_usertype','au.firstname_admin','au.lastname_admin','cd.name as to_compayname')
->leftjoin('auths as au', 'au.id', '=' ,'srr.fromid')
->leftjoin('companydetails as cd', 'cd.authid', '=', 'srr.toid')
->leftjoin(DB::raw("((select authid, firstname, lastname from userdetails)
union (select authid, firstname, lastname from yachtdetail)
union (select authid, firstname, lastname from talentdetails)
union (select authid, name as firstname, null as lastname from companydetails)) as unionSub1"), function($join){
$join->on(DB::raw('unionSub1.authid'), '=', DB::raw('srr.fromid'));
})
->where('srr.isdeleted', '0')
->where('srr.parent_id', '0');
$data = $queryBuilder->get();
There are first_name, mid_name and last_name fields in my model. And I am receiving 2 fields (name, last_name) with POST request. I am sure last_name will be there for sure, but I want to be able to compare first_name or mid_name with name.
The issue here is, I don't want to repeat myself but I couldn't figure out a cleaner way.
$query = User::where('last_name', $request->last_name);
if ($request->name) {
$query = $query->where('first_name', $request->name);
}
$result = $query->get();
If I chain, ->orWhere('mid_name', $request->name) inside the if statement, I will end up repeating myself for rechecking for last_name.
I tried dividing this line:
$query = User::where('last_name', $request->last_name);
to this but it doesn't find even the last_name (probably because I am accessing the class?):
$query = User::class;
$lastnameQuery = $query->where('last_name', $request->last_name);
How would you approach a scenario like this to achieve it in a clean way?
Using a orWhere it could be done easily, for example:
$query = User::where('last_name', $request->last_name);
if ($request->name) {
$query = $query->where(function($query) use ($request) {
$query->where('first_name', $request->name)
->orWhere('mid_name', $request->name);
});
}
$result = $query->get();
The closure is required here to separately apply the orWhere only on the first_name and mid_name, so the pseudo query would be something like this:
WHERE last_name = 'doe' AND (first_name = 'john' OR mid_name = 'john')
My view page has the following fields
id,name,email,gender etc..
I have a search box , and i want to perform search in all the above fields
How to write the query string in CI way
Controller
public function search()
{
$sql = "SELECT * FROM users";
$this->form_validation->set_rules('search_term', 'Search_term', 'trim|xss_clean');
if ($this->form_validation->run() == TRUE)
{
$keyword = $this->input->post('search_term');
$sql.= "WHERE name like '{$keyword }'";
$sql.= "OR email like '{$keyword }'";
$sql.= "OR gender like '{$keyword }'";
}
$query = mysql_query($sql);
}
read CI Like like query
$keyword = $this->input->post('search_term');
$this->db->select('*');
$this->db->like('name', $keyword);
$this->db->or_like('email', $keyword);
$this->db->or_like('gender', $keyword);
$res = $this->db->get('users');
I have form search contains:
customer id customer name email postal
when user fill for example customer name like "john" and email like "hotmail"
I want to search on customer table where custumer_name like "%john%" and email like "%hotmail%".
but when fill customer_name,email and postal
select * from customers where custumer_name like "%john%" and email like "%hotmail%" and postal = 123
what I tried:
if ($name != "") Customer::with('province')->with('country')->with('user')->where("customer_name", "like", "%$name%")->get();
if ($name != "" and $email != "")
Customer::with('province')->with('country')->with('user')->where("customer_name", "like", "%$name%")->where("email", "like", "%$email%")->get();
.
.
.
and so on check all cases but this not efficient solution because if the fields increase i will more IFs.
what is the best solution?
using advanced wheres
if (!empty($customer_id)) {
$customerCollection = $customerCollection->where("id","like", "%$customer_id%");
}
if (!empty($name)) {
$customerCollection = $customerCollection->where(function($q) use ($name){$q->where("first_name", "like", "%$name%")
->orWhere("middle_name", "like", "%$name%")
->orWhere("last_name", "like", "%$name%");});
}