Ad

How To Use Laravel Blade Directives For Api

i want to use angular and laravel for our project in that i am using spatie library i want to know that is there any way that you can use laravel blade directives like i want to use @hasrole , @role etc in blade or do you know how to use any other blade directives like @if, @foreach i know you can manage it in the angular but my main doubt is about how to use spatie @hasrole, @role etc for api's please tell me if you hit with any idea its like

 @role('individual')
      <h5>Only individual can see this </h5>
 @endrole
 @role('agent')
      <h5>Only agent can see this</h5>
 @endrole

i want to all this functionality on api any suggestions?

Ad

Answer

Do you want to use Laravel Blade and Angular in your project? If so, I would advice to not use Laravel Blade at all to render HTML since Angular is doing it. Laravel, with Blade, is a backend and frontend framework, which means that it is able to compute and render the pages. Angular is only frontend: it uses data received from a remote source to build its pages. To put it in a nutshell, in my opinion, using Laravel with Blade and Angular besides is counterproductive.

But, if you only want to use annotations like you can find in Laravel Blade but in Angular, you can't. Angular, as I said, is decorrelated from the backend, it uses only data received to render the HTML. By this, I mean that you have two parties: Laravel (backend) and Angular (frontend). Angular need to have access to a variable which contains the role. There is many way to accomplish this (JWT for example). If I assume that you succeed to have the role somewhere in your Angular app, you can control the view like this:

<h5 ng-if="role === 'individual'">
    ...
</h5>
<h5 ng-if="role === 'agent'">
    ...
</h5>

You can see the Angular documentation here. There is another directive that is used the same way but have a different behaviour: ngShow.

For further help, I found an article that can show you how to communicate between Laravel and Angular using tokens: https://scotch.io/tutorials/token-based-authentication-for-angularjs-and-laravel-apps

Ad
source: stackoverflow.com
Ad