duplicate data in blade when send data - laravel

i´m trying to show all my roles in my edit blade and with checkbox, that this checks it´s checked if this role have this permission.
my problem it´s when send data permissions and permissions to this role, all permission name it´s duplicate.
for set roles and permission i´m using laravel-permission/spatie
in my controller i have this:
public function edit($id)
{
$role = Role::find($id);
$permissions = Permission::all();
$permissionAssigned = Role::find($id)->givePermissionTo();
return view('opciones.roles.edit', ['role' => $role, 'permissions' => $permissions,
'permissionAssigned' => $permissionAssigned["permissions"] ]);
}
and in my blade i have this:
<h3>Permissions</h3>
<div class="row flex-row justify-content-center">
#foreach($permissions as $permission)
#foreach ($permissionAssigned as $asigned)
#if($permission->name == $asigned->name)
<div class="form-check p-4">
<input class="form-check-input" checked type="checkbox" name="permission[]" value="{{ $permission->name }}" id="flexCheckDefault">
<label class="form-check-label" for="flexCheckDefault">{{ $permission->name }}</label>
</div>
#endif
#endforeach
<div class="form-check p-4">
<input class="form-check-input" type="checkbox" name="permission[]" value="{{ $permission->name }}" id="flexCheckDefault">
<label class="form-check-label" for="flexCheckDefault">{{ $permission->name }}</label>
</div>
#endforeach
</div>
i don´t know that i´m doing wrong. I know that i have a logic problem, but i can´t solve it.
Thanks for readme and sorry for my bad english

You just need to pass the Role with associated Permissions and all Perrmissions to compare against, from the controller.
public function edit($id)
{
$role = Role::with('permissions')->find($id);
$permissions = Permission::all();
return view('opciones.roles.edit', ['role' => $role]);
}
In the view
<h3>Permissions</h3>
<div class="row flex-row justify-content-center">
#foreach($permissions as $permission)
<div class="form-check p-4">
<input type="checkbox"
id="flexCheckDefault"
class="form-check-input"
name="permission[]"
value="{{ $permission->name }}"
#checked((old($permission->name, $role->permissions->contains('name', $permission->name)) />
<label class="form-check-label" for="flexCheckDefault">{{ $permission->name }}</label>
</div>
#endforeach
</div>

Related

How to fill the form checkboxes with checked data from database laravel 5.8

I'm having a problem in my update Form,
I create user with multiple roles assigned using checkboxes in my form, and when I need to update a user i get all the data into my update form input fields except the checkbox fields will be empty. Is there a way that I can get the checkbox checked with the data from database ?
Here's my User Model Relationship
public function roles()
{
return $this->belongsToMany(Role::class)->withTimestamps();
}
Here's my Role Model Relationship
public function users()
{
return $this->belongsToMany(User::class)->withTimestamps();
}
Here's my Edit User Form
<form action="{{ route('users.update',['user'=>$user]) }}" method="POST">
#method('PATCH')
<div class="form-group">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" value="{{ old('name') ?? $user->name }}">
<span class="text-danger">{{ $errors->first('name') }}</span>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" name="email" class="form-control" value="{{ old('email') ?? $user->email }}">
<span class="text-danger">{{ $errors->first('email') }}</span>
</div>
<div class="form-group">
#foreach ($roles as $role)
<label for="role">{{ $role->name }}</label>
<input type="checkbox" name="role[{{$role->id}}]" value="{{ old($role->id) }}"
#if(in_array($role->id,old('role',[]) )) checked #endif >
#endforeach
<span class="text-danger">{{ $errors->first('role') }}</span>
</div>
#csrf
<button class="btn btn-primary" type="submit">Create</button>
</form>
Here's my Edit Function
public function edit(User $user)
{
$roles = Role::all();
return view('users.edit', compact('user', 'roles'));
}
I just wanted to know how can I pull data into my checkboxes from the database and mark them as checked
I guess you should check role id with the user role id:
#if(in_array($role->id, $user->roles->toArray())) checked #endif
Finally I found out the answer
<div class="form-group">
#foreach ($roles as $role)
<label for="role">{{ $role->name }}</label>
<input type="checkbox" name="role[]" value="{{ $role->id }}"
{{ $role->users->contains($user->id) ? 'checked' : '' }}
#if(in_array($role->id,old('role',[]))) checked #endif>
#endforeach
<span class="text-danger">{{ $errors->first('role') }}</span>
</div>

button disappeared after configuration laravel

hello my radiobutton disappeared after a laravel configuration they are used to modify user roles they worked for a moment and then disappeared
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Modifier <strong> {{ $user->name }}</strong></div>
<div class="card-body">
<form action="{{ route('admin.user.update', $user) }}" method="POST">
#csrf
#method('PATCH')
#foreach ($roles as $role)
<div class="form-group form-check">
#foreach ($user->roles as $userRole)
#if ($userRole->id == $role->id)
<input type="radio" class="form-check-input" name="roles[]" value="{{ $userRole }}" checked>
#else
<input type="radio" class="form-check-input" name="roles[]" value="{{ $userRole }}">
#endif
#endforeach
<label for="{{ $role->id }}" class="form-check-label">{{ $role->name }}</label>
</div>
#endforeach
<button type="submit" class="btn btn-primary">Modifier les roles</button>
</form>
You can use contains() with key and value defined:
The contains() method determines whether the collection contains a given item:
#foreach ($roles as $role)
<div class="form-group form-check">
<input type="radio" class="form-check-input" name="roles[]" value="{{ $role->id }}" {{ $user->roles->contains('id', $role->id) ? 'checked' : '' }} >
<label for="{{ $role->id }}" class="form-check-label">{{ $role->name }}</label>
</div>
#endforeach

Laravel Request Input is not usable data

Ok maybe this is a noob laravel question but when I'm trying to store data from a form I used a $request->input in a query to get a needed field for insert but the query will not run. Note: does run if I just set something like $project_id = 6.
public function store(Request $request)
{
$project_id = $request->input('project_id');
$company = Project::where('id', $project_id)->first();
if(Auth::check()){
$task = Task::create([
'name' => $request->input('name'),
'project_id' => $project_id,
'company_id' => $company->id,
'days' => $request->input('days'),
'hours' => $request->input('hours'),
'user_id' => Auth::user()->id
]);
if($task){
return redirect()->route('tasks.index')
->with('success' , 'Task created successfully');
}
}
return back()->withInput()->with('errors', 'Error creating new task');
}
Note:
I've tried a couple different things I've found online like $project_id = $request->project_id or $project_id = $request['project_id']
Is request->input just used for inserts and can't be used as a normal varible?
Update: here is the create.blade form it's coming from
#extends('layouts.app')
#section('content')
<div class="row col-md-9 col-lg-9 col-sm-9 pull-left " >
<h1>Add a Task </h1>
<!-- Example row of columns -->
<div class="col-md-12 col-lg-12 col-sm-12" style="background: white; margin: 10px;" >
<form method="post" action="{{ route('tasks.store') }}">
{{ csrf_field() }}
<div class="form-group">
<label for="project-name">Name<span class="required">*</span></label>
<input placeholder="Enter name"
id="project-name"
required
name="name"
spellcheck="false"
class="form-control"
/>
</div>
<div class="form-group">
<label for="task-days">Days Taken<span class="required"></span></label>
<input
id="task-days"
required
name="days"
type="number"
spellcheck="false"
class="form-control"
/>
</div>
<div class="form-group">
<label for="task-hours">Hours Taken<span class="required"></span></label>
<input
id="task-hours"
required
name="hours"
type="number"
spellcheck="false"
class="form-control"
/>
</div>
<input
class="form-control"
type="hidden"
name="project_id"
value="{{ $project_id }}"
/>
#if($projects != null)
<div class="form-group">
<label for="company-content">Select Project</label>
<select name="project_id" class="form-control">
#foreach($projects as $project)
<option value="{{$project_id}}">{{ $project->name }}</option>
#endforeach
</select>
</div>
#endif
<div class="form-group">
<input type="submit" class="btn btn-primary"
value="Submit"/>
</div>
</form>
</div>
</div>
<div class="col-sm-3 col-md-3 col-lg-3 col-sm-3 pull-right">
<div class="sidebar-module sidebar-module-inset">
<h4>Actions</h4>
<ol class="list-unstyled">
<li>All tasks</li>
</ol>
</div>
</div>
#endsection
Let's examine your <form> below:
<input class="form-control" type="hidden" name="project_id" value="{{ $project_id }}"/>
#if($projects != null)
<div class="form-group">
<label for="company-content">Select Project</label>
<select name="project_id" class="form-control">
#foreach($projects as $project)
<option value="{{ $project_id }}">{{ $project->name }}</option>
#endforeach
</select>
</div>
#endif
In this code, you have a hidden input with the name "project_id", and if $projects is not null, you also have a select with the name "project_id". Having multiple elements with the same name is invalid, and can cause issues.
Secondly, in this line:
<option value="{{ $project_id }}">{{ $project->name }}</option>
$project_id is the same value you have in the hidden input above. When you're looping over $projects, this should be $project->id:
<option value="{{ $project->id }}">{{ $project->name }}</option>
Lastly, make sure that $project_id is a valid value if you're going to send it, and consider adjusting your logic to only send the hidden input if $projects is null:
#if($projects != null)
<div class="form-group">
<label for="company-content">Select Project</label>
<select name="project_id" class="form-control">
#foreach($projects as $project)
<option value="{{ $project->id }}">{{ $project->name }}</option>
#endforeach
</select>
</div>
#else
<input class="form-control" type="hidden" name="project_id" value="{{ $project_id }}"/>
#endif
With all that adjusted, you should be able to retrieve the expected value with $request->input("project_id")

How to autofill Edit form with old Information from two different tables?

I have this form "Edit tache". Editing one "tache" affects two tables user and technicien, when i choose to edit tache the edit form is autofilled with information from the technicien table but the fields related to the table user are not autofilled. How could i autofill the form with information from the two tables? Thank you.
controller
public function edit($id)
{
$technicien=technicien::find($id);
$user = user::orderBy('id', 'asc')->get();
return view('technicien.edit',['moyenne_avis'=>$technicien],['actif'=>$technicien],['user_id'=>$technicien])->with('users', $user );
}
public function update(Request $request, $id)
{
// do some request validation
$technicien=technicien::find($id);
$technicien->update($request->all());
return redirect('technicien');
}
View
#extends('Layouts/app')
#extends('Layouts.master')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-10">
<h1>Modifier Tache</h1>
<form action="{{ route('technicien.update', $actif->id , $moyenne_avis->id ) }}" method="post">
{{csrf_field()}}
{{ method_field('PATCH') }}
<div class="form-group">
<label for="">Nom</label>
<input id="nom" type="text" class="form-control" name="nom" value="{{ old('nom') ? old('nom') : $actif->nom }}" required autofocus>
</div>
<div class="form-group">
<label for="">Prenom</label>
<input id="nom" type="text" class="form-control" name="nom" value="{{ old('nom') ? old('nom') : $actif->nom }}" required autofocus>
</div>
<div class="form-group">
<label for="">moyenne Avis</label>
<input type="text" name ="moyenne_avis" class="form-control"value ="{{$moyenne_avis->moyenne_avis}}" >
</div>
<div class="form-group">
<label for="">Etat Technicien</label>
<input type="text" name ="actif" class="form-control"value ="{{$actif->actif}}" >
</div>
<div class="form-group">
<input type="submit" value = "enregistrer" class="form-control btn btn-primary">
</div>
</form>
</div>
</div>
#endsection
route
Route::get('/technicien/{id}/edit', 'TechnicienController#edit');
Route::put('/technicien/{id}', 'TechnicienController#update')-
>name('technicien.update');
$user = user::find($id);
if they are retrieved using one id on the controller, else please tell the relation between them.
and in your view, i don't see a reason to check old since its not updated yet! use
{{$user->nom)}}

Storing registration data in database using laravel 5.4

Have been trying to create a user registration/login page following laracast video.
But when I try to register a user, it return to the same page without given any error as to why. I also noticed that the supplied info is not stored in the database.
Am so confused I don't know what am doing wrong you help will be really appreciated
Here is my view:
<div class="col-md-offset-1 col-md-8">
<h1 class="text-center ">Register</h1>
<form method="POST" action="register" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" required>
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" email="email" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" required>
</div>
<div class="form-group">
<label for="password_confirmation">Password Confirmation</label>
<input type="password" class="form-control" name="password_confirmation" required>
</div>
<button type="submit" class="btn btn-primary"><i class="fa fa-reply"></i>Register</button>
</form>
</div>
This is my controller
public function create()
{
return view('registration.create');
}
public function store(Request $request)
{
//validate form
$this->validate(request(),[
'name' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed',
]);
//create and save user
$user = new User; //create new Post model object
$user->name = $request->name; //adding thing to the the Post object
$user->email = $request->email;
$user->password = $request->password;
//save user
$user->save(); //to save the new item into the DB
//$user = User::create(request(['name', 'email', 'password']));
//sign them in
auth()->login($user);
//redirect to the admin
return redirect('/admin');
}
While this is my route file:
Route::get('register', 'RegistrationController#create');
Route::post('register', 'RegistrationController#store');
Thanks a lot in advance
Check your form, there's an error in the email field. It says:
and should be:
<input type="email" class="form-control" name="email" required>
So, replace email="email" with name="email"
first you add this error block. so, you can find error easily.
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
Then, your mistake is in form action. please replace it to below code.
<form method="POST" action="{{ route('register') }}" enctype="multipart/form-data">

Resources