How can i update one row usign two keys in laravel? - php

I have a need to update a table like this :
UPDATE table SET column = value WHERE first_id = 1 AND second_id = 2
Is it possible to do this in Laravel using eloquent model? If it is how can i do this?
I was trying with :
$object = Model::where('first_id' , 1)->where('second_id' , 2)->get();
$object->column = $value
$object->save();

Yes you can do it. Here is how you can do it.
Model::where('first_id', 1)
->where('second_id', 2)
->update(['column' => 'yourValue']);

You are getting a collection. You can update the column of each of those collection. You can only get the first one collection using first.
foreach($object as $obj){
$obj->column = $value;
$obj->save();
}
Or
$object = Model::where('first_id' , 1)->where('second_id' , 2)->first();
$obect->column =$value;
$object->save();

Related

how to merge 2 same collections in larave

consider on first query i get 2 rows and on 2nd query i get 1 row i want to 2+1 = 3 rows into a single collection so that i can have all the data here is my query
$invoices = Invoice::whereIn('user_id', $ids)->get(); //have data in column id 1 and 2
$stock = Invoice::where('customer_id' , Auth::id())->get(); // have data in column id 3
$merge = //how i can merge this?
now consider i merged both my output should be using foreach
foreach($merge as $a){
$b[] = $a->id;
}
dd($b);
output should be [1,2,3]
you can get the both result in one query using orWhere:
$mergedResult=Invoice::whereIn('user_id', $ids)->orWhere('customer_id' , Auth::id())->get();
You can do this in one query, BUT if you actually want to merge these 2 Eloquent Collections:
$new = $invoices->merge($stock);
Surprise, there is a merge method.
Laravel 8.x Docs - Collections - Available Methods - merge

Laravel Eloquent update multiple rows with same value but different ID

I'm using Laravel 6 and Eloquent. I'm looking for a way to update a set of rows with a set value, each identified with a unique ID.
This is what I'm doing right now:
$ids = [3948, 1984, 7849, 4456, 394];
$value = false;
foreach ($ids as $id)
{
User::where("id", $id)->update(["status" => $value]);
}
Is there a way to accomplish the same with only 1 query instead of 5?
You can use whereIn, like:
$ids = [3948, 1984, 7849, 4456, 394];
$value = false;
User::whereIn("id", $ids)->update(["status" => $value]);

return 1 column from relation in 1 array

everybody, I need to return just imagePath in images into 1 array not objects
has many relation
any ideas
$albums_category = AlbumCategory::with('albums.images')->find($id);
You can do :
$album = AlbumCategory::with('images')->find($id);
$imagePaths = $album->images->pluck('imagePath');
or if you have nested relation then :
$album = AlbumCategory::with('albums.images')->find($id);
$imagemapths = [];
$album->albums->each(function($album) use($imagemapths){
return array_merge($album->images->pluck('imagePath'), $imagemapths);
});
However, if you are plucking just one column, its better to just select it and avoid selecting columns from database you do not need. SO you can do :
$album = AlbumCategory::with('albums.images:id,imagepath')->find($id);
or similar using :foreign_key,column_to_select depending on your table stucture.

eloquent relation in laravel with json

suppose i have two tables(Team and User) in my database . in Team table i have data like "["5","6","7","9","10","26","27"]"(json) , and in users table i have id column in which data is like serial no(Auto_increment .1,2,3 ....nth).is it possible a relation which finds a if json has that single key in it.
i have a loop statement which has the result that i want but i want that data in relation the code is :
foreach($model as $key => $value){
$user_data = User::whereIn('id',json_decode($value->member_ids))->get();
$model[] = $user_data;
}
Try this
$arr = json_decode("["5","6","7","9","10","26","27"]");
if (in_array(5, $arr )) {
// code
}

Laravel 5 - Change all values in a Column of a table

I have a table with column called "estado" and I want change all the column in one shot.
This is the right method? The faster one?
$vendedor = Vendedor::with('produtos')->find( $idVendedor );
foreach ( $vendedor->produtos as $produto ) {
$produto->pivot->estado = $novoEstado;
};
The column what I want change is "estado". There's a way to do it without the foreach?
Update Specific column you want to upadate using Laravel eloquent.
Way -> 1
[Status-> Column you want to update]
Model::where('status', '=', 1)->update(['status' => 0]);
Way -> 2
[Status-> Column you want to update]
$allData = Model::where('status', 1)->get();
foreach ($allData as $data){
$data->status = 0;
$data->update();
}
The simplest would be to just use the query builder for this (instead of your model)
DB::table('table_name')->where('vendedor_id', $idVendedor)->update('estado', $novoEstado);

Categories