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

clin407's avatar

Accessing requests from within blade template?

Currently I have the following requests class (strip out logic)

<?php namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Redirect;
use Laracasts\Flash\Flash;

abstract class Request extends FormRequest {

    //
    public function canView($user_id = false)
    {
      logic
    }
    public function canModify($user_id = false)
    {
      logic
    }

    public function forbiddenResponse()
    {
        Flash::error("You do not have proper authorization to view/manage the page");
        return redirect()->back();
    }
}

I use it for my form validation but I wanted to know if I could use the same methods in my blade templates? In particular, I have a list of records and I want to be able to use the canModify method to make it so an edit link does or does not show up. I thought if I placed into my helpers.php file

function canModify($user = false) {
    if(!$user) {
        return false;
    }
    return App\Http\Requests\Request::canModify($user);
}

then did {{ canModify($user) }} in my blade template, it would work but it gives me the following error:

ErrorException in helpers.php line 439:
Non-static method App\Http\Requests\Request::canModify() should not be called statically (View: /home/vagrant/site/resources/views/list.blade.php)

What's the best way to go about this?

0 likes
7 replies
webkenny's avatar

I don't want to throw you too far off track here but you might consider a small pivot in your direction. There's a nifty library called Entrust that can handle roles and/or permissions in a Laravel project. This may help you as you build out the various things users can do. This way, if more than one user needs to have the Edit permission you'll be able to support it. And then you don't have to go creating all this logic. It's really easy to use!

If you take that library, define your permissions and roles, then use its Facade in a Blade template you'll be able to pull this off nicely. Let's say you had a permission called edit-post that permits the user to access and submit your edit form. It might look like this:

@if (Entrust::can('edit-post'))
    <a href="my/edit/path">Edit</a>
@endif 

Hope this helps you!

P.S. Don't forget to also apply the check to your route or someone could obviously just directly navigate to it. Good news! Entrust supports that too. :)

1 like
clin407's avatar

Hey @webkenny, I actually use Entrust already but my specific methods for canModify/canView take groups into account. So canModify basically checks if the user owns the particular record or if the user is an admin and returns true and canView checks if the user is part of the same group or if the user is an admin and returns true as well.

jimmck's avatar

@clin407 I think if you move helper canModify code into your Blade file it will solve your problem. Just follow @webkenny example code. Facades need a class instance to work.

1 like
bimalshah72's avatar
Level 6

Hi,

I think FormRequest is only for authenticating and validating on server side. Also you never be able to create instance of "Request" class in your code, since it is an abstract class. As per your issue - it won't access statically either.

Best approach for this is create trait ..

namespace App\Models;
use Auth;
// other uses

trait UserPermissions
{

    public function canView()
    {
        logic
    }
    public function canModify()
    {
        $user_id =  Auth::User()->id;
        //or do your logic
    }

    public function forbiddenResponse()
    {
        Flash::error("You do not have proper authorization to view/manage the page");
        return redirect()->back();
    }
}

Use this trait in your User Model..

use UserPermissions;

And in your blade ..for the record in foreach loop..

@if (Auth::User()->canModify())
                <a href="">Edit </a> 
@endif

The same mechanism also you can use in controller or -- you have to create another FormRequest which extends above "Request" class and put your logic thier (i.e. Auth::User()->canModify())

2 likes
clin407's avatar

@bimalshah72 I went with your method and it worked perfectly. Only issue now is there's two locations with the same methods (one is located in Requests.php and the other is the new trait I just made). Is there a way so the form validations can utilize the traits? In my authorize method, I am utilizing the canView/canModify forms as such...

public function authorize()
    {
        switch ($this->method()) {
            case 'POST':
            {
                return true;
            }
            case 'GET':
            {
                return $this->canView($this->route()->parameter('post')->user_id);
            }
            case 'PUT':
            case 'PATCH':
            case 'DELETE':
            {
                return $this->canModify($this->route()->parameter('post')->user_id);
            }
            default:break;
        }
    }
bimalshah72's avatar

You need to remove all canModify code etc from the FormRequest, only keep it in trait ..

and do ... (implement in FormRequest, same as blade)

public function authorize()
    {
        ....
        return \Auth::User()->canModify()
    }

Please or to participate in this conversation.