In this tutorial you will learn about the Laravel 8 Traits Example Create Use Trait in Laravel and its application with practical example.
In this Laravel 8 Traits Example Create Use Trait in Laravel tutorial I will show you how to create and use reusable traits in laravel 8 application. In this tutorial you will learn to create and use traits in laravel. In this article I will share example to create and use traits in laravel application.
What Is Laravel Traits?
In this article we are going to learn about Laravel 8 Traits. Traits is a independent reusable class that is that allows you to implement single inheritance in order to reuse it. With traits we can create a reusable piece of code and inject it in controller and modal in a Laravel application.
Laravel 8 Traits Example: Create & Use Trait in Laravel
In this step by step tutorial I will demonstrate you with example to create and use traits in laravel 8. Please follow the instruction given below:
- Traits in Laravel Example
- Install New Laravel Application
- Configure Database Connection
- Model and Migrations
- Insert Fake Data
- Create a Trait
- Using Traits in Laravel
Laravel Traits Example
In this example we will be creating and use a reusable traits.
Install New Laravel Application
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project laravel/laravel laravel-traits-example --prefer-dist |
Configure Database Connection
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD= |
Model and Migrations
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Student -m |
Open database/migrations/timestamp_students_table.php file and incorporate the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateStudentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('students', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('students'); } } |
Next, go to app/Models/Student.php file and add the $fillable property in the Student model as following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Student extends Model { use HasFactory; public $fillable = [ 'name', 'email', ]; } |
Now, run the migration to create database table using following artisan command: