How to Store Data Array in Database on Laravel 9?

 How to Store Data Array in Database on Laravel 9?



In this article, we will show you how to store data arrays in a database on Laravel 9.

The most common way to store data in a database is by using a SQL query. However, there are times when you might need to store data in an array.

One such instance is when you are using the Laravel framework. The Laravel framework makes it easy to store data in an array.

In this article, we will show you how to store data arrays in a database on Laravel 9.

Step 1: Install Laravel


composer create-project laravel/laravel example-app


Step 2: Create Migration

php artisan make:migration create_items_table

database/migrations/2022_04_11_100937_create_items_table.php

Then run migration command to create items table.

php artisan migrate


Step 3: Create Model

php artisan make:model Item

App/Models/Item.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Database\Eloquent\Casts\Attribute;

class Item extends Model

{

use HasFactory;

/**

* Write code on Method

*

* @return response()

*/

protected $fillable = [

'title', 'data'

];

/**

* Get the user's first name.

*

* @return \Illuminate\Database\Eloquent\Casts\Attribute

*/

protected function data(): Attribute

{

return Attribute::make(

get: fn ($value) => json_decode($value, true),

set: fn ($value) => json_encode($value),

);

}

}

Step 4: Create Route

routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ItemController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('item', [ItemController::class, 'index']);



Step 5: Create Controller


app/Http/Controllers/ItemController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Item;

class ItemController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$input = [

'title' => 'Demo Title',

'data' => [

'1' => 'One',

'2' => 'Two',

'3' => 'Three'

]

];

$item = Item::create($input);

dd($item->data);

}

}



Run Laravel App:


All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/item

You can see database output and print variable output:

Database Output:


array:3 [

1 => "One"

2 => "Two"

3 => "Three"

]



In this tutorial, you have learned how to store an array in a database using Laravel. we explain each step clearly so that you can follow along easily. I believe now you will be able to store array data in your Laravel database.

Also, we make sure we discuss the fact that sometimes it is necessary to store data in a JSON format in order to accommodate large data sets or data that is not fixed in columns. The author then provides a Laravel-specific example of how to store a JSON array in a database and how to access it.

Thanks




Post a Comment (0)
Previous Post Next Post