I'm using Laravel 8 for my project and in this project, I have made a page for creating users by admins.
And basically, each user has a role, so in order to show the roles, I have added this:
<div class="form-group">
<label class="col-sm-2 control-label">Role</label>
<select class="form-control" name="role" dir="ltr">
#foreach(\App\Models\Role::all() as $role)
<option value="{{ $role->id }}">{{ $role->name }}</option>
#endforeach
</select>
</div>
And all of roles goes like this:
Now I don't want to print Super Admin in this form field. So my question is, how can I prevent \App\Models\Role::all() from printing Super Admin in select option?
I would really appreciate any idea or suggestion from you guys...
Thanks in advance.
Instead of using all, you could do something like where('name', '!=', 'SuperAdmin')->get() or whereNotIn('name', [...hidden_roles])->get()
Now this should also be in your controller as the view shouldn't be interested with the business logic
Even better create a method in your user model called something like getNonAdminRoles (and you can add to this later if there are more hidden roles), or a more laravel way and create a scope https://laravel.com/docs/8.x/eloquent#local-scopes
You can do something like this in the view:
<div class="form-group">
<label class="col-sm-2 control-label">Role</label>
<select class="form-control" name="role" dir="ltr">
#foreach(\App\Models\Role::all() as $role)
#if($role->slug != 'super-admin')
<option value="{{ $role->id }}">{{ $role->name }}</option>
#endif
#endforeach
</select>
</div>
Related
So I know how to retrieve data to views(using classes in controller)
but how can I return some data to layout which doesnt(or rather i dont know where they are) have its classes where i could get code to do so.
here is my form in which i would like to put cities name.
I always do it by giving to my frontend controller data from db using
$events = DB::select('select * from events');
return view('frontend/index', ['event' => $events]);
Edit
How to pass $city->id from loop to action?
My app.blade form:
<form method="POST" action="{!! route('cityView', ['id' => $city->id]) !!}" class="form-inline">
<div class="form-group">
<select name="city_id" class="form-control">
<option>xd</option>
#foreach(\App\Models\City::all() as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-info">Search</button></a>
</form>
But how can I do it for a layout?
Would making a layout controller help with it? Or maybe make another class inside existing controller and return view of layout view?
Second solution would be adding this form to every single view since there will be only 3 of them but i prefer solution with app.blade
You can call data directly from layout something like this:
<form method="POST" action="#" class="form-inline">
<div class="form-group">
<select name="room_size" class="form-control">
<option></option>
#foreach(\App\Models\City::all() as $city)
<option value="{{ $city->id }}">{{ $city->name }}</option>
#endforeach
</select>
</div>
<button type="submit" class="btn btn-info">Search</button>
</form>
I made view that adds edits data, to database, its table got foreign keys. So for request I need its id. Can i somehow display name of it while under will be ids?
Im sorry if title is missleading, i dont know how to ask about this properly
In my controller im using both tables for foreign and master keys
Im doing it using this: (i dont know how its called)
Form code
<div class="form-group col-md-4">
<label for="inputState">City</label>
<select id="inputState" name="city_id" class="form-control">
<option selected >{{ $data->city_id }}</option>
#foreach($city as $city)
<option>{{ $city->id }}</option>
#endforeach
</select>
</div>
I think this is you're looking for. On edit, you pass the $data and $city properties to blade and on loop you can make selected the option in select element if the condition is checked
<div class="form-group col-md-4">
<label for="inputState">City</label>
<select id="inputState" name="city_id" class="form-control">
<option value="">Select</option>
#foreach($city as $city)
<option value="{{ $city->id }}" #if($city->id == $data->city_id) selected #endif>{{ $city->name }}</option>
#endforeach
</select>
</div>
I am creating a module to ToDo list. If the role type is admin, she/he can assign ToDo to any user. However, if a normal user creates a ToDo, it will automatically, be assigned to current user. I implemented like below:
ToDoController function looks like below:
public function create()
{
return view('todo.create',['countries' => Country::all(), 'business_types' => BusinessType::all(), 'users' => User::all()]);
}
The view file create.blade.php with user selection select box is as below:
#if(!empty(Auth::user()->roles()->where('name','admin')->first()))
<div class="form-group">
<label for="description">Assigned To</label>
<select class="form-control" name="user_id">
#foreach ($users as $user)
#if ($user->id == old('user_id'))
<option value="{{$user->id}}" selected>{{$user->name}}</option>
#else
<option value="{{$user->id}}">{{$user->name}}</option>
#endif
#endforeach
</select>
#if ($errors->has('user_id'))
<div class="alert-danger">
<p>{{ $errors->first('user_id') }}</p>
</div>
#endif
</div>
#else
<div class="form-group" style="display:none;">
<label for="description">Assigned To</label>
<select class="form-control" name="user_id">
<option value="{{$user->id}}" selected>{{$user->name}}</option>
</select>
#if ($errors->has('user_id'))
<div class="alert-danger">
<p>{{ $errors->first('user_id') }}</p>
</div>
#endif
</div>
#endif
In the case of normal user, it is not required to take all users value, but I can't avoid that. Is there any optimized method to implement the same this ?
Thanks in Advance !
Manu
I am creating a website using laravel and I have a form to create a new users and within it i need an option to display a way to select a users role, i would like to do this in the form of a drop down list but I'm having difficulty figuring it out.. so far I have this (see below my create.blade.php), but it is not displaying the data from the three roles i have in the table in the DB, (these are admin,instructor and student).
<div class="form-group">
<label for="role">Role</label>
<select name="roles[]" class="form-control">
#foreach($roles as $role)
<ul class="dropdown-menu" role="menu">
{{ $role }}
</ul>
#endforeach
</select>
</div>
Below is my form, I am new to laravel so just trying to learn to better myself, any help is much appreciated :)
If you're using native HTML select, you may use
<div class="form-group">
<label for="role">Role</label>
<select name="roles[]" class="form-control">
#foreach($roles as $role)
<option value="{{ $role->id }}"> {{ $role->nameOrWhatever }} </option>
#endforeach
</select>
</div>
In your UserController
/**
* Show the form for creating a new user
*
* #param \App\Role $model
* #return \Illuminate\View\View
*/
public function create(Role $model)
{
return view('users.create', ['roles' => $model->get(['id', 'name'])]);
}
then, in your create.blade.php
<select name="role_id" id="input-role" required>
<option value="">-</option>
#foreach ($roles as $role)
<option value="{{ $role->id }}" {{ $role->id == old('role_id') ? 'selected' : '' }}>{{ $role->name }}</option>
#endforeach
</select>
This will get you the desired
I am using laravel 5.4 . I am trying to get data form Department table and data come also the problem there more dropdown menu come also please tell how can i solve this problem. Thanks advance.[
<div class="form-group">
{{Form::label('department','Department')}}
#foreach($department as $value)
{{Form::select('department',[$value->name], ' ', array('placeholder' => 'Select Department','class'=>'form-control'))}}
#endforeach
</div>
It's not entirely clear what you're after. Your current code seems to output several select boxes, one for each department and my best guess is that you want to output only one select box, with each department as an option in it. You can achieve that by doing something like this (You may have to adapt this code slightly to suit your needs):
The manual approach
<div class="form-group">
<label for="department">Department</label>
<select id="department" name="department" class="form-control">
<option value="">Select Department</option>
#foreach ($departments as $department)
<option value="{{ $department->id }}">{{ $department->name }}</option>
#endforeach
</select>
</div>
Using Laravel Collective's Form Builder
<div class="form-group">
{!! Form::label('department', 'Department') !!}
{!! Form::select('department', ['' => 'Select Department'] + $departments->pluck'name', 'id')) !!}
</div>
Try to do this way :-
<div class="form-group">
{{Form::label('department','Department')}}
<select class="form-control" name="department">
#foreach($department as $value)
<option value="$value">$value</option>
#endforeach
</select>
</div>