skip to Main Content

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

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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
        ]);
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search