please assist to convert this SQL query into laravel format
"update user_names set assignedTo = '09874',Assigned=1 where timePackage='1 hr' and Assigned=0 order by id limit 1"
Try this:
use Illuminate\Support\Facades\DB;
DB::table('user_names')
->where('timePackage', '1 hr')
->where('Assigned', 0)
->orderBy('id')
->limit(1)
->update([
'assignedTo' => '09874',
'Assigned' => 1,
]);
See Laravel docs for more info.
Related
This raw SQL query is returning the expected result on my SQL console. Would you please help me to transform it into a Laravel Eloquent query?
SELECT * FROM `my_services`
WHERE `user_id` = 1 and `financial_year` = '2021-2022'
AND (service_type = 'Return' OR service_type = 'Correction Return')
ORDER BY id DESC LIMIT 1,1;
I have tried to implement it like the following.
MyService::where([
'user_id' => $user->id,
'financial_year' => $request->financial_year,
'financial_year' => '2021-2022'
])
->orWhere(['service_type' => 'Return'])
->orWhere(['service_type' => 'Correction Return'])
->orderBy("id", "desc")
->offset(1)
->limit(1)
->get();
Try this query -
MyService::where('user_id', 1)->where('financial_year', '2021-2022')->where(function($q) {
$q->where('service_type', 'Return')->orWhere('service_type', 'Correction Return');
})->limit(1)->offset(1)->orderBy('id', 'DESC')->get();
From: https://laravel.com/docs/8.x/queries#limit-and-offset
Use ->skip(1) or ->offset(1) for offset
Use ->take(1) or ->limit(1) to limit count of returned results
How do I convert this MySQL query to Laravel Eloquent Query:
SELECT * FROM service_package WHERE price IN ('110010', 'Test 02', '11009')
You can use the whereIn() like:
Using DB::table()
$data = \DB::table("service_package")
->whereIn("price", ['110010', 'Test 02', '11009'])
->get();
Using Eloquent
$data = App\ServicePackage::whereIn("price", ['110010', 'Test 02', '11009'])
->get();
Here, make sure you have created model ServicePackage for the table service_package.
How can I convert the following SQL query into Laravel query builder? Can anyone help?
SELECT email,user from (select email,user_id as user from businesses ) as ui WHERE user > 1
Use this:
DB::query()->select('email', 'user')
->fromSub(function($query) {
$query->select('email', 'user_id as user')
->from('businesses');
}, 'ui')
->where('user', '>', 1)
->get();
What is the active Record way of adding IN condition to an active Query
in yii 1.x you could use CDbCriteria like this
$cr = new CDbCriteria();
$cr->addNotInCondition('attribute', $array);
There seem to be no equivalent API call in yii2 active record implementation, how to do this via active record ?
Well all query operands seems now merged within in yii\db\QueryInterface::Where() per documentation
an In condition can now be added using something like
$query = MyModel::find()->where(['attribute'=>$array]);
for a not In condition it is slightly different format
$query = MyModel::find()->where(['not in','attribute',$array]);
$query = MyModel::findAll(['not in ','attribute',$array]);
http://www.yiiframework.com/doc-2.0/guide-db-active-record.html
For numbers:
$query = MyModel::find()->where('NOT IN('.implode(',', $array).')');
For strings
$deleteContracts = Contract::find()
->where([
'session_id' => $session_id,
'status' => Contract::STATUS_COMPLETED
])
->andWhere(['not in', 'contract_id', $contracts])
->all();
For me the only working solution was :
$query = MyModel::find()->where('`your-attribute` NOT IN(' . implode(',', $array) . ')')->all();
This is related to one of my question earlier where:
Update table1 field with table2 field value in join laravel fluent
But since this is a different approach now, I will just ask another question:
How do you properly do an update using DB:raw?
I want to update the favorite_contents.type with the value of contents.type, but it doesn't do anything, the static setting of 1 to favorite_contents.expired is working.
This is my code which still doesn't update the type even when the DB::raw was used:
$table = 'favorite_contents';
$contents = DB::table($table)
->join('contents', function($join) use($table){
$join->on("$table.content_id", '=', 'contents.id');
})
->whereIn("$table.content_id",$ids)
->update(array(
"$table.expired" => 1
));
DB::raw("UPDATE favorite_contents, contents SET favorite_contents.type = contents.type where favorite_contents.content_id = contents.id");
This is the first code that doesn't update before I resorted to the above code that doesn't work as well:
$table = 'favorite_contents';
$contents = DB::table($table)
->join('contents', function($join) use($table){
$join->on("$table.content_id", '=', 'contents.id');
})
->whereIn("$table.content_id",$ids)
->update(array(
"$table.expired" => 1,
"$table.type" => "contents.type"
));
P.S:
This is working when done on an sql editor:
UPDATE favorite_contents, contents SET favorite_contents.type = contents.type where favorite_contents.content_id = contents.id
code raw updates like this:
...->update( array(
'column' => DB::raw( 'column * 2' )
) );
DB::statement("UPDATE favorite_contents, contents SET favorite_contents.type = contents.type where favorite_contents.content_id = contents.id");
Try DB::statement for raw queries that does not involve outputting something (select).
Will be work such similar, simple realization in Laravel 5.2 ,
Query Builder:
DB::table('stores')
->where('id', $request)
->update(['visibility' =>DB::raw($value)]);
This response is tested real site and working properly