I am new to Laravel and finding it a bit hard to learn it. I am having trouble in writing an SQL query that involves joining between multiple tables. I viewed the documentations but it did not understand how to write it.
This is my raw sql query which I want to write in Laravel style:
SELECT
cd.`company_details`,cd.`company_id`,cd.`company_logo`,cd.`company_name`,
cd.`company_type_id`,cd.`company_website`,cd.`login_email`,cd.`phone_number, ld.`date_created`,ld.`is_active`,ld.`login_password`,ld.`login_type`
FROM `company_details` AS cd
JOIN `login_details` AS ld
ON cd.`login_email`=ld.`login_email`
WHERE cd.`login_email`=$login_email
AND cd.`company_id`=$company_id
AND cd.`company_name`=$company_name
AND ld.`login_type`='COMPANY'
I know the basic syntac like DB::tablename()->select()->where()->get() but can't write the query. Please Help.
DB::table('company_details AS cd')
->join('company_details AS ld', 'ld.login_email', '=', 'cd.login_email')
->where('cd.login_email', $login_email)
->where('cd.company_id', $company_id)
->where('cd.company_name', $company_name)
->where('ld.login_type', 'COMPANY')
->select('cd.company_details, cd.company_id, cd.company_logo, cd.company_name, cd.company_type_id, cd.company_website, cd.login_email, cd.phone_number')
->get();
For joining multiple tables read this documentation: https://laravel.com/docs/5.4/queries#joins
Related
I am using lumen with oracle database and getting error on query with
group by.not undestand the problem because its working with mysql.
"ora 00979 not a group by expression"
my query :
DB::table('company')
->select('company.name','branch.id','branch.name','branch.id','branch.branch_com_id')
->leftjoin('branch', 'company.id', '=', 'branch_com_id')
->groupBy('branch.branch_com_id')
->get()
->toArray();
I am trying to groupby rows from branch table with branch_com_id but its showing above error.
i tried with adding strict mode => false in .env and database.php but still its not working for me.
Given your code, what Oracle gets is a query like
select company.name, branch.id, branch.name, branch.id, branch.branch_com_id
from ...
group By branch.branch_com_id
which clearly gives the issue.
If you need to extract company.name, branch.id, branch.name, branch.id, you need to use them in the group by clause; differently, you may need to use aggregation functions (like sum, min, ..) over this columns, but this depends on the logic you need
Here my MySQL query (work in phpMyAdmin) :
SELECT workcenter, (SUM(w1+w2 +w3 +w4)/ (COUNT(DISTINCT(teknisi))*40*4) )*100 AS total FROM `team` GROUP by workcenter ORDER BY total
then, i try in Laravel Sintax like this below (not work) :
$sql = Team::groupBy('workcenter')->select('workcenter', \DB::raw('(SUM(w1+w2+w3+w4)/ (COUNT(DISTINCT(teknisi))*30*4) )*100 AS total'))
->OrderBy('total', 'Desc')
->get();
When i run the laravel sintax, its doesn't show any errors, but the output is nothing..
Please anyone help me to convert the MySQL query to Laravel Sintax. Thank you!
I think you are close enough, however, this doesn't look like a correct way to group by with Eloquent ORM. Try using raw expressions, something like this might work:
$sql = DB::table('team')
->select(DB::raw('workcenter, (SUM(w1+w2 +w3 +w4)/ (COUNT(DISTINCT(teknisi))*40*4) )*100 as total'))
->orderBy('total', 'desc')
->groupBy('workcenter')
->get();
More about raw expressions here - https://laravel.com/docs/6.x/queries#raw-expressions
Whenever I want to convert SQL query to Laravel I always change one column name, the laravel error report will show your current query and u can compare it to the SQL query
I need to write this SQL query in Propel 2.0
SELECT RequestUser.userID, requests.requestID, requests.created, Responses.created, Responses.response, Responses.createdby_userID FROM requests
LEFT JOIN requestusers RequestUser ON (requests.requestID=RequestUser.requestID)
LEFT JOIN responses Responses ON (requests.requestID=Responses.requestID AND RequestUser.userID=Responses.createdby_userID)
WHERE requests.supportstatusID=3
and requests.requestid=50208;
Most important is the second join, it has multiple conditions.
I for the life of me cannot figure this out.
Any ideas?
I have used the Criteria class to AddMultipleJoins() but to no avail.
$pendingRequests = RequestQuery::create($criteria)
->select(['RequestUser.UserId', 'Request.Id', 'Request.CreatedDate'])
->leftJoin('Request.RequestUser RequestUser')
->leftJoin('Request.Response Responses')// <------ I need this line to have multiple conditions
->filterBySupportStatusId(3)
->find();
Okay I figured it out using this:
->addJoinCondition('JoinName', 'LeftColumn=?', 'RightColumn')
This adds an extra condition to the join!
I have this query in laravel 5.2
$obj_custom_stdy_data = QstCustomStudyData::where('student_id', $this->data_user['student_id'])
->select($list_id . ' as list_id ', 'chapter_id', 'subject_id', 'subject_code_id')
->get()
->toArray();
Well I have a fixed value $list_id got from top code. Actually I want to add new field during query selection as list_id. However I got error for such that method.
When I tried in mysql IDE for example:
SELECT (1+2) as total, c.* FROM users
Then the result is no wrong at all.
Is that anyway to write in query builder for laravel instead of raw style?
You can take the use of DB::raw() method of QueryBuilder like this:
->select(DB::raw('(1+2) as total'));
See more about Query Builder's Raw Expressions
Hope this helps!
I am newbie on Laravel, I have a proper SQL Statement (which works on PHPMyAdmin and Navicat) and I can get results. What i want to do is, I want to take that statements in Laravel without using DB::raw();.
Any Helps will be appreciated.
Select
rmm.message,
count(rmm.message) as number,
receivedTime as time
FROM
rcs rcs, rmm rmm
WHERE
rmm.smsCid = rcs.smsCid AND rmm.receivedTime LIKE '%2013-04-01%' AND length('rmm.message') > '3'
GROUP BY(rmm.message)
Alternatively you can use the Fluent Query Builder. Here's my attempt, of course it's not checked as I don't have your data structure to hand. Hopefully enough to get you started:
DB::table('rcs') // SELECT FROM rcs
->join('rmm', 'rmm.smsCid', '=', 'rcs.smsCid') // Simple INNER join
->where('rmm.receivedTime', 'LIKE', '%2013-04-01%')
->where(DB::raw('LENGTH(rmm.message)'), '>', 3)
->get(array(DB::raw('COUNT(rmm.message)'), 'rmm.message', 'receivedTime')); // Get only the columns you want
You can use DB::query() to run your query. This calls the query method on the active DB connection.