Traits are a mechanism for code reuse in single inheritance languages such as PHP. Write the same code again, to avoid this use the traits. The traits are used when multiple classes share the same functionality.
Here is an example of trait.
<?php
namespace App\Http\Traits;
use App\Models\Users;
trait UserTrait {
public function userdata() {
// Fetch all the user from the 'user' table.
$user = User::all();
return $user;
}
}
************************************************
use App\Http\Traits\UserTrait;
class .....
{
use UserTrait;
public function ()
{
$data = $this->userdata();
we will get the data in $data variable.
}
}
Monday, 11 January 2021
What is traits?
Subscribe to:
Post Comments (Atom)
Machine Learning - Potato Leaf Disease Prediction
Step 1: import numpy as np import pandas as pd import splitfolders import matplotlib.pyplot as plt import tensorflow as tf from tensorflow i...
-
Step1: composer create-project laravel/laravel canpac Step2: cd canpac Step3: composer require laravel/ui Step4: php artisan ui bootst...
-
1. Create a middleware name PostShortCodeMiddleware 2. Add this middleware in the kernal file. 3. Create a shortcode.php and shorcodeurl.php...
No comments:
Post a Comment