What is traits in PHP with example
In Laravel we will learn about Traits and how to upload image in folder using traits.
What are Traits?
PHP only support single inheritance (a child class can inherit only from one single parent). But if we want to use multi-inheritance (if a class needs to inherit multiple behaviours) then PHP OOP Traits solve this problem.
Traits is used to declare methods that can be used in multiple classes, and it can have methods and abstract methods that can be used in multiple classes, and the methods can have any access modifier (public, private, or protected).
Note: We can declare Traits with the trait
keyword.
Here a small example of trails just for understand. We will learn more to next point.
Example.
<?php
trait class1 {
public function msg1() {
echo "OOP is fun! ";
}
}
Class class2 {
use class1;
}
$obj = new class2();
$obj->msg1();
?>
How to upload image in Laravel using Traits
In Laravel we will upload image in public folder using traits, so there are given few step we should follow:
- First Create trait folder inside the app folder.
We can create Traits file“saveFile.php”
inside“App\Traits”
folder and follow the below code.
<?php
namespace App\traits;
trait saveFile{
protected function saveImage($file){
$image_name= time().'.'.$file->extension(); // get image name
$file->move(public_path('images/'), $image_name); // move image to public image folder
return $image_name;
}
}
-
Now create Blade file as per your choice
We are going to create blade“blog.balde.php”
file inside views folder and create form for upload image. In form action“saveImageTrait”
route we will create next part.
<div class="container">
<h2>Upload Image Via Traits</h2>
<label for="email">Image Upload</label>
<form action="{{url('saveImageTrait')}}" method="post" enctype="multipart/form-data">
@csrf
<div class="form-group">
<input type="file" class="form-control" name="image">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
- Create Controller and call traits.
Now we will create a controller and call traits function , you can create controller using given below command.
php artisan make:controller BlogController
Now put the given code into it
<?php
namespace App\Http\Controllers;
use App\Models\blogs; // add Model
use Illuminate\Http\Request;
use App\Traits\saveFile; // Declare traits
class BlogsController extends Controller
{
use saveFile; // use traits
public function saveImageTrait(Request $request){
/* saveImage() call from traits class */
$imageName=$this->saveImage($request->image);
print($imageName);
/* You can write code for save the image name */
}
// For call blade file upload image
public function index(){
return view('blog');
}
}
- Now create Route
We can create route for call blade file form and save image to folder follow then give code.
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BlogsController;
# call blade file
Route::get('blogs', [BlogsController::class, 'index']);
# save image to folder
Route::post('saveImageTrait',[BlogsController::class, 'saveImageTrait']);
Now you can see image in "public/image"
folder
Latest Post
Upload Image in MySQL database using Node js and React js
How to add new column to existing table without losing data in Laravel
Laravel Eloquent display query log in php
What is traits in PHP with example
How to disable click outside modal to close modal in bootstrap
Auto refresh code in HTML using meta tags
How to Optimize the Speed & Performance of a Laravel Website