can anyone help me to convert this query into laravel 5:
SELECT `id`,`mobile_number`,`shortcode`,`chapter_id`,`message`
FROM tbl_votes
WHERE id in (SELECT MAX(id) from tbl_votes GROUP BY `mobile_number`, position_id)
Thanks
Figure it out. Thanks
DB::table('votes')->whereIn('id', function($query){
$query->select(DB::raw('MAX(id) as id'))
->from('votes')
->groupBy('mobile_number', 'position_id');
})->where(array('shortcode'=>$shortcode,'position_id'=>$positionId))->get();
Related
please help
SELECT CUSTOMER, COUNT(USER_ID) FROM ticket_table GROUP BY CUSTOMER ORDER BY COUNT(USER_ID) DESC LIMIT 10;
for the following query when implemented in the Laravel query builder, what kind of controller is it?
Do you mean like this :
\DB::table('ticket_table')
->selectRaw('customer, count(user_id)')
->groupBy('customer')
->orderBy(\DB::raw('COUNT(user_id)'), 'DESC')
->limit(10);
I recently started learning Laravel and cannot solve my problem with queries.
SELECT * FROM films WHERE id IN (SELECT film_id FROMfavorites WHERE user_id = 2)
This is the correct code, but how to make it for Laravel
I tried to look in documentation, but it's all not that. Maybe someone knows how to solve this problem?
You can use Eloquent model with whereIn() just like below
Films::whereIn('id',Favorites::where('user_id', 2)->pluck('film_id'))->get();
and also if you use Eloquent model with relation, then it'll be more simple as below
$user->favorites()->films
To build it as a query you can use whereIn
\DB::table('films')->whereIn('id', function($query) {
$query->select('id')->from('favorites')->where('user_id',2);
})->toSql();
this will generate
select * from `films` where `id` in (select `id` from `favorites` where `user_id` = ?)
Could anybody help to translate below query to Laravel Eloquent syntactics.
SELECT LEFT(subject , 10) FROM tbl
Thanks.
on the top of your Class, Call this
use DB;
then type this as your query
DB::table('tbl')->select('LEFT(subject , 10)')->get();
I am trying to to get latest record having the same id. For example
I have table
ID | Created_at
1 |2016-04-26
1 |2016-04-20
1 |2016-04-18
2 |2016-04-27
2 |2016-04-19
I want to get result like this
ID | Created_at
1 |2016-04-26
2 |2016-04-27
How do I do this in Laravel? I was only able to order the record by descending order. Don't know how to pick the latest record of each ID. Thank you.
Try using Collections service from laravel , and use last() method. Example:
$data = ModelName::all();
$last_data_object = collect($data)->last();
//try to see the object using var_dump()
var_dump($last_data_object);
I get it from https://laravel.com/docs/5.2/collections#method-last
You can try with whereRaw to getting last records in groupBy laravel
$data = Table::whereRaw('Created_at IN (select MAX(Created_at) FROM table GROUP BY ID)')->get();
Try it this way, it will give you the max grouped ID which will be the latest:
SELECT MAX(ID), Created_at
FROM table
GROUP BY ID, Created_at
Thank you everyone for help. I was able to solve this by
DB::raw("SELECT ID,created_at FROM <table name> a WHERE created_at=(SELECT max(created_at) FROM <table name> b WHERE a.ID=b.ID)
DB::raw('SELECT * FROM `test` WHERE 1 GROUP BY `ID` ORDER BY `Created_at` ASC');
Try this
Include the directive Db
$id = DB::table('your_table')->orderBy('id', 'desc')->value('id');
If you have user_id.
$id = DB::table('your_table')->where('user_id', $user_id)->orderBy('id', 'desc')->value('id');
hy I'm new to Laravel 4.2 And I have Sub Query Sql Like this
Select *, Count(*) as total_rows From(
SELECT test_id,
SUM(IF(app.status='complete',apt.result,0)) AS complete_sum,
SUM(IF(app.status='process',apt.result,0)) AS process_sum
FROM application_test AS apt
JOIN application AS app ON app.id=apt.application_id
GROUP BY apt.test_id)
my quetion is how I can write this sub query sql on laravel 4.2
You can make subqueries like this.
Test::where('id', function($query){
$query->select('other_test_id')
->whereIn('category_id', ['223', '15'])
->where('active', 1);
})->get()
If you're new on Laravel, consider to use the last version of it, the 5.2 ;)
Convert the MYSQL CASE INTO LARAVEL Query
$query = DB::raw("(CASE WHEN user_group='1' THEN 'Admin' WHEN user_group='2' THEN 'User' ELSE 'Superadmin' END) as name");
and simply execute this query in
DB::table('tablename')->select($query)->get();
or
YourModelClass::select($query)->get();
You will get the result.
Note: Syntax is based on Laravel 5.0