i have a want to update data in a column in the database according to some value in an array.here is an example of the code below
$rmupdate=Room_name::where(['rentalhouse_id'=>2,'id'=>['110','112']])->first();
dd($rmupdate);
$rmupdate->update([
$rmupdate->is_occupied=1,
]);
here i want to fetch all the room names that has an id of both 110 and 112 and also whose rentalhouse_id is 2.i have tried the code above but i get a null response but on the table the room names have data in them.where might i be missing a point.
CodePudding user response:
First of all, if you want to search for a value inside an array, you need to use whereIn
instead of where
. Also, since you need every room and not only the first one, you have to use ->get()
instead of ->first()
. So in your example, I believe that would have to be something like this.
$rmupdate=Room_name::where('rentalhouse_id', 2)->whereIn('id', [110,112])->get();
What bothers me though is that you are telling that you get null as a response, despite the fact that your code seems to not have mistakes. You should get the just one element that meets these criterias. Could it be that the ids that you are looking for do not exist?
Keep in mind that by using get()
instead of first()
you will get as a response a collection so you ll have to iterate through the objects using a foreach
loop in order to update them.
CodePudding user response:
Try somethig like:
$rmupdate = Room_name::where('rentalhouse_id', 2)
->whereIn('id', ['110','112'])
->update([ 'is_occupied' => 1]);
Look Mass updates