> mobileapp Strategy - From Idea to Mobile App RealityVinova Our team will brainstorm with you on where to begin, where to go, and how to get you there. Whether you have a spark of an idea or an existing app – we can help. Getting your mobile strategy right is what our unique services are all about. We’ll wrestle with business challenges, discover new opportunities that will help you define and refine your product ideas into mobile app reality.

Working with MySQL JSON Columns in Laravel: Custom Properties Example – Laravel Daily

Working with MySQL JSON Columns in Laravel: Custom Properties Example – Laravel Daily

JSON fields are gaining more popularity since they became officially supported in MySQL 5.7.8. Even the popular Spatie Laravel Medialibrary package use them, so why shouldn’t we? This tutorial will show you an example.

Let’s say we have an e-shop project and want to store products. For some of them, we don’t really know what the fields will be – some will require sizes, others will require colors, origin country, manufacturer etc. So here’s where JSON field may become useful – we will store whatever custom properties data there.

Here’s the form for our product:

Notice: For the simplicity, I didn’t implement JavaScript add/remove dynamic fields, this is outside of JSON fields article. Here it’s hard-coded to 5 fields.

Step 1. Back-end: Migration + Model

To create a JSON field, all we need to do in Laravel migration is use ->json() method:

Next, we need to tell our model app/Product.php to cast that column from JSON to an array automatically:

This way, we will receive $product->properties as array and don’t need to do json_decode() at all.

Step 2. Blade Form With Array

As I mentioned before, I added five fields for properties, user may fill one of them or all five:

As you can see, a simple @for loop and every key-value pair has an index in the array, from 0 to 4.

Step 3. Saving Properties

Our ProductController::store() method will be simple.

Yes, that’s it. Some validation for required fields in StoreProductRequest, but nothing more fancy. And we don’t need to do anything with our JSON field, because we’re already passing the array from Blade, so it will be automatically casted to JSON.

This is how it will look in the database:

We have only one problem. In this example, I’m not validating for empty values so they will still be stored in JSON, like this – see last two values:

To avoid that, we need to eliminate null values from the array. I will use Eloquent mutator feature and transform the array to the one without empty values in app/Product.php model:

Notice: I know there’s probably more elegant way to perform this array operation, I just thought it’s not that relevant to this article to search for one-liner array solution.

Step 4. Showing Properties

In the table of products, we will probably want to show something like this:

To achieve that, we will simply do a @foreach in the <td> cell of resources/views/products/index.blade.php file:

Step 5. Edit/Update Properties

Edit form will have the same structure, just the values of the fields will be set from the array. Here’s the part of our resources/views/products/edit.blade.php:

With controller’s update() method, it’s really similar to store() – we just use all request to update the data.

That’s it, JSONs are simple, right?

Like our articles?
Check out our Laravel online courses!

This content was originally published here.

Malcare WordPress Security

web design singapore,mobile app development singapore,app developer singapore,web design services singapore,mobile game developer singapore,singapore web development,mobile apps singapore,singapore web design,ruby on rails developer singapore,android developer singapore,web development company singapore,website developer singapore,mobile apps development singapore,website design singapore,singapore mobile app developer,web designer singapore,singapore mobile application developer,mobile developer singapore,singapore website design,mobile application development singapore,app development singapore,design firms in singapore,design agency singapore,website development singapore,graphic designer in singapore,developers in singapore,web design company singapore,ios developer singapore,mobile application developer singapore,web application singapore,developer in singapore,ios app development singapore,website designer singapore,singapore web design services,singapore app developer,web development singapore,mobile app developer singapore