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);
Related
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();
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]);
I have a simple eloquent query and want to include another table with my results, however, the order of relationship results is incorrect.
Is it possible to order the results without using an SQLRAW statement
$groups = AttributeGroup::with('attribute')->where('page_id', $page->id)->get();
What I would like -
$groups = AttributeGroup::with('attribute')->orderBy('iteration', 'DESC')->where('page_id', $page->id)->get();
I get the error of Unknown column because this column is part of relationship table.
This will order each attribute relation of every attribute group result:
$groups = AttributeGroup::with(['attribute' => function ($query) {
$query->orderBy('iteration', 'DESC');
}])->where('page_id', $page->id)->get();
Is this what you want to achieve?
You can use closures to change the query when using with and has.
$groups = AttributeGroup::with(['attribute' => function($query){
$query->orderBy('iteration');
})->where('page_id', $page->id)->get();
Details are available on https://laravel.com/docs/5.6/eloquent-relationships#constraining-eager-loads
I have related items in my database. I selected all of items from database by related id:
$next_stock = $this->model->get()->where('part_id', $in_data['part_id'])->all();
and I collection of rows grouped by one specific id, like on the picture. All of them selected by "part_id":
Selection Of Items
Grouped By Same Id
Also with this line of code i can select one of the items from this collection:
$next_stock = $this->model->get()->where('id', $old_stock['id'])->where('part_id', $in_data['part_id'])->first();
But how can I select the following items after this one?
Or, how can I select second or third item from this collect?
I cannot just increase id number by one from first, because sometimes this item ids not following each other.
Having a collection, you can take a specific element in the position with a combination of take() and last().
$collection = $this->model->get()->where('part_id', $in_data['part_id'])->all();
$second = $collection->take(2)->last(); //if this doesnt work, do it in 2 steps
$third = $collection->take(3)->last(); //if this doesnt work, do it in 2 steps
If you don't have a collection, take directly from database like this
$second = $this->model
->where('part_id', $in_data['part_id'])
->skip(1)
->first();
If it doesn't work with first()
$collect = $this->model
->where('part_id', $in_data['part_id'])
->skip(1)
->take(1)
->get();
$second = $collect->first();
Edit
skip() and take() are actually part of the query builder, not eloquent model. So it won't work with Eloquent in Laravel 5.4
Try with
$collect = $this->model
->where('part_id', $in_data['part_id'])
->get(1); //For the second record, 0 being the first
If you aren't doing it yet, you should set your model's relationships.
E.g. If you use "one-to-many", Eloquent will automatically determine the proper foreign key column on the model for you.
$parts = App\Stock::find(1)->partId;
foreach ($parts as $part) {
//
}
I have some problems here.
I get a collection which like:
$VenderData=Vender::where('active', '=', '1')->get();
Inside the collection i have a column called 'type' and the data looks like 'A1,A2,A3,'
or
'A1,A3,'
I want to transfer those codes to real names from
another table 'vender_type'.
code name
A1 XX
A2 OO
A3 ZZ
then add a new column into the original collection $VenderData.
How can i do this?
You must split the type attribute (using array explode(string $delimiter , string $string)) , and get the name of each one from vender_type table, try this :
$VenderData = Vender::where('active', '=', '1')->get();
foreach($VenderData as $vend)
{
$vend->new_type = array();
$types = explode(",", $vend->type);
foreach($types as $type)
{
$t = vender_type::where('code', $type)->first();
if($t)
$vend->name_type[] = $t->name;
// or for example : $vend->name_type[$type] = $t->name;
}
}
I hope that will help you.
I think that you need better query than modifying each value after. This vender_type is probably some foreign key in you database so you can get that value in query builder. To find out about foreign keys and joins in laravel eloquent check this.
Basically you need something like this
$users = DB::table('table1')
->join('table2', 'table1.vender_type', '=', 'table2.vender_type')
->select('table1.code', 'table1.name', 'table2.vender_type')
->where('table1.active', '=', '1')
->get();
Just replace table1 and table2 with values of you table names in database and you're done.
Hope it helps