Today i am going to discuss about the laravel macro
Macro
is the powerful feature of the laravel framework. Macros allow you to add custom functionality to internal Laravel components. This laravel macro also work with laravel 5.8 , 5.7 and 5.6 .
Let’s see Laravel Macro
Example 1
Let’s say you are working with the api and want the below response on success
{ "success": true, "msg": "Your message", "data": Array() }
To achieve the above response. Open the AppServiceProvider.php
file and put the below code
Request::macro(‘success’, function ($msg, $data) { Request::json([ "success" => true, "msg" => $msg, "data" => $data ]); });
And then use the above defined method
Request::success('Your message', Array());
Example 2
Let’s see another example. Suppose you want to create a new method which we will use at the time of fail
Open the AppServiceProvider.php
file and put the below code
Request::macro(‘fail’, function ($msg, $data) { Request::json([ "success" => false, "msg" => $msg, ]); });
And then use
Request::fail('Your message');
Will return
{ "success": false, "msg": "Your message" }
You can create another macro
according to your requirement using laravel macro
Which components are “Macroable”?
Macros can be defined on any class with the Macroable
trait. Below is a list of Macroable facades & classes:
Facades
- Cache
- File
- Lang
- Request
- Response
- Route
- URL
Illuminate Classes
- Illuminate\Routing\UrlGenerator
- Illuminate\Cache\Repository
- Illuminate\Validation\Rule
- Illuminate\Console\Scheduling\Event
- Illuminate\Database\Eloquent\Builder
- Illuminate\Database\Eloquent\Relation
- Illuminate\Database\Query\Builder
- Illuminate\Filesystem\Filesystem
- Illuminate\Foundation\Testing\TestResponse
- Illuminate\Http\RedirectResponse
- Illuminate\Http\Request
- Illuminate\Http\UploadedFile
- Illuminate\Routing\ResponseFactory
- Illuminate\Routing\Router
- Illuminate\Support\Str
- Illuminate\Support\Arr
- Illuminate\Translation\Translator
- Illuminate\Support\Collection
Uwielbiając informacje na tej stronie, wykonałeś świetną robotę przy artykułach. https://samocholand.pl/samochody/
Bardzo podoba mi się twoja strona. Jeżeli chcesz się lepiej ze mną poznać i potrzebujesz odżywek lub suplementów diety na masę sprawdź mój blog! http://www.tabletkinamasesterydynmez.pl
I happen to be writing to make you understand what a terrific experience our princess undergone visiting yuor web blog. She picked up such a lot of things, with the inclusion of what it is like to possess an incredible teaching style to make the others without hassle comprehend a number of complex issues. You actually did more than her desires. I appreciate you for coming up with such effective, dependable, edifying and cool guidance on that topic to Ethel. https://harrington-service.com/
I enjoy you because of all of the effort on this blog. Betty delights in carrying out internet research and it’s easy to see why. A lot of people hear all about the compelling form you convey both interesting and useful guidance by means of the blog and attract contribution from other individuals about this area and my child has always been becoming educated a great deal. Enjoy the remaining portion of the new year. You’re carrying out a brilliant job. https://bupronbupropion.com/
I am also writing to make you know what a impressive encounter my friend’s girl gained reading through your web page. She learned plenty of pieces, which include what it’s like to possess an amazing helping mood to let most people smoothly know several impossible subject matter. You truly exceeded our own expectations. Thank you for offering these invaluable, safe, explanatory and even fun tips about this topic to Julie. https://pamelornortriptyline.com/
My spouse and i got quite happy that John could deal with his preliminary research through the ideas he grabbed out of your web page. It’s not at all simplistic to just continually be giving out points that many others have been selling. And we also fully understand we have got the website owner to be grateful to for that. The main explanations you have made, the simple site menu, the friendships you can help foster – it’s got many impressive, and it’s really helping our son and the family imagine that the matter is excellent, and that is pretty indispensable. Thank you for everything! https://tofranilimipramine.com/#
What’s up with the new WordPress dashboard??? I don’t know how to work it!!!.