Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel migration array type (store array in database column)

I want to store an array of integers in my table and I can't find any type that supports array in Documentation, any suggestion.

Migration:

public function up() {     Schema::create('pickups', function (Blueprint $table) {         $table->increment('id');         $table->boolean('default');         $table->integer('shifts');  <<--------- HERE I want to store an array of integers         $table->integer('status_id');          $table->timestamps();     }); } 
like image 835
Zakaria Acharki Avatar asked Oct 05 '15 17:10

Zakaria Acharki


People also ask

Where are migrations stored laravel?

Migrations are in the /database/migrations directory.

How do I add a column to an existing table using migration in laravel 8?

Make sure to put the word add instead of create within the class name. Then go to app/database/migrations folder and you see the result below. Step 2 Amend the migration script by adding column to be created. The file name generated by the migration was 2021_10_12_000000_add_reference_number_to_books_table.


2 Answers

The array datatype is not present in all database systems and because Laravel's Schema Builder is database agnostic, it doesn't offer methods to create non-common datatype columns. So you have two options:

1. Use a raw SQL statement to add the column, something like the statement below I think should work. Although I'm not sure if the Query Builder or Eloquent can handle these types of columns correctly:

DB::statement('ALTER TABLE pickups ADD COLUMN shifts integer[]'); 

2. Use Eloquent's available workaround by using attribute casting. In your migration create the column as json like so:

public function up() {     Schema::create('pickups', function (Blueprint $table) {         $table->increment('id');         $table->boolean('default');         $table->json('shifts');         $table->integer('status_id');          $table->timestamps();     }); } 

Then you can setup your Pickup model (if you haven't done so already) and use the $casts property:

class Pickup extends Model {     protected $casts = [         'shifts' => 'array'     ]; } 

This will let Eloquent know that when it fetches data from the database it will have to convert the shifts column value to an array. This is only emulating an actual array, as at the database level the column is of type TEXT and the array is serialized. However when unserializing the column value, Eloquent returns an actual array of integers for you to use in your code. Below is an example use case:

// Create a new Pickup entry $pickup = App\Pickup::create([     'default' => true,     'shifts' => '[1, 5, 7]', // you can easily assign an actual integer array here     'status_id' => 1 ]); 

Assuming the above generated an entry with id equal to 1 when you later retrieve the entry:

$pickup = App\Pickup::find(1); dump($pickup->shifts); 

The dump() from the code above will output an actual array of integers:

array:3 [▼   0 => 1   1 => 5   2 => 7 ] 
like image 111
Bogdan Avatar answered Sep 28 '22 05:09

Bogdan


There is another more complicated approach, but it allows to create truly native arrays using the schema builder.

Example for PostgreSQL.

  1. Register new int_array type which will resolve into int[] by extending the existing DB Schema Grammar:
\DB::connection()->setSchemaGrammar(new class extends PostgresGrammar {     protected function typeInt_array(\Illuminate\Support\Fluent $column)     {         return 'int[]';     } }); 

You can put this code right inside the migration if you need it just once or into AppServiceProvider to make it available in whole project.

  1. Now you can use this type in your migrations:
Schema::table('users', function (Blueprint $table) {     $table->addColumn('int_array', 'group_ids')->nullable(); }); 
like image 27
Stalinko Avatar answered Sep 28 '22 06:09

Stalinko