Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inserting array value to database laravel

I have a form attributes called, name, description and features where features is a multiple checkboxes which is like

feature 1
feature 2
feature 2
feature 4

User can select multiple checkbox at once. I have a database table called product like

-----------------------------------------
    id    name    description    features  
-----------------------------------------

When user selects the multiple checkbox, i need to insert all checkbox value in the features column. Right now I can echo the selected checkbox value like

 $feat = Input::get('features');
    foreach ($feat as $key => $n){
     echo $feat[$n];
    }

but I need those features to insert into the database, for normal insertion, we would do like:

$product = new Product;
$product->name = Input::get('name');
$product->description = Input::get('description');
$product->features = Input::get('features');
$product->save();

but how should i modify the above code in order to save the array value to the database? I am trying to insert the value to same column because i won't be querying it on the basis of features.

like image 533
Sid Avatar asked Jan 07 '23 14:01

Sid


1 Answers

It's quite simple. If you know that you won't query the features, it's perfectly fine to store them as Json or a serialized array. But if you will need to query them and they are a key aspect of your application, you should put them in their own table.

I prefer to store arrays in Json-format because it's easier to read and is not specific to PHP. Laravel gives you pretty sweet options to make this work.

At first, declare the features-field of you products-table as json in your migration:

Schema::create('products', function (Blueprint $table) {
    // ...
    $table->json('features');
});

Then tell your Product-model to automatically cast it to an array when you access it by simply setting a $casts-attribute:

class Product extends Model
{
    // ...
    protected $casts = [
        'features' => 'json'
    ];
}

That's it. Now the array will be stored as Json, but when you access it with $product->features you'll get an array back.

To make everything even more simple, you should set up a fillable attribute on your Product model:

class Product extends Model
{
    // ...
    protected $fillable = ['name', 'description', 'features'];
}

This allows for doing something like this in your controller (or wherever you create the product):

$product = Product::create(Input::all());

...instead of newing it up and setting the attributes one by one.

And as mentioned above, be sure you don't need the features to be query-able, meaning you won't encounter situation where you're trying to get certain products only having specific features or something. But if you do need to look up features, you should put them in their own table. Otherwise this approach is just fine because it's more convenient.

like image 109
Quasdunk Avatar answered Jan 23 '23 03:01

Quasdunk