Using traits is one of the best practices alongside OOP(Object-Oriented Programming) and [SOLID Principles] in PHP. Traits are a mechanism for code reuse in single inheritance languages such as PHP. A trait is intended to reduce some limitations of single inheritance by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

We will create one trait ImageTrait and in that trait we will write code for image upload.

app/Http/Controllers/ItemController.php use App\Traits\ImageTrait; Exit fullscreen mode at the top part use ImageTrait; Exit fullscreen mode inside the controller class $input['image'] = $this->verifyAndUpload($request, 'image', 'images'); Exit fullscreen mode using the function inside the trait
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners