I´m traying to create list of checkbox in my blade with all roles that i have in my DB. I´m doing loop to get all roles and if to check that roles have this user that i´m doing edit:
#foreach($roles as $rol)
#foreach ($selRoles as $role)
#if ($role == $rol->id)
<div class="col-md-4">
<input type="checkbox" name="rol" checked=checked class="form-check-input" value="{{ $rol->id }}" id="{{ $rol->id }}">
{{ $rol->name }}
</div>
#else
<div class="col-md-4">
<input type="checkbox" name="rol" class="form-check-input" value="{{ $rol->id }}" id="{{ $rol->id }}">
{{ $rol->name }}
</div>
#endif
#endforeach
#endforeach
With this code, my problem it´s that all my roles it´s duplicated. In my controller i have this:
public function edit(User $usuario)
{
$roles = Bouncer::role()->orderBy('title', 'DESC')->get();
$selRoles = $usuario->roles->pluck('id')->toArray(); //selRoles it´s roles from user
$usuario->load('roles');
return view('admin.empleados.edit', compact('usuario', 'roles','selRoles'));
}
and retult in blade it´s:
teleoperadora
teleoperadora
teleoperadora
repartidor
repartidor
repartidor
prueba
prueba
prueba
Jefe de equipo
Jefe de equipo
Jefe de equipo
jefa-sala
jefa-sala
jefa-sala
instalador
instalador
for example. I don´t know that i´m doing bad for get this result in my blade. Anybody can help me please?
Thanks for read and help. Sorry for my english
Your roles get duplicated because you are using two foreach loops. Your selected roles are an array, so it should suffice to check if a role->id is in that array:
#foreach($roles as $role)
<div class="col-md-4">
<input type="checkbox" name="rol" #if(in_array($role->id, $selRoles))checked=checked#endif class="form-check-input" value="{{ $role->id }}" id="{{ $role->id }}">
{{ $role->name }}
</div>
#endforeach
The part #if(in_array($role->id, $selRoles))checked=checked#endif will check the checkbox if the role's id is in your array of selected roles.
Related
I'm planning to do code confirmation in a program where it requires 2 or more codes and confirmation.I tried and stuck with code like this
#foreach ($kode as $k)
#if ($k->kode != $k->konfirmasi )
<form action="/tps/kode" method="post">
#csrf
<input type="text" name="{{ $k->status }}" id="{{ $k->status }}" placeholder="{{ $k->status }}">
<button type="submit">submit</button>
</form>
{{-- #break --}}
#else
succes
#endif
#endforeach
I tried to use break and continue but that's not the place, maybe. It seems more to the logic where if the konfirmasi is the same and everything is the same then it will be successful. but I'm confused what to use
If I understand your question correctly, then you should first validate the codes and after iterating over the $kode variable, check if a kode was not the same as the konfirmasi. If so, show the form.
#php($failure = false)
#foreach ($kode as $k)
#if ($k->kode != $k->konfirmasi )
#php
$failure = true;
$failedKode = $k;
#endphp
#endif
#endforeach
#if($failure)
<form action="/tps/kode" method="post">
#csrf
<input type="text" name="{{ $failedKode->status }}" id="{{ $failedKode->status }}" placeholder="{{ $failedKode->status }}">
<button type="submit">submit</button>
</form>
#endif
Although this is a solution, let me advise you, like #TimLewis mentioned, move this kind of logic to the controller. This logic doesn't belong in views.
my controller
$id= Auth::guard('artist')->user()->id;
$profiles= Profile::where('id', $id)->get();
$profiless= Profile::findorFail($id);
return view('artists.profile_edit',compact('profiles','profiless'));
my model
protected $casts = [
'social_media_channel_name' =>'array',
'social_media_channel_link' =>'array',
];
my blade
#foreach($profiless as $key => $profiles)
<div class="col-md-6">
<input type="text" class="form-control mb-3 {{ $errors->has('social_media_channel_name') ? 'is-invalid' : '' }}" name="social_media_channel_name[]" value="{{$profiles->social_media_channel_name}}" placeholder="Social Media Channel Name">
</div>
#if($errors->has('social_media_channel_name'))
<div class="invalid-feedback">
{{ $errors->first('social_media_channel_name') }}
</div>
#endif
<div class="col-md-6">
<input type="text" class="form-control mb-3 {{ $errors->has('social_media_channel_link') ? 'is-invalid' : '' }}" name="social_media_channel_link[]" value="{{$profiles->social_media_channel_link}}" placeholder="Social Media Channel Link">
</div>
#if($errors->has('social_media_channel_link'))
<div class="invalid-feedback">
{{ $errors->first('social_media_channel_link') }}
</div>
#endif
#endforeach
here Attempt to read property "social_media_channel_name" on string laravel show error.how to solve
here Attempt to read property "social_media_channel_name" on string laravel show error.how to solve
There's a lot wrong with your code...
$profiles= Profile::where('id', $id)->get();
You're querying based on id, which should return a single row, but you're calling ->get() which returns a Collection... That's pointless.
$profiless= Profile::findorFail($id);
This is more correct, but that variable name is really bad.
#foreach($profiless as $key => $profiles)
You're looping over a single Model instance, which would make $key and $profiles both strings; you don't need this foreach at all. Additionally, you're overwriting the $profiles variable you're passing from the Controller via compact('profiles','profiless'));
Let's fix your code.
$id = auth()->guard('artist')->user()->id;
$profile = Profile::where('user_id', $id)->first();
// I assume `Profile` has a `user_id` column, you'd want to reference this instead of `id`
return view('artists.profile_edit', compact('profile');
profile_edit.blade.php:
<div class="col-md-6">
<input type="text" class="form-control mb-3 {{ $errors->has('social_media_channel_name') ? 'is-invalid' : '' }}" name="social_media_channel_name[]" value="{{ $profile->social_media_channel_name }}" placeholder="Social Media Channel Name">
</div>
#if($errors->has('social_media_channel_name'))
<div class="invalid-feedback">
{{ $errors->first('social_media_channel_name') }}
</div>
#endif
<div class="col-md-6">
<input type="text" class="form-control mb-3 {{ $errors->has('social_media_channel_link') ? 'is-invalid' : '' }}" name="social_media_channel_link[]" value="{{ $profile->social_media_channel_link }}" placeholder="Social Media Channel Link">
</div>
#if($errors->has('social_media_channel_link'))
<div class="invalid-feedback">
{{ $errors->first('social_media_channel_link') }}
</div>
#endif
Edit
If a User can have multiple Profiles, then you can do:
$id = auth()->guard('artist')->user()->id;
$profiles = Profile::where('user_id', $id)->get();
return view('artists.profile_edit', compact('profiles');
And include the loop in your view:
#foreach($profiles as $profile)
// The rest of the code would be the same
#endforeach
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>
I'm using laravel 5.8 i have two tables askquestions and Responses. When a user post a
question passing in the front-end, i notify all the back-end agent peer mail with the link they access to the show.blade.php here a button to redirect to the Response Form but in this form i'm listing all asked question with a #foreach but i just want to show the last asked question. Need help
<form method="POST" action="{{ route("admin.respones.store") }}" enctype="multipart/form-data">
#csrf
<div class="form-group {{ $errors->has('ask_question') ? 'has-error' : '' }}">
<label class="required" for="ask_question_id"><strong>
{{ trans('La question est:') }}</strong></label>
<select class="form-control select2" name="ask_question_id" id="ask_question_id" required>
#foreach($ask_questions as $id => $ask_question)
<option value="{{ $id }}" {{ old('ask_question_id') == $id ? 'selected' : '' }}>
{{ $ask_question }}</option>
#endforeach
</select>
#if($errors->has('ask_question_id'))
<span class="help-block" role="alert">
{{ $errors->first('ask_question_id') }}
</span>
#endif
<span class="help-block">
{{ trans('') }}
</span>
</div>
public function create()
{
abort_if(Gate::denies('respone_create'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$categories = Category::all()->pluck('name', 'id')->prepend(trans('Sélectionnez la thématique'), '');
$author_emails = User::all()->pluck('email', 'id')->prepend(trans('Choisissez votre email'), '');
$ask_questions = AskQuestion::all()->pluck('text_question', 'id');
return view('admin.respones.create', compact('categories', 'author_emails', 'ask_questions'));
}
I am using entrust in my laravel project.
In blade file when I edit a role I want to display all the permissions with checkbox.
But I stuck at the thought that I want the checkbox status is checked if the role has the permission.I pass the role and all permissions to blade ,and try
#foreach($permissions as $permission)
<input type="checkbox" value="{{$permission->name}}"
#if($role->hasPermission($permission->name))
checked="checked"
#endif
#endforeach
but it didn't work
I also try convert $role and $permissions to array and pass them to blade and use foreach twice ,it didn't work either.
Is there any way to make it?
You can try this:
#foreach($permissions as $permission)
#foreach($roles as $role)
#if($permission->hasRole($role->name))
<input type="checkbox" checked="checked" name="perms[[]" value="{{ $permission->id }}">
#else
<input type="checkbox" name="perms[]" value="{{ $permission->id }}">
#endif
#endforeach
#endforeach
turns out that $role also can call the hasPermission method
#foreach($permissions as $permission)
<div class="checkbox pull-left" >
<label class="">
<input type="checkbox" name="perms[]" value="{{$permission->id}}"
#if($role->hasPermission($permission->name)) checked #endif>
<p>{{$permission->display_name}}</p>
</label>
</div>
#endforeach