Home > Software engineering >  Trying to send a variable to a route from a button
Trying to send a variable to a route from a button

Time:04-17

I want to turn the tag with a user's name into a button and send it's id to an edit file.

    <div >
        <button  href="{{  route('users.edit', ['id'=>Auth::User()->id])}}">
            <span >
                {{ Auth::user()->name }}
            </span>
        </button>
    </div>

I tried to do it with Auth::User()->id, but the button doesn't do anything and I can't figure out what is wrong.

CodePudding user response:

use <a></a> instead of <button></button> example :

<div >
        <a  href="{{  route('users.edit', ['id'=>Auth::User()->id])}}">
            <span >
                {{ Auth::user()->name }}
            </span>
        </a>
 </div>

if you want it to look like a button add btn class .

i hope it was useful !

CodePudding user response:

If you are willing to use button on there use;

    <div >
        <form id="YOUR_FORM_ID" action="{{  route('users.edit', ['id'=>Auth::User()->id])}}" method="YOUR_METHOD">
            @csrf
        </form>
        <button  type="submit" form="YOUR_FORM_ID">
        <span >
            {{ Auth::user()->name }}
        </span>
        </button>
    </div>

Don't forget to change YOUR_FORM_ID and YOUR_METHOD. Or else @Joukhar's formula will work.

  • Related