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");
}
Related
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 have three user roles: Student, Teacher, Admin.
If a user is a student I want to display the option to Elevate them to either Teacher or Admin in a dropdown and the same for the other roles eg. Teacher gets Admin and Student option.
Currently this looks like this:
Index
#if ($user->role == 'admin')
<form action="/admin/users/{{ $user->id }}" method="POST">
#csrf
#method('PATCH')
<input class="hidden" name="role" value="user">
<button>Make User</button>
</form>
#else
<form action="/admin/users/{{ $user->id }}" method="POST">
#csrf
#method('PATCH')
<input class="hidden" name="role"value="teacher">
<button>Make Teacher</button>
</form>
#endif
Controller
public function update(User $user)
{
$attributes = request()->validate([
'role' => ['required', Rule::exists('users', 'role')]
]);
$user->update($attributes);
return back()->with('success', 'User Updated!');
}
How would I be able to do an if statement to list all the roles except for the current role assigned to the user or perhaps this is better done in the controller instead?
And is it possible to put the role in a slug and then run a foreach so I don't repeat code?
Im new top Laravel so any help would be appreciated :)
Thanks
Provide all roles to your page.
Add to User hasRole() method if it does not exists.
Use FormRequest for validation.
Don't forget to get roles with a $user to avoid n+1 queries with User::with('roles')->find($id).
#foreach($allRoles as $role)
#if(! $user->hasRole($role))
Your form
#endif
#endforeach
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');
}
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);
}
i have a users list & here i put a option to assign role to each users, for that when click 'Add Role' link it will show this route
Route::get('admin/home/role/{user}', 'RoleController#create');
in create function my form code is...
<form method="post" action="{{ url('admin/home/role') }}">
{{ csrf_field() }}
<div class="form-group">
<select name="role" class="form-control" >
<option value="1"> Admin </option>
<option value="2"> Editor</option>
</select>
</div>
<button type="submit" class="btn btn-primary">
Add Role
</button>
</form>
to manage this form my POST route is...
Route::post('admin/home/role', 'RoleController#store');
now how to insert this form request data into role_user table? oh! i have already 3 table, users, roles & role_user.
User model relationship code is...
public function role()
{
return $this->belongsToMany(Role::class, 'role_user');
}
Role model relationship code is...
public function user()
{
return $this->belongsToMany(User::class, 'role_user');
}
my question is how to insert form request data into role_user table?
i know one way that is...
public function store(Request $request, User $user)
{
$role = Role::find(1);
$user = User::find(19);
$role->user()->attach($user);
}
it works, but this is not dynamic. How to insert by form request? please help me. I searching about this topic tutorial but not found.
you need to send Role id and user id value to store function
public function store(Request $request)
{
$user_id=$request->input('user_id'); // get user id from post request
$role_id=$request->input('role_id'); // get Role id from post request
/* Todo request validation*/
$user = User::find($user_id);
$role = Role::find($role_id);
$user->roles()->attach($role);
}
view :
<form method="post" action="{{ action('RoleController#store') }}">
{{ csrf_field() }}
<input type="hidden" name="user_id" value="{{ $user->id }}" /> // you need to pass $user to this view
<div class="form-group">
<select name="role_id" class="form-control" >
<option value="1"> Admin </option>
<option value="2"> Editor</option>
</select>
</div>
<button type="submit" class="btn btn-primary">
Add Role
</button>
</form>