How to Create Traits in Laravel 9

How to Create Traits in Laravel 9?

How to Create Traits in Laravel 9?

In this post, we will give you information about How to Create Traits in Laravel 9?. Hear we will give you detail about How to Create Traits in Laravel 9? And how to use it also give you a demo for it if it is necessary.

In this article, I will share with you what is trais in laravel and how it used in our laravel application? Here in this article, i will give you one simple example of creating your own traits in your laravel application and how it will be used in your laravel application. but the first thing we will know why traits are defined in laravel?

What is Traits?

One of the major problems in PHP you can inherit only a single class with any other class. This means a class can only inherit from one other class. For example, it might be desirable to inherit methods from a couple of different classes in order to prevent code duplication. In PHP 5.4 a new feature of the language was added known as Traits and is used extensively in the Laravel Framework.

Step – 1 : Create Traits

In our first step, we need to create appTraitsStoreImageTrait.php a file as a traits in laravel. and just put the following code into it.

namespace AppTraits;

use IlluminateHttpRequest;

trait StoreImageTrait {
    public function verifyAndStoreImage( Request $request, $fieldname = 'image', $directory = 'unknown' ) {
        if( $request->hasFile( $fieldname ) ) {
            if (!$request->file($fieldname)->isValid()) {
                flash('Invalid Image!')->error()->important();
                return redirect()->back()->withInput();
            return $request->file($fieldname)->store('image/' . $directory, 'public');
        return null;

Step – 2 : How to use Traits in Controller

After, creating our traits then now we will see how to use our created traits in our laravel application controller.

namespace AppHttpControllersAdmin;

use AppModelsAdminPost;
use IlluminateHttpRequest;
use AppTraitsStoreImageTrait;

class PostController extends Controller
    use StoreImageTrait;

    public function store(CreatePostRequest $request)
        $formInput = $request->all();
        $formInput['image'] = $this->verifyAndStoreImage($request, 'image', 'post');
        $post = Post::create($formInput);
        flash('Post saved successfully.')->success();
        return redirect()->back();

i hope this article will help you.

Hope this code and post will helped you for implement How to Create Traits in Laravel 9?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US