Updated 1 August 2023
Hello Everyone in this blog, we’ll learn Laravel casting with examples, step by step.
Working with Laravel’s Eloquent ORM, you might come across situations where you want to treat some attributes of your models as specific data types instead of raw strings. Laravel provides a feature called “casting” that allows you to automatically transform attribute data into desired datatypes. casting provides functionality similar to accessors and mutators without requiring you to define any additional methods on your model. Laravel’s casting feature saves you from manual type conversions and provides a cleaner and more maintainable codebase.
By defining the $casts
property within your model, you can instruct Laravel to automatically convert those attributes to desired data types. Laravel supports various casting types, including integers, floats, booleans, dates, arrays, and more.
Step 1: Install Laravel Application
Let’s create a fresh new Laravel project. It’s optional if you have already created then you may go ahead.
1 |
composer create-project laravel/laravel laravel-casting-example |
Step 2: Create a Model
Let’s create a simple Eloquent model that represents a book. Run the following command to generate the model and migration:
1 |
php artisan make:model Book -m |
This command will create a Book
model file inside the app/Models
directory and a migration file in thedatabase/migrations
directory.
Step 3: Define the Migration
Open the generated migration file in the database/migrations
directory and define the table schema for the books table.
1 2 3 4 5 6 7 8 9 10 |
public function up() { Schema::create('books', function (Blueprint $table) { $table->id(); $table->string('title'); $table->integer('pages'); $table->date('published_at'); $table->timestamps(); }); } |
Run the migration to create the books table:
1 |
php artisan migrate |
Now, let’s open the app/Models/Book.php
file and define the casting for the pages
and published_at
attributes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Book extends Model { use HasFactory; protected $casts = [ 'pages' => 'integer', 'published_at' => 'date', ]; } |
With this configuration, Eloquent will automatically convert the pages
attribute to an integer and the published_at
attribute to a date whenever you access or set them.
Now that we have our model and casting defined let’s use it to insert and retrieve data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
// Create a new book $book = new Book(); $book->title = 'Laravel for Beginners'; $book->pages = '200'; // This will be automatically casted to an integer $book->published_at = '2023-07-31'; // This will be automatically casted to a date $book->save(); // Retrieve the book $book = Book::find(1); // Access the attributes echo $book->title; // Output: Laravel for Beginners echo $book->pages; // Output: 200 (as an integer) echo $book->published_at; // Output: 2023-07-31 (as a date) |
We created a simple Eloquent model for books and determine how to define casting for attributes. I hope you will like the Tutorial and it will help you to learn Laravel Casting. Please comment below if you have any questions.
You can also hire laravel developers to build your custom solutions on laravel. For exploring the available extensions for Bagisto, you can check out the bagisto extension marketplace.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.