Laravel view show data based on option selected - php

Inside my view, I have a Modal (vue component), in which there is a select with 3 options (admin, user, manager) and I want to display in the Modal all the registers for the role selected.
<modal-component id="showGroups">
<div class="form-group">
<select class="form-control">
<option disabled>Choose a role</option>
<option>admin</option>
<option>user</option>
<option>manager</option>
</select>
</div>
#foreach
<p>User register for the role {{ user->role }}</p>
#endforeach
</modal-component>
I'm struggling with this

Related

How to return value from different cell of table column by foreign key id in Laravel 8.*

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>

How to show data from table via Model except a custom row

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>

Laravel 7: Undefined variable in foreach loop

I want to display to select the campus from the campuses table in my budget allocation create.blade.php template but it displays an error.
create.blade.php
<div class="form-group row">
<label class="col-md-3">Campus</label>
<div class="col-md-9">
<select name="campus_id" class="form-control">
<option value="">--- Select Campus ---</option>
#foreach($campuses as $campus)
{{ var_dump($campus) }}
<option value="{{ $campus->campus_id }}">{{ $campus->campus_name }}</option>
#endforeach
</select>
</div>
<div class="clearfix"></div>
</div>
in your controller you must send the data to the view using second paramter of the view ...
return view('admin.budgetallocation.create',$arr);

How can I use same view file for different role types in Laravel 7?

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

how to grab database values to edit form in dropdown input in laravel

I am developing web application using laravel. In my application I have data entry form and in this form I have one optional value selections input
<label for="status" class="control-label">Choose Status</label>
<select name="status" id="status">
<option value="">Choose a status</option>
<option value="Upcoming">Upcoming</option>
<option value="Active">Active</option>
<option value="Completed">Completed</option>
</select>
#if ($errors->has('status'))
<span class="help-block">{{ $errors->first('status') }}</span>
#endif
Now I need develop edit data form some of my data in the data table. In this form I need show selection input field in edit page with current values in the data table. and select optional values if user go to the edit. then how can I develop selection field in the edit blade file.
Edited
<select name="assign" id="status">
<option value="{!! $task->assign !!}">{!! $task->assign !!}</option>
{{ getstatus($task->assign) }}
</select>
#if ($errors->has('assign'))
<span class="help-block">{{ $errors->first('assign') }}</span>
#endif
what about this edited optional input fields. it is working but not displaying other optional values.

Categories