Laravel Dropdown Form Get Form Database - php

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>

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>

Multiselect Box with Laravel and values from database

I am having difficulty with multi-select boxes with Laravel, particularly, reading values from the database and displaying these values so they can be edited in my edit view.
In my controller, in the store function, I am using the implode function to save positive integers into a database field.
Here is the implode function
$prodmulti = implode(',', $prodmulti);
The database stores these values in a cell such as
prodmulti
1,2,3
This works fine and I get the cell, populated with values fine.
The problem is when I wish to edit these values.
If I have nothing saved in this cell, then as expected, I have nothing in the multi-select box which is good.
If I have 1 number in the database cell, then again, it is fone, I can see the corresponding item in the multi-select box.
If I have 2 or more items in the database cell, then the multi-select box shows as blank in the edit view. This is where I am having my problem.
The view blade for the edit page with the multi-select box looks like this:
<div class="col-lg-8 col-xl-5">
<div class="form-group">
<select class="js-select2 form-control" name="prodmulti[]" style="width: 100%;" multiple>
<option></option>
#foreach($productcategories as $productcategory)
<option value="{{ $productcategory->id }}" {{ $user->prodmulti == $productcategory->id ? "selected" : ""}}>
{{ $productcategory->name }}
</option>
#endforeach
</select>
</div>
</div>
And the show function in the controller looks like this where I use the explode to extract the values as an array as such.
$prodmulti = explode(',', $prodmulti);
return view(
'admin.users.edit',
compact([
'user',
'roles',
'countries',
'states',
'productcategories',
'prodmulti'
])
);
If it helps, here is my create a view where I again use one of these multi-select boxes to add values
<div class="col-lg-8 col-xl-5">
<div class="form-group">
<select class="js-select2 form-control" id="prodmulti" name="prodmulti[]" style="width: 100%;" data-placeholder="Choose many.." multiple>
<option></option>
#foreach($productcategories as $productcategory)
<option value="{{ $productcategory->id }}">
{{ $productcategory->name }}
</option>
#endforeach
</select>
</div>
</div>
Finally, I am using select2 multi-select boxes, but I get the same result if I use just standard bootstrap multi-select boxes and have tried both, I just like the look of the select2 multi-select boxes.
Any help would be greatly appreciated,
Thanks,
Steve.
use in_array() to check
$prodmulti = explode(',', $prodmulti);
return view('admin.users.edit', compact(['user', 'roles', 'countries', 'states', 'productcategories', 'prodmulti']));
then in blade
<div class="col-lg-8 col-xl-5">
<div class="form-group">
<select class="js-select2 form-control" id="prodmulti" name="prodmulti[]" style="width: 100%;" data-placeholder="Choose many.." multiple>
<option></option>
#foreach($productcategories as $productcategory)
<option value="{{ $productcategory->id }}"
{{ in_array($productcategory->id,$prodmulti) ? "selected" : "" }}>
{{ $productcategory->name }}
</option>
#endforeach
</select>
</div>
</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 to use Select tags in Laravel Forms

For some reason the value is not outputting when I press submit. There is always an error it basically says there is no data.
<div class="form-group">
<div class="">
<label for="category">Size</label>
<br>
<select name="category" class="form-control">
#foreach($size as $s)
<option value="{{ $s->id }}">{{ $s->size }}</option>
#endforeach
</select>
</div>
</div>
just using pluck and map into Form select
in your controller
$model = Model::pluck('size', 'id')->toArray();
return view('view', compact('model'))
in view
{{ Form::select('category', $model, null, ['class' => 'form-control']) }}

Categories