I have the following eloquent code:
$gifts = FriendGift::find($collectedGifts); // An array of IDS
I want to set all as collected = 1.
I tried this:
$gifts->update(['collected' => 1]);
but I get the following error:
Method update does not exist
What's wrong?
You are not calling update on a Model. You are calling update on an Eloquent Collection which doesn't have a method update.
Just call the model and do a mass update like this:
FriendGift::whereIn('id', $collectedGifts)->update(['collected' => 1]);
If $collectedGifts is an Array, its not gonna work. Model::find() is waiting for a simple id, not an array.
Anyways, i prefer using the save method of an element if i want to update a field.
So you just get the Row you want, assign some value to it, and then save the instance.
For example:
$gifts = FriendGifts::find(15);//This is just 1 gift
$gifts->collected = 1;
$gifts->save();
For multiple gifts:
$gifts = FriedGifts::where('id',15)->get();
foreach($gifts as $one_gift){
$one_gift->collected = 1;
$one_gift->save();
}
Or something like that!
Hope this helps!
Good luck!
Related
I have some properties and i want to get the object for each property, currently, I am using the eloquent in the foreach loop like this as shown in the image that will describe the best..
but it is really not a good approach because if I have 100 published property I will making 100 calls to the DB... and that is not correct?
I need a suggestion and a proper solution to this query?
Thanks in advance
Before foreach you can get all the RentalProperty items from db like this:
$allRentalProperties = RentalProperty::all();
and in foreach loop you can get those items without connecting database like this:
$propertyObj = $allRenatalProperties -> where('id', $property['id']) -> first();
Also, you can use array shorthand.
$newArray = [];
it's much simple and readable.
You can do array pluck before loop:
$propertyIds = Arr::pluck($published_properties, 'id');
Then you can make a whereIn query to get only those data which are in $published_properties object
$propertyObj = RentalProperty::whereIn('id', $propertyIds);
Then you can access that object with id if you change array key with record id.
I have the following ResearchModel (eloquent):
$research = new ResearchModel();
$research->id = 2;
$research->name = "test";
$research->save();
I am expecting Laravel to run an update statement (because I set the id), but it runs an insert statement instead.
$research->update(); wont do anything.
I don't want to use array in this case because I need the eloquent model events to be triggered.
I also don't want to run ResearchModel::find(2); before, this will cause significant performance problems in my use-case.
Is there any way to tell Laravel to update by the id?
Thanks
If you don't want to "select" first. Then you can update directly. This will run only one query.
$research = ResearchModel::where('id',2)->update([
'name' => 'test',
]);
A dirty solution:
// Create an instance without insert to database.
$instance = new ResearchModel;
$instance->id = 2;
$instance->name = "test";
// Set the instance status directly.
$instance->exists = true;
// Update database without model.
ResearchModel::where('id', $instance->id)
->update(['name' => $instance->name]);
// Manually trigger event.
event('eloquent.updating: App\ResearchModel', $instance);
The premise of this method is that you already have the required data. If the data required for the event must be read from the DB, then this method is useless.
Find by id
$research = ResearchModel::find(2);
Then you can run update function
$research->update([ 'name' => "test"]);
and if u don't want to use
ResearchModel::find(2);
then u have to use route binding
https://laravel.com/docs/6.x/routing#implicit-binding
I used this query to update the status column.
$val="1";
vehicles::where('id' , '=' , $veh_status)->update(['status' => $val]);
But when I submitted the status value doesn't change.
you can trace your query by using ->toSql() method !
try this to find whats happening in back
Not sure what the problem is there because you haven't given much info to work with, but you can check these suggestions:
Check if the column is set to be mass assignable in the model class, that is, it is in the fillable[] array.
make sure the id you pass to the where() function is valid.
Try using another function, save() which will achieve the same results you seek, like this;
// filter the vehicle
$vehicle = vehicles::where('id', '=', $veh_id)->first();
or
$vehicle = vehicles::find($veh_id);
$vehicle->status = 1;
$vehicle->save();
Lastly, I noticed your id variable you pass to the where the () function is called $veh_status "presumably - vehicle status" and not $veh_id, "presumably - vehicle id" so probably check that out.
Ref: Laravel Model Update documentation
$user_emails = ["email_1#domain.com", "email_2#domain.org"];
$users = Users::whereIn("email", $user_emails);
The table for users also has a phone column for each user. What's the best way to get a list/array of the phone number as an array?
$users->all()->phone(); // Like (which is not correct)
Try to use get() like :
$users = Users::whereIn("email", $user_emails)->get(['phone'])->toArray();
Or also pluck() like :
$users = Users::whereIn("email", $user_emails)->pluck('phone')->all();
Hope this helps.
Use pluck method to fetch a specific column's values and then use toArray on returned Collection object to get results as an array.
$phoneNumbers = Users::whereIn("email", $user_emails)->pluck('phone')->toArray();
You can get all column data with get()
Example:
$user = $user::where('email', $user_emails)->get();
You can get the list with foreach loop method.
Does the Laravel wherecollection method modify the collection?
On the Laravel documentation you can read this:
almost every method returns a new Collection instance, allowing you to preserve the original copy of the collection
And the only methods which have a warning about modifying the collection are transformand forget
But I have this code:
$descriptions = Description::where('description', $request->description);
if ($descriptions->count()) {
$descriptionsWithSameUnit = $descriptions->where('unit_id', $request->unit);
if ($descriptionsWithSameUnit->count()==0) {
$descriptionsWithAnotherUnit = $descriptions->where('unit_id', '!=', $request->unit);
if ($descriptionsWithAnotherUnit->count()) {
...
And the collection IS modified after the first where so $descriptionsWithAnotherUnit is always empty, because on that point the collection only has the records where unit_id == $request->unit. Is this a bug in the framework or in the documentation?
And the question that arises from here: What can I do to keep a copy of the original object without retrieving again from the DB? I tried this:
$descriptions = Description::where('description', $request->description);
if ($descriptions->count()) {
$descriptionsWithSameUnit = $descriptions;
$descriptionsWithSameUnit->where('unit_id', $request->unit);
...
But the $descriptions object is also modified when I apply the where method to the $descriptionsWithSameUnit object
The first thing is that to get collection you need to use get, so if you want to get collection, you should do:
$descriptions = Description::where('description', $request->description)->get();
and not only:
$descriptions = Description::where('description', $request->description);
Second thing is that there's no possibility to use operator using where method on collection, so:
$descriptionsWithAnotherUnit = $descriptions->where('unit_id', '!=', $request->unit);
is completely incorrect. You should use filtermethod here.