I get the problem when I try looping the data to store in variable for making the pie chart on Laravel
this is my code in controller
// $countUser = DB::select(DB::raw("
// SELECT r.name AS name, count(u.id) AS countUser
// FROM users u, role_user ru, roles r
// WHERE u.id = ru.user_id
// AND ru.role_id = r.id
// GROUP BY name "));
$countUser = DB::table(DB::raw('users'))
->select(DB::raw('roles.name AS name, count(users.id) AS countUser'))
->join('role_user', 'users.id', '=', 'role_user.user_id')
->join('roles', 'role_user.role_id', '=', 'roles.id')
->groupByRaw('role_user.role_id, name')->get();
$data = " ";
foreach($countUser as $user){
$data = "['".$user->name."', ".$user->countUser."],";
}
dd($data);
I expect the result like this
but, I get the result only one
When I try this dd($countUser);. This is the result
How to fix it?
In your example you keep overwriting $data, instead lets use Laravel Collection methods to help you.
Map all your entries to the format [$name, $count]. Then use the Collection method implode(), to join the strings with a comma.
$concatenatedUsers = $countUser->map(function ($user) {
return '[' . $user->name . ', ' . $user->countUser ']';
})->implode(', ');
dd($concatenatedUsers);
Related
i use this query in laravel but still not work
$query = DB::table('parks')
->join('park_provinces', 'parks.province_id', '=', 'provinces.id')
->select('parks.id as id''main_image','news_time_date')
->get();
the value province_id=[34 35 36]
As the value in the province_id is not number but string with space separated ids of the provinces, you can't apply the = in this case.
Instead you need to replace the " "(space) with ,(comma) and use find_in_set method of mysql.
$query = DB::table('parks')
->join('parks', function($query){
$query->on(DB::raw('find_in_set(provinces.id, REPLACE(parks.province_id, " ", ",")'));
})
->select('parks.id as id', 'main_image','news_time_date')
->get();
I have following query that I need to achieve in Laravel eloquent:
SELECT Q.quoteid
FROM `tblquote` Q
INNER JOIN tbladdress A ON A.addressid = Q.addressid
INNER JOIN tblquotecompany QC ON QC.quoteid = Q.quoteid
INNER JOIN tblcompany C ON C.companyid = QC.companyid
WHERE
Q.useremail = 'test#test' or
(Q.ipaddress = '000.00.00.' and A.zipcode = '00000')
I have all relation set up in laravel.
I am trying to achieve this like below:
$this->eloquentQuote->newQuery()
->with(EloquentQuote::RELATION_ADDRESS)
->with(EloquentQuote::RELATION_QUOTE_COMPANIES . '.' . EloquentQuoteCompany::RELATION_COMPANY)
->whereHas(EloquentQuote::RELATION_ADDRESS,
function ($query) use ($userEmail, $userIp, $zipCode) {
/** #var Builder $query */
$query->where([
[EloquentQuote::USER_EMAIL, '=', $userEmail],
])
->orWhere([
[EloquentQuote::IP_ADDRESS, '=', $userIp],
[EloquentAddress::ZIP_CODE, '=', $zipCode],
]);
})->get();
This Eloquent query is giving expected result but taking too much time.
Is there any other way to do that efficiently?
Your help is highly regarded.
I hope the following code will be helpfull for you
$result = DB::table('tblquote')
->join('tbladdress', 'tbladdress.addressid', 'tblquote.addressid')
->join('tblquotecompany', 'tblquotecompany.quoteid', 'tblquote.quoteid')
->join('tblcompany', 'tblcompany.companyid', 'tblquotecompany.companyid')
->where('tblquote.useremail', 'test#test')
->orWhere([['tblquote.ipaddress','000.00.00.'], ['tbladdress.zipcode', '00000']])
->get();
I am trying to make the following query in laravel:
SELECT a.name AS subname, a.area_id, b.name, u. id, u.lawyer_id,u.active_search,
FROM subarea a
LEFT JOIN user_subarea u ON u.subarea_id = a.id
AND u.user_id = ?
LEFT JOIN branch b ON a.area_id = b.id
The idea is to obtain the subareas and see if the search is activated by the user.
The user_subarea table might have a record that matches the id of the subarea table where the active_search is equal to 0 or 1. If it doesn't exist I would like the query to return null.
While I was able to achieve this in raw SQL when I try the same with eloquent in Laravel I am not returning any value. I have done the following:
$query = DB::table('subarea')
->join('user_subarea', function($join)
{
$value = \Auth::user()->id;
$join->on( 'subarea.id', '=', 'user_subarea.subarea_id')->where('user_subarea.user_id', '=',$value);
})
->leftJoin('branch', 'subarea.area_id', '=', 'branch.id')
->select('branch.name', 'subarea.name as subarea', 'user_subarea.active_search_lawyer', 'user_subarea.id' )
->get();
Any help will be much appreciated.
I found by myself the answer it was just to add a lefjoin in the first join. It is not in the laravel docs but works too.
$query = DB::table('subarea')
->lefjoin('user_subarea', function($join)
{
$value = \Auth::user()->id;
$join->on( 'subarea.id', '=', 'user_subarea.subarea_id')->where('user_subarea.user_id', '=',$value);
})
->leftJoin('branch', 'subarea.area_id', '=', 'branch.id')
->select('branch.name', 'subarea.name as subarea', 'user_subarea.active_search_lawyer', 'user_subarea.id' )
->get();
Try this one, If you get a problem, please comment.
$value = \Auth::user()->id;
$query = DB::table('subarea')
->where('user_subarea.user_id', '=',$value)
->leftJoin('user_subarea', 'subarea.id', '=', 'user_subarea.subarea_id')
->leftJoin('branch', 'subarea.area_id', '=', 'branch.id')
->select('subarea.name AS subname','subarea.area_id', 'branch.name', 'user_subarea.id','user_subarea.lawyer_id','user_subarea.active_search')
->get();
I do currently have this code:
return Datatable::query($query = DB::table('acquisitions')
->where('acquisitions.deleted_at', '=', null)
->where('acquisitions.status', '!=', 2)
->join('contacts', 'acquisitions.contact_id', '=', 'contacts.id')
->join('user', 'acquisitions.user_id', '=', 'user.id')
->select('contacts.*', 'acquisitions.*', 'acquisitions.id as acquisitions_id', 'user.first_name as supervisor_first_name', 'user.last_name as supervisor_last_name', 'user.id as user_id'))
The data from the user table is used for 2 columns: acquisitions.supervisor_id and acquisitions.user_id. I need the first_name and the last_name for both of this tables, the above query does however currently only use the id from the acquisitions.user_id field. I also tried to use a table alias, that does also not work, I assume that I'm doing something wrong here.
So in short: I also need that the query selects the data for the user, based on the id from the acquisitions.supervisor_id and makes it available as supervisor_first_name and supervisor_last_name.
According to your next to last comment on the other answer, you need a self join per reference table. Try this:
$result = DB::select('SELECT
u.name user_first_name,
u.last_name user_last_name,
u.email user_email,
s.name supervisor_name,
s.last_name supervisor_last_name,
s.email supervisor_email
FROM acquisitions a
JOIN users u ON a.user_id = u.id
JOIN users s ON a.supervisor_id = s.id');
return $result;
Note that $result is an array of StdClass objects, not a Collection, but you can still iterate it and call the current item's values:
foreach ($result as $item) {
print($item->supervisor_first_name);
}
If you need a WHERE clause, e.g. to get a specific user's row from acquisitions, you would do that by adding a parameter to the query like so:
$result = DB::select('SELECT
u.name user_first_name,
u.last_name user_last_name,
u.email user_email,
s.name supervisor_name,
s.last_name supervisor_last_name,
s.email supervisor_email
FROM acquisitions a
JOIN users u ON a.user_id = u.id
JOIN users s ON a.supervisor_id = s.id
WHERE a.user_id = ?
', [3]);
EDIT
If you need the resultset to be a Collection, you can easily convert the array to one, using the hydrate method:
$userdata = \App\User::hydrate($result); // $userdata is now a collection of models
It should be something like;
return Datatable::query($query = DB::table('acquisitions')
->where('acquisitions.deleted_at', '=', null)
->where('acquisitions.status', '!=', 2)
->join('contacts', 'acquisitions.contact_id', '=', 'contacts.id')
->join('user', 'acquisitions.user_id', '=', 'user.id')
->select( \DB::raw(" contacts.*, acquisitions.*, acquisitions.id as acquisitions_id, user.first_name as supervisor_first_name, user.last_name as supervisor_last_name, user.id as user_id ") )
);
$devices = DB::table('devices as d')
->leftJoin('users as au', 'd.assigned_user_id', '=', 'au.id')
->leftJoin('users as cu', 'd.completed_by_user_id', '=', 'cu.id')
->select('d.id','au.name as assigned_user_name','cu.name as completed_by_user_name');
Also follow this link
https://github.com/yajra/laravel-datatables/issues/161
I was wondering if you could look at 3 sql queries followed by the converted JOIN queries that do not work. I keep getting an undefined index error - not sure what i'm doing wrong.
Please note that $db_table_prefix = app_ No big deal.
$db->sql_escape = mysql_real_escape_string (removed b/c laravel does it's own escaping)
1:
OLD:
$sql = "SELECT ".$db_table_prefix."Users.Group_ID,
".$db_table_prefix."Groups.*
FROM ".$db_table_prefix."Users
INNER JOIN ".$db_table_prefix."Groups ON ".$db_table_prefix."Users.Group_ID = ".$db_table_prefix."Groups.Group_ID
WHERE
User_ID = '".$db->sql_escape($this->user_id)."'";
NEW:
return (array)DB::table('app_Users')
->select('app_Users.Group_ID')
->join('app_Groups', 'app_Users.Group_ID', '=', 'app_Groups.Group_ID')
->where('User_ID', '=', $this->user_id)
->first();
2:
Old:
$sql = "SELECT ".$db_table_prefix."Users.Group_ID,
".$db_table_prefix."Groups.* FROM ".$db_table_prefix."Users
INNER JOIN ".$db_table_prefix."Groups ON ".$db_table_prefix."Users.Group_ID = ".$db_table_prefix."Groups.Group_ID
WHERE User_ID = '".$db->sql_escape($this->user_id)."'
AND
".$db_table_prefix."Users.Group_ID = '".$db->sql_escape($db->sql_escape($id))."'
LIMIT 1
";
New:
return (bool)DB::table('app_Users')
->select('app_Users.Group_ID')
->join('app_Groups', 'app_Users.Group_ID', '=', 'app_Groups.Group_ID')
->where('User_ID', '=', $this->user_id)
->where('app_Users.Group_ID', '=', $id)
->first();
3.
Old:
$sql = "SELECT ".$db_table_prefix."Users.Group_ID,
".$db_table_prefix."Groups.*
FROM ".$db_table_prefix."Users
INNER JOIN ".$db_table_prefix."Groups ON ".$db_table_prefix."Users.Group_ID = ".$db_table_prefix."Groups.Group_ID
WHERE
User_ID = '".$db->sql_escape($userId)."'";
New:
return (array) DB::table('app_Users')
->select('app_Users.Group_ID')
->join('app_Groups', 'app_Users.Group_ID', '=', 'app_Groups.Group_ID')
->where('User_ID', '=', $userId)
->first();
Thanks!