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