Home > Software design >  How to use @csrf to String in blade.php page from Laravel controller?
How to use @csrf to String in blade.php page from Laravel controller?

Time:07-20

@csrf is not hiding in Modal of this page After Click on Delete button showing error 419 | PAGE EXPIRED

'<form  action="psize/'.$psize->id.'" method="post">
        @csrf
        <input type="hidden" name="_method" value="DELETE" />
      <button type="submit"  onclick="return confirm("Are you sure?")">Del</button>
</form>'

CodePudding user response:

You need to concat @csrf

   '<form  action="psize/'.$psize->id.'" method="post">
    ' . @csrf . '
           <input type="hidden" name="_method" value="DELETE" />
        <button type="submit"  onclick="return confirm("Are you sure?")">Del</button>
    </form>'

CodePudding user response:

From what I can see, you are doing @csrf in PHP code which is not parsed by blade. You should instead use function csrf_token()

'<form  action="psize/'.$psize->id.'" method="post">
        ' . csrf_token() . '
        <input type="hidden" name="_method" value="DELETE" />
      <button type="submit"  onclick="return confirm("Are you sure?")">Del</button>
</form>'

CodePudding user response:

Yes, It is solved by replacing @csrf with <input type="hidden" name="_token" value="'.csrf_token().'" />

'<form  action="psize/'.$psize->id.'" method="post">
    <input type="hidden" name="_token" value="'.csrf_token().'" /> 
           <input type="hidden" name="_method" value="DELETE" />
        <button type="submit"  onclick="return confirm("Are you sure?")">Del</button>
    </form>'

In Controller Function Destory()

function destroy(Request $request, $id)
    {
        $token = $request->session()->token();
        //....
    }

Thanks Laravel Docs

  • Related