Issue
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" class="col-md-4 col-form-label text-md-right">{{ __('Productor:') }}</label>
                        <div class="col-md-6">
                        <select class="form-control{{ $errors->has('id') ? ' is-invalid' : '' }}" 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 class="invalid-feedback" role="alert">
                                    <strong>{{ $errors->first('id') }}</strong>
                                </span>
                            @endif
                        </div>
                  </form>
                  </div>
                  <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
                    <button type="submit" class="btn btn-primary" form="selector" Value="desplegable">Seleccionar Perfil</button>
                  </div>
Thanks you!
Solution
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
Answered By - rachids
 
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.