show data select option by user in laravel - laravel

I want to show Project Data in select option by assign User login.
this my form select option
<div class="form-group">
<label for="">Project *</label>
<select class="form-control select2" style="width: 100%;" name="project_id">
<option value="project_id" selected>Select One</option>
#foreach($projects as $id => $project)
<option value="{{$id}}">{{$project}}</option>
#endforeach
</select>
</div>
this my controller to pluck project name by assign user
$projects = UserProjects::pluck('project_id', 'user_id');
and I use Many To Many Relation.
in my User Model
public function projects()
{
return $this->belongsToMany(Project::Class, 'user_projects');
}
in my Project model
public function users()
{
return $this->belongsToMany(User::class, 'user_projects');
}
and in my table pivot user_projects it just project_id and user_id. what should i write in my select option in form.?

In many to many relation you don't have to define a Model for the intermediate table (user_projects).
Controller
$user_projects = auth()->user()->projects;
View
<div class="form-group">
<label for="">Project *</label>
<select class="form-control select2" style="width: 100%;" name="project_id">
<option value="project_id" selected>Select One</option>
#foreach($user_projects as $user_project)
<option value="{{$user_project->id}}">{{$user_project->name}}</option>
#endforeach
</select>
</div>

Related

Livewire dependent dropdown, multiple row in a form

i have a form than can submit multiple rows using livewire. I have school level and subjects model. School level has many subjects. Subjects belongs to School level. I want to make dependent dropdown. When i select a school level, the subjects related to the school level will be loaded. If i only have one row(one entry). it would not be a problem, but when i add new row, the subjects select field will follow the first row value. How do i load subjects so that it is unique to its own row?
School Level:
<label class="required form-label">School Level</label>
<select wire:model="studentForms.{{ $index }}.level" name="level" class="form-select" required>
<option value="" selected>--Select School Level--</option>
#foreach ($schoolLevels as $schoolLevel)
<option class="mb-2" value="{{ $schoolLevel->id }}">{{ $schoolLevel->name }}</option>
#endforeach
</select>
Subjects:
<select wire:model="studentForms.{{ $index }}.subject" name="subject" class="form-select" data-placeholder="Select Subjects" required>
<option value="" selected>--Select Subject--</option>
#foreach ($subjects as $subject)
<option value="{{ $subject->id }}">{{ $subject->name }}</option>
#endforeach
</select>
Livewire Class:
public function mount()
{
$this->subjects = collect();
}
public function render()
{
return view('livewire.form', [
'schoolLevels' => SchoolLevel::has('subjects')->get(),
]);
}
public function updated($value, $key)
{
$this->subjects = Subject::where('school_level_id', $key)->get();
}
Livewire Class

How to convert and display the value from the database in laravel

I want do display outlet name as "Food" if the value in the database is "FC" and display "Express" if the value in the database is "EX" in my for each drop down.
Here's my controller
$outlet_name = StoreFC::select('loc_type')
->groupBy('loc_type')
->orderBy('loc_type', 'ASC')
->get();
$district_name = StoreFC::select('location')
->groupBy('location')
->orderBy('location', 'ASC')
->get();
return view('test.store-fc', compact('outlet_name','district_name'));
Here's my view
<div class="form-group">
<select name="filter_district" id="filter_district" class="form-control" required>
<option value="">Select Location</option>
#foreach ($district_name as $district)
<option value="{{ $district->location }}">{{ $district->location }}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="filter_outlet" id="filter_outlet" class="form-control" required>
<option value="">Select Outlet</option>
#foreach($outlet_name as $outlet)
<option value="{{ $outlet->loc_type }}">{{ $outlet->loc_type }}</option>
#endforeach
</select>
</div>
Here's my model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class StoreFC extends Model
{
protected $table = 'store_foodcity';
}
This is how it show now in the drop down
You need to create custom getters in model StoreFC.
[https://laravel.com/docs/5.8/eloquent-mutators#defining-an-accessor][1]
The best thing U can do is to create a new column with the full name "Food" for "FX", otherwize.... you have to modify that mutator every time U change something in database.

Laravel - How do create Hierarchical dropdownlist for Edit View Blade

I am Developing a Web Application with Laravel-5.8.
I have done the create.blade as shown below:
public function create()
{
$supervisors = Employee::all();
return view('employees.create')->with('supervisors', $supervisor);
}
<select class="form-control select2bs4" data-placeholder="Choose Supervisor" tabindex="1" name="supervisor_id" style="width: 100%;">>
<option value="">Select Supervisor</option>
#if($supervisors->count() > 0)
#foreach($supervisors as $supervisor)
<option value="{{$supervisor->id}}">{{$supervisor->employee_code}}</option>
#endforeach
#endif
</select>
I have this Laravel model:
class Employee extends Model
{
protected $table = 'employees';
protected $fillable = [
'employee_code',
'supervisor_id',
'first_name',
'emp_image',
'last_name',
];
public function supervisor()
{
return $this->belongsTo('App\Models\Employee','supervisor_id');
}
}
edit.blade
<div class="col-12 col-sm-4">
<div class="form-group">
<label class="control-label"> Supervisor:</label>
<select class="form-control select2bs4" data-placeholder="Choose Employee Type" tabindex="1" name="supervisor_id" style="width: 100%;">
<option value="">Select Supervisor</option>
#if($supervisors->count() > 0 )
#foreach($supervisors as $supervisor)
<option value="{{$supervisor->id}}" #if($supervisor->id == $employee->id) selected #endif>{{$supervisor->employee_code}}</option>
#endforeach
#endif
</select>
</div>
</div>
The dropdown value this gives me is the same as the loaded employee.
How do I write the edit view blade for the supervisor dropdownlist as I have done for create view blade?
Thank you.
your edit function in controller
public function edit($id)
{
$employee = Employee::findOrFail($id);
$supervisors = Employee::all();
return view('employees.edit')->with('supervisors', $supervisor);
}
and the code for edit page
<select class="form-control select2bs4" data-placeholder="Choose Supervisor" tabindex="1" name="supervisor_id" style="width: 100%;">>
<option value="">Select Supervisor</option>
#if($supervisors->count() > 0)
#foreach($supervisors as $supervisor)
<option value="{{$supervisor->id}}" #if( $employee->supervisor_id == $supervisor->id )
selected
#endif>{{$supervisor->employee_code}}</option>
#endforeach
#endif
</select>

show data of nested foreach loop in two different Select Tag in laravel

I have two Model(Category,Brand) are relate with one to many relation(hasMany) in laravel, Like category hasMany brands. now i want to display brand data in one select tag and category model data in another select tag.
Here is my Controller code
$category=Category::with('brands')->get();
return view('product.add')->with('category',$category);
Here is my view blade.
<div class="form-group">
<select name="category_id" class="input">
<option value="">Select Main Category</option>
#foreach($category as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="brand_id" class="input">
#foreach($brands->brands as $brand)
<option value="{{$brand->id}}">{{$brand->name}}</option>
#endforeach
</select>
</div>
its not working
You can do something simple like this:
<div class="form-group">
<select name="category_id" class="input">
<option value="">Select Main Category</option>
#foreach($category as $cat)
<option value="{{$cat>id}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select name="brand_id" class="input">
#foreach($category as $cat)
#foreach($cat->brands as $brand)
<option value="{{$brand->id}}" category="{{$cat->id}}">{{$brand->name}}</option>
#endforeach
#endforeach
</select>
</div>
and you need to show only the brands for the selected option using javascript(jquery)
$('[name="category_id"]').on('change',function() {
if($(this).val() != '') {
$('[name="brand_id"] option').hide();
$('[name="brand_id"] option[category="'+$(this).val()+'"]').show();
} else {
$('[name="brand_id"] option').show();
}
});

Failed to save value select option

what's wrong in my project.
I want to save with select option, but the filed doesn't save data select option. it just save input text.
the project is taken different table, and the form just get the project_name and project_id. project_id will save in in table spent_times.
and the select option will save task_category in table spent_times
this my model
protected $table = 'spent_times';
protected $fillable = [
'task_category',
'story/meeting_name',
'assign',
'estimated_time',
'user_story',
'spent_time',
'percentage',
'lateness',
'index',
'project_id'
];
public function users() {
return $this->hasMany(User::class);
}
public function project() {
return $this->belongsTo(Project::class);
}
my create.blade.php
<form action="{{route('store')}}" method="POST">
#csrf
<div class="box-body">
<div class="form-group">
<label for="">Project *</label>
<select class="form-control select2" style="width: 100%;">
<option>Select One</option>
#foreach($projects as $id => $project)
<option value="{{$id}}">{{$project}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Story # Meeting Name *</label>
<input type="text" class="form-control" name="user_story">
</div>
<div class="form-group">
<label for="">Category *</label>
<select class="form-control select2" style="width: 100%;">
<option>Select One</option>
#foreach($task_categories as $category)
<option value="{{$category}}">{{$category}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<label for="">Estimated *</label>
<input type="text" class="form-control" name="estimated_time">
</div>
</div>
<div class="box-footer">
<a href="{{route('index')}}">
<button type="submit" class="btn btn-primary col-md-12" style="border-radius : 0px;">SAVE</button>
</a>
</div>
</form>
my controller
public function create()
{
$spentimes = new SpentTime;
$project = new Project;
$projects = Project::select('project_name', 'id')->get();
return view('Ongoings.index', compact ('projects', 'task_categories', 'spentimes', 'project'));
}
/**
* Store a newly created resource in storage.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function store(Request $request)
{
dd($request->all());
$spentime = SpentTime::create([
'project_name' => request('project_name'),
'user_stroy' => request ('user_story'),
'task_category' => request('task_category'),
'estimated_time' => request('estimated_time')
]);
$spentime->save();
return redirect()->route('index');
}
error like this
error in web browser
please help me
You are expecting those fields from request in controller.
project_id
meeting_name
task_category
estimated_time
But You are just sending estimated_time,
other 3 fields are not present in you create.blade.php
In create.blade.php, category select input don't have any name.
that's why task_category is getting null from request('task_category'). But task_category field is not nullable in your database table.
Send form input properly as you are expecting in your controller. You can use dd($request->all()) to check.
I believe this is your task_category select box.
so this select box doesn't have a name attribute. that's the error.
Possible solution.
<div class="form-group">
<label for="">Category *</label>
<select name="task_category" class="form-control select2" style="width: 100%;">
<option>Select One</option>
#foreach($task_categories as $category)
<option value="{{$category}}">{{$category}}</option>
#endforeach
</select>
</div>

Resources