Traits are basically a peice of code where we reuse wherever we need it , like we need to save image in different controllers so what we do , we actually use move command and save the image , whcih results in repetive same code then we use traits. Hence traits are like objects where you can't direct call them , basically they are public protected functions which are mostly call by inside a function .
Here we are consider an example of api response.
For creating trait create a folder manually , traits inside app like this app/Traits and inside this traits make a file ApiResponse.php
As shown below we are using an API response for success() and error() method which sends the response in json format by calling them from controller , paste the following code in app/Traits/ ApiResponse.php code for traitsCopy
We are Vineet Maanas we are working as professional developers from the last 8 years and always looking to discover something amazing getting followed by market trends and always push new developers to learn something new with our tutorials
Thankyou.
0 Comments (Please let us know your query)