Home > database >  Sending parameters in LARAVEL 5.8
Sending parameters in LARAVEL 5.8

Time:03-24

I need to send parameters to my "show" function in Laravel 5.8 via a select HTML tag but I am not able. I've been there for several days and I'm a little desperate.

In the action of the Form I put the following:

This is the route: {{route('users.show', ['id' => {{$list->id}}])}} but since the variable does not exist, it gives me error. Another option I considered is to use a link <a href='#'></a> where '#' is the route. But $list->id returns the last ID in the List in the Users table.

How can I capture the selected data in my drop-down list and send it as a parameter to the controller's show function?

Route::group(['middleware' => ['verified']], function () {
// Panel de control
Route::get('/home', 'HomeController@index')->name('home');
Route::resource('users', 'UserController');

This is the Controller:

public function index()
{
    #   Panel de control
    $log = Auth::id();
    $users = User::orderBy('created_at', 'asc')->where('id',$log)->with('imagenes')->get()->toArray();
    $users=Array_chunk($users,3,true);
    $imgBorrado = array();
    $imgB=null;
    //dd($imgBorrado);
    $user=User::find('id');

    $userslist=User::all();
    //$list=User::orderBy('created_at', 'asc')->get('id','name')->toArray();
    //dd($userslist);
    return view('users.index', compact('user','users','imgBorrado','imgB','userslist'));
}
public function show($id)
{ 
    #   Mostrar usuario con sus atributos
    $user=User::find($id);
    dd($id);
    return view('users.show', compact('user'));;
}

This is the view:

                    <form method="POST" action="#" id="selector" enctype="multipart/form-data">
                    @csrf
                    <label for="desplegable" >{{ __('Productor:') }}</label>

                        <div >
                        <select  id="desplegable" name='workers[]' required>
                          <option selected>...</option>
                          @foreach($userslist as $list)
                              <option value="{{$list->id}}">
                                  {{$list->name}}
                              </option>
                          @endforeach
                        </select>
                            
                            @if ($errors->has('id'))
                                <span  role="alert">
                                    <strong>{{ $errors->first('id') }}</strong>
                                </span>
                            @endif
                        </div>
                  </form>
                  </div>
                  <div >
                    <button type="button"  data-dismiss="modal">Cerrar</button>
                    <button type="submit"  form="selector" Value="desplegable">Seleccionar Perfil</button>
                  </div>

Thanks you!

CodePudding user response:

You cannot have access to the ID of the selected user in your <form> tag.

One option would be to catch the request in your show() method and then fetch the desired user through it.

Here is an example based on your code:

// In your Controller (don't forget to import Request facade)
use Illuminate\Support\Facades\Request

public function show(Request $request)
{ 
    $userId = $request->get('workers'); // Name of the select input

    $user = User::find($userId); 

    return view('users.show', compact('user'));
}

You can even validate the Request passed to the show() method. Read more about that here

CodePudding user response:

I add the modification here:

public function show(Request $request)
{ 
    #   Nombre del select (input)
    $userId = $request->get('workers'); 
    #   Mostrar usuario con sus atributos
    $usu=User::find($userId);
    foreach($usu as $user) {
        $user->name;
    }
    return view('users.show', compact('user'));;
}
  • Related