How create it in Laravel Query builder - laravel

SELECT e.match_id, e.team_id
FROM matches
LEFT JOIN match_events e
ON e.match_id = matches.id AND e.match_event_type_id = 1

Try this,
$result = DB::table('matches as M')
->leftjoin('match_events as E','E.match_id','=','M.id')
->where('E.match_event_type_id',1)
->select(['E.match_id','E.team_id'])
->get();

Related

Laravel 5.6: how to create a subquery using Query Builder

I would like to reproduce following mySQL query using Laravel query builder:
*SELECT SUM(scores) FROM (SELECT scores FROM player_games WHERE player_id = 1 ORDER BY id DESC LIMIT 2) scores
Any suggestions?
Here the solution:
$sub = playerGame::where('player_id',1)->where('scores','>',0)->limit(2)->orderBy('id','desc');
$count = DB::table( DB::raw("({$sub->toSql()}) as sub") )
->mergeBindings($sub->getQuery())
->sum('scores');
return $count;
Use fromSub():
$sub = playerGame::select('scores')
->where('player_id', 1)
->where('scores', '>', 0)
->limit(2)
->orderBy('id','desc');
$sum = DB::query()->fromSub($sub, 'scores')->sum('scores');
$responce= DB::table('player_games')->where('player_id',1)->sum('amount');
dd(collect($responce)->sortByDesc('id')->take(2));
please cheack this one.....i try it's work....and add use DB;in the top of controller....

Group By and Order By in laravel 5.4

I just need to order my records according to updated, and then group them according to card_id.
This is my code:
$triages = Triyage::latest('updated_at')->groupBy('card_id')->paginate(8);
return view('Admin.Opd.card_opd', compact('triages'));
Use a subquery JOIN:
$join = Triyage::select('card_id', DB::raw('max(updated_at) updated_at'))
->groupBy('card_id');
$sql = '(' . $join->toSql() . ') as latest';
$triages = Triyage::join(DB::raw($sql), function($join) {
$join->on('triyages.card_id', 'latest.card_id')
->on('triyages.updated_at', 'latest.updated_at');
})->paginate(8);

Covert MySql query to Laravel

Can you please help me out to write the Laravel Raw Query for below Mysql query.
SELECT
SUM(IF(d.business_email_template_10_open = "Yes", 1,0)) AS `business_email_template_10_open`,
SUM(IF(d.business_email_template_11_open = "Yes", 1,0)) AS `business_email_template_11_open`
FROM dummy_email_track d
join recommend_email_send_by_cron r on d.user_id = r.user_id
join user_form_submission u on r.user_id = u.id'
where d.business_id = $businessId
If you can't use stored procedure, ignore my answer.
I think you can't use Laravel QueryBuilder cause something reason(I have a similar situation). How about using SP(stored procedure)?
Mysql SP
CREATE DEFINER=`your_mysql_id`#`%` PROCEDURE `get_usage_business_email_template`()
__SP:BEGIN
BEGIN
SELECT
SUM(IF(d.business_email_template_10_open = "Yes", 1,0)) AS `business_email_template_10_open`,
SUM(IF(d.business_email_template_11_open = "Yes", 1,0)) AS `business_email_template_11_open`
FROM dummy_email_track d
join recommend_email_send_by_cron r on d.user_id = r.user_id
join user_form_submission u on r.user_id = u.id'
WHERE d.business_id = $businessId
END;
END
And, Laravel call this SP like below
$result = DB::select('CALL get_usage_business_email_template()');
I hope you will save your time becasue my answer...
This should give you a headstart!
https://laravel.com/docs/5.6/database#running-queries
$results = DB::select('select * from users where id = :id', ['id' => 1]);
Make sure you give right id to your where clause. So in you case it would something like this:
(where d.business_id = :businessId, ['businessId' => 1])
Goodluck!
Please try this :
$data = DB::table('dummy_email_track as d')
->select('d.*','r.*','u.*')
->selectRaw('SUM(IF(d.business_email_template_10_open = "Yes", 1,0)) AS business_email_template_10_open')
->selectRaw('SUM(IF(d.business_email_template_11_open = "Yes", 1,0)) AS `business_email_template_11_open`')
->leftJoin('recommend_email_send_by_cron as r','d.user_id', '=', 'r.user_id')
->leftJoin('user_form_submission as u', 'r.user_id', '=', 'u.id')
->where('d.business_id','=', $businessId)
->get();

How to make and condition with or in laravel query

My query is as below
SELECT * FROM `user_register`
INNER JOIN `locationdetail` on `locationdetail`.`userid` = `user_register`.`id`
INNER JOIN `lifestyle` on `lifestyle`.`userid` = `user_register`.`id`
WHERE `lifestyle`.`drink` in (2) and
( `locationdetail`.`state_id` in (4121) or `locationdetail`.`country_id` in (38))
In this query how to make bracket inner query in laravel model
Sorry there got distracted
$drinks = [1];
$state_ids = [1,2]
$country_ids = [1,2,3]
$someModel
->join() // enter your joins here
->whereIn('lifestyle.drink', $drinks)
->where(function( $q1 ) use ($state_ids, $country_ids) {
// insert the whereOr queries here against the $q1 using the data in `use` params
})
->get();
To check your query there is a toSql() function you can use in laravel too.

Symfony Doctrine_Query DELETE with INNER JOIN

I am using symfony+doctrine, and I want to perform a delete query with a join. See below for my code I am currently using that works fine.
$sql = 'DELETE a
FROM a
INNER JOIN b ON a.b_id = b.id
WHERE b.c_id = :c_id';
$pdo = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();
$stmt = $pdo->prepare($sql);
$params = array('c_id' => $c_id);
$stmt->execute($params);
Anyone know how I can do this using:
Doctrine_Core::getTable('a')
Or
Doctrine_Query::create()->delete()->from('a')
I have had no luck with either.
I just don't really want to use raw SQL in my app.
Something like this should do it
Doctrine_Query::create()
->delete('a a')
->innerJoin('a.b b')
->where('b.c_id = ?', $c_id)
->execute()

Resources