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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners