Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

GodziLaravel's avatar

How to use middleware in blade ?

Hello ,

I would like to use some middlewares in the blade view ,

These middlewares are used already in the route :

    Route::view('/companies', 'irp.companies.companies')->middleware(['verified','companies_view'])->name('Companies');
    Route::view('/deals', 'irp.deals.deals')->middleware(['verified','projects_view'])->name('Deals');
    Route::view('/tasks', 'irp.tasks.tasks')->middleware(['verified','tasks_view'])->name('Tasks');

now in sidebar.blade.php I would like to show the related links to the above routes only when user has the permission (conditions):

                    <a class="nav-link " href="/companies">
                        <i class="nav-icon fa fa-industry"></i> Companies
                    </a>


                    <a class="nav-link " href="/deals">
                        <i class="fa fa-cubes" aria-hidden="true"></i> Projects
                    </a>


                    <a class="nav-link " href="/tasks">
                        <i class=" nav-icon fa fa-tasks" aria-hidden="true"></i> Tasks
                    </a>

0 likes
7 replies
Snapey's avatar

Agreed. Not a job for middleware.

1 like
GodziLaravel's avatar

@nakov Thanks it works ,

Another question , how I could use this in VueJS for example :

                            <button :disabled="findInMembers(row.id)"
                                    class="btn btn-square btn-success align-center"
                                    slot="actions" slot-scope="{row}"
                                    type="button"
                                    v-on:click="addMembersToList(row)">
                Add
                            </button>

let's say that this button is only shown if @can('companies_view')

GodziLaravel's avatar

@nakov

Thanks again ,

This bellow works for me but when the {{ Gate::allows('tasks_edit') }} returns false I have this error

[Vue warn]: Error compiling template:

The value for a v-bind expression cannot be empty. Found in "v-bind:auth-user"

    <div class="py-1">
        <div class="row justify-content-center">
            <div class="col-md-12">
                <tasks
                :auth-user="{{ Gate::allows('tasks_edit') }}"
                ></tasks>
            </div>
        </div>
    </div>

How to do when It's false return false instead of empty ?

Nakov's avatar

@mostafalaravel have you tried using ternary? Like this:

:auth-user="{{ Gate::allows('tasks_edit') ? true : false }}"

// or

:auth-user="{{ Gate::allows('tasks_edit') ?: false }}"
1 like
GodziLaravel's avatar

Update : I added one condition , and it seems working !

    <div class="py-1">
        <div class="row justify-content-center">
            <div class="col-md-12">
                <tasks
                :auth-user="{{ (Gate::allows('tasks_eddit'))?'1':'0' }}"
                ></tasks>
            </div>
        </div>
    </div>

Please or to participate in this conversation.