How to convert and display the value from the database in laravel - 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.

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

Getting name and id value from select Laravel livewire

I need a little help. I want to save also $item->name from the option to the Category database in the name column. Thanks for the help.
My blade code:
<label>Select a Category</label>
<select class="select2 form-control" for="cateId" wire:model.lazy="cateId">
<option value="">Select a Category</option>
#foreach ($categorylist as $item)
<option value="{{ $item->id }}">{{ $item->name }}</option>
#endforeach
</select>
my livewire controller:
public $name;
public function submit()
{
Category::create([
'name' => $this->$item->name,
]);
}
my Category model:
protected $table = "category";
protected $fillable = [
'name',
];
Just change option value like this:
<label>Select a Category</label>
<select class="select2 form-control" for="cateId" wire:model.lazy="catName">
<option value="">Select a Category</option>
#foreach ($categorylist as $item)
<option value="{{ $item->name }}">{{ $item->name }}</option>
#endforeach
</select>
and get it from $request->input('catName')

How do I search in Laravel using selects?

On my test site I have a search by cities and categories, which are selected using a select
<div class="form-group">
<select class="selectpicker" name="sCity">
#foreach($cities as $city)
<option value="{{$city->slug}}">{{$city->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select class="selectpicker" name="sCat">
#foreach($categories as $cat)
<option value="{{$cat->slug}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
And such a method for searching
public function search(Request $request)
{
$result = DB::table('ads')
->where('city_slug', $request->sCity)
->orWhere('category_slug', $request->sCat)
->get();
dd($result);
}
If you search for one select, then the search is correct, but if you set values ​​to two selections at once, then only the first is triggered, and the second is ignored. How can you fix this?
Try this:
$result1 = DB::table('ads')
->where('city_slug', $request->sCity)
->get();
$result2 = DB::table('ads')
->where('category_slug', $request->sCat)
->get();
$result = $result1->merge($result2):
you could do something like this
in your view
<div class="form-group">
<select class="selectpicker" name="sCity">
<option value=0>please select one</option>
#foreach($cities as $city)
<option value="{{$city->slug}}">{{$city->name}}</option>
#endforeach
</select>
</div>
<div class="form-group">
<select class="selectpicker" name="sCat">
<option value=0>please select one</option>
#foreach($categories as $cat)
<option value="{{$cat->slug}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
and in your controller
$city_slug='';
$category_slug='';
if($request->sCity!=0)
{
$city_slug=' city_slug='.$request->sCity;
}
if($request->sCat!=0)
{
$category_slug=' or category_slug='.$request->sCat;
}
$result = DB::table('ads')
->whereRaw($city_slug.$category_slug)
->get();
dd($result);

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();
}
});

show data select option by user in 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>

Resources