Use a pageNation to request the value of the page to the controller. But why can't any parameters reach the controller?
Route::get('/index', 'Penpal\ViewController#index')->name('penpal.index');
<form action="{!! route('penpal.index', ['menu' => 'p11-c3']) !!}" method="get">
<select id="inputState" class="form-control" style="height:35px; width:80%" name="pagination" onchange="this.form.submit()">
<option value="3">#lang('penpal/component/indexMenu.twelve')</option>
<option value="4">#lang('penpal/component/indexMenu.twenty_four')</option>
<option value="5">#lang('penpal/component/indexMenu.thirty_six')</option>
</select>
</form>
public function index (Request $request){
return $request;\
}
A parameter named "menu" cannot be received from the controller.
Your <form> is using method='get', instead of method='POST' (which is used to post data to the request via a form.
You will also need to use #csrf in your blade template or you will not be able to post data:
<form action="{!! route('penpal.index', ['menu' => 'p11-c3']) !!}" method="POST">
#csrf
<select id="inputState" class="form-control" style="height:35px; width:80%" name="pagination" onchange="this.form.submit()">
<option value="3">#lang('penpal/component/indexMenu.twelve')</option>
<option value="4">#lang('penpal/component/indexMenu.twenty_four')</option>
<option value="5">#lang('penpal/component/indexMenu.thirty_six')</option>
</select>
</form>
Finally, make sure that your route is a ::post() route.
Use Post method both route and form
<form action="{!! route('penpal.index', ['menu' => 'p11-c3']) !!}" method="post">
Route::match(['get','post'],'/index', 'Penpal\ViewController#index')->name('penpal.index');
You haven't set any route parameters for your route and neither passing any to your controller method. And it would be a better idea to use POST then GET.
Change this to
Route::get('/index', 'Penpal\ViewController#index')->name('penpal.index');
this
Route::post('/index/{menu?}', 'Penpal\ViewController#index')->name('penpal.index');
and your form
<form action="{{ route('penpal.index', ['menu' => 'p11-c3']) }}" method="POST">
#csrf
And in your controller method you can fetch the passed parameter
public function index (Request $request, $menu){
print_r($menu);
}
Related
I am doing roles&permissions, an action that admin can change user to staff. I am able to send id to editRolePermission function, but the role value.
function editRolePermission($id, "role value here")
{
$row = DB::table('users')
->where('id',$id)
->limit(1)
->update(array('role' => ''));
return redirect()->back();
}
<form action="{{ route('updateRolePermission', $user->id) }}" method="POST">
#method('PATCH')
#csrf
<select name="roles">
<option name ="user" value="user">User</option>
<option name= "staff" value="staff">Staff</option>
</select>
<input type="submit" onchange="this.form.submit()">
</form>
Route::patch('edit-role-permission/{id}', [AdminController::class, 'editRolePermission'])->name('updateRolePermission');
The following should do what you want. Here is the route - notice I have swapped {id} for {user} - this is the ID of the user we need to edit the role for:
Route::post("/edit-role-permission/{user}", [AdminController::class, "editRolePermission"]);
How to implement the route in your form:
#if(session()->has("message"))
{{ session("message") }}
#endif
<form action="/edit-role-permission/{{ $user->id }}" method="POST">
#csrf
<select name="roles">
<!-- ... options ... -->
</select>
<!-- submit button -->
</form>
Thanks to Laravel magic, passing the user ID as the second parameter allows us to access the user model so we can update the user easily, with a lot less code. We can use the request to get any posted values, in this instance $request->roles refers to the input named roles:
public function editRolePermission(Request $request, \App\Models\User $user)
{
$user->update(["role" => $request->roles]);
$user->save();
return redirect()->back()->with("message", "User role updated successfully");
}
The submit form which allows admin to change the role of user or staff, error shows Missing required parameter for [Route: updateRolePermission] [URI: admin/edit-role-permission/{id}] [Missing parameter: id] I have fighting with this issues for many hours, everyone can help thanks!!!!!
<form action="{{ route('updateRolePermission'), ['id' =>$user->id] }}" method="POST">
#csrf
<select name="roles">
<option value="user">User</option>
<option value="staff">Staff</option>
</select>
<input type="submit">
</form>
Route::group(['prefix'=>'admin', 'middleware'=>['isAdmin','auth']], function(){
Route::get('dashboard', [AdminController::class, 'index'])->name('admin.dashboard');
Route::get('role-permission', [AdminController::class, 'rolePermission'])->name('admin.rolePermission');
//it doesnt work!!!!
Route::get('edit-role-permission/{id}', [AdminController::class, 'editRolePermission'])->name('updateRolePermission');
});
function editRolePermission($id)
{
$row = DB::table('users')
->where('id',$id)
->limit(1)
->update(array('role' => 'fdas'));
return redirect()->back();
}
Change this line:
action="{{ route('updateRolePermission'), ['id' =>$user->id] }}"
to this:
action="{{ route('updateRolePermission', $user->id) }}"
First your route is GET method while your form is POST method.
For the $id, you may get it in your controller by:
$id = \Route::current()->parameter('id');
I am trying to update some values and it's creating a new one instead of updating the selected data
this is the controller code
public function update(Request $request, Payment_Student $payment_Student)
{
$payment_Student->date =request('date');
$payment_Student->amount =request('amount');
$payment_Student->formation_id =request('formation_id');
$payment_Student->student_id =request('student_id');
$payment_Student->save();
return redirect()->route('payment.index');
}
This is my route code
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/search','CategoryController#search');
Route::resource('/category','CategoryController');
Route::resource('/formation','FormationController');
Route::resource('/professor','ProfessorController');
Route::resource('/student','StudentController');
Route::resource('/classroom','ClassroomController');
Route::resource('/session','SessionController');
Route::resource('/payment','PaymentController');
Route::resource('/seance','SeanceController');
Route::resource('/paymentprof','PaymentProfessorController');
Route::resource('/paymentstudent','PaymentStudentController');
Route::resource('/presence','PresenceController');
Route::resource('/profile','ProfileController');
This is my balde view code
<form method="POST" enctype="multipart/form-data" action="{{route('paymentstudent.update',$payment_Student->id)}}" class="form-horizontal">
{{method_field('PATCH')}}
#csrf
Date:
<br/>
<input class="form-control" type="date" name="date">
<br/>
Amount:
<input type="number" value="{{$payment_Student->amount}}" name="amount"
class="form-control">
<br/>
Formation:
<select class="form-control" name="formation_id">
#foreach($formation as $formation)
<option value="{{$formation->id}}">{{$formation->name}}</option>
#endforeach
</select>
<br/>
Student:
<select class="form-control" name="student_id">
#foreach($student as $student)
<option value="{{$student->id}}">{{$student->lastname}}</option>
#endforeach
</select>
<div class="card-footer">
<button type="submit" class="btn btn-primary">Save Changes</button>
</div>
<br>
</form>
when I select a data to update it creates new data the updated value that I have inserted
By default when you make a resource laravel will singularize the resource name and make a variable out of it.
Example: Route::resource('videos', 'VideoController') laravel will expect the variable $video
In your case, paymentstudent will probably give the variable $paymentstudent
Fortunatly, you can customize your own parameter name
Route::resource('paymentstudent', 'PaymentStudentController', ['parameters' => [
'paymentstudent' => 'payment_Student'
]]);
Fetch the eloquent model from the one passed in the route and update accordingly, since you're using route resource, pass the actual object in the route by named parameter
<form method="POST" enctype="multipart/form-data" action="{{ route('paymentstudent.update', ['paymentstudent' => $payment_Student]) }}"
public function update(Payment_Student $payment_Student)
{
$payment_Student = Payment_Student::find($payment_Student->id);
$payment_Student->date = request('date');
$payment_Student->amount = request('amount');
$payment_Student->formation_id = request('formation_id');
$payment_Student->student_id = request('student_id');
$payment_Student->save();
return redirect()->route('payment.index');
}
I have a view whit the next code:
<h1>Edicion</h1>
#foreach ($usuarios as $usu)
<h4>{{$usu->nombre}}</h4>
editar
#endforeach
This route:
Route::resource('/prurequests','PruebasControllers\PrurequestsController');
The method edit:
public function edit($slug)
{
$usuario = Usuario2::where('slug','=',$slug)->firstOrFail();
return view('vistaspruebas.edit', compact('usuario'));
}
In this route my URL is:
/public/prurequests/vaca/edit
The code in this view 'vistaspruebas.edit' is:
<form action="prurequests/suma" method="POST">
#method('PUT')
#csrf
<label for="nombre">ingrese nombre</label>
<input type="text" name="nombre" value="{{$usuario->slug}}">
<br />
<button type="submit" name="" value="submit">Actualiza</button>
</form>
instead of looking for this route: "prurequests/suma " Laravel look for /public/prurequests/vaca/prurequests/suma
someone knows why after the tag and call other route it removes 'edit' and change it by and other route i put here?
Please use action() helper method in your form as the following:
<form action="{{ action('Controller#method') }}" method="POST">
Or you can use the route helper method as the following:
<form action="{{ route('route_name') }}" method="POST">
Also you can use url() helper if you want to use a given path:
<form action="{{ url('path_here') }}" method="POST">
I have a form like this:
<form class="form-horizontal" role="form" method="POST" name="myform" id="myform" action="{{ url('/mypage/') }}">
{{ csrf_field() }}
<div id="somecontent">
</div>
<div id="abutton">
</div>
</form>
Then some Jquery like this:
$('#somecontent').html('<select class="form-control" id="myselect" form="my"><option value="1">Hello</option><option value="2">World</option></select>');
And then I add a button like this:
button = $('<button type="submit" form="myform" class="btn btn-theme">Send</button>');
$('#abutton').html(button);
And I also change the action dynamically:
$("#myform").attr("action","/mypage/" + item_id);
Then I got this in the web file:
Route::post('/mypage/{item_id}','mycontroller#do_something');
And then do this in the controller:
public function do_something($item_id,Request $request){
dd($request);
}
But the $request is empty, it does not contain the value selected in the dynamically generated select.
Any ideas why?
you haven't added name attribute of select, add name attribute and see it will populate into $request.
Try below code.
$('#somecontent').html('<select name="select_name" class="form-control" id="myselect" form="my"><option value="1">Hello</option><option value="2">World</option></select>');