Home > OS >  i want to convert my laravel select query with update query
i want to convert my laravel select query with update query

Time:11-15

This is my query:

$data = Collections::select(DB:raw("REGEXP_REPLACE(tour_id,'(,2|2,|2)','') as `new_tour_id"))->get();

I want to convert this query to update all my records in the database. This is my database table shows:

I want this result: enter image description here

CodePudding user response:

Since Laravel 5.x allows attribute casting so it's possible to cast attributes to another data type for converting on runtime. In this case, just declare a protected $casts property for example:

protected $casts = [
    'tour_id' => 'array', // Will converted to (Array)
];

then store your ids like this

enter image description here

and finally search like this :

->whereJsonContains('tour_id', 3)->update([...]);

read more : JSON Where Clauses

CodePudding user response:

Assuming that you have a model for this table as Tour what you have to do is this:

$tours = Tour::select('tour_id')

foreach($tours as $tour) {
    $tour->update([
        tour_id = $whatever_id_to_update
    ]);
}
  • Related