I have a clients table and one of the fields is office. This value can be London or Paris. Within my controller I pass the view the options
public function edit(Client $client)
{
$office = array('London' => 'London', 'Paris' => 'Paris');
return view('clients.edit', compact('client', 'office'));
}
Now within my view I am doing
#foreach($office as $off)
<option value="{{ $client->office }}">{{ $client->office }}</option>
#endforeach
Now $client->office is the previously selected value. So say Paris is saved in the database for the client, the selected option should be Paris and the unselected option should be London.
How can I achieve this?
Thanks
If your array is $office = array('London' => 'London', 'Paris' => 'Paris');
You can try
#foreach($office as $key => $value)
<option value="{{ $value }}" #if($value == $client->office) selected #endif>{{ $key }}</option>
#endforeach
Related
First of all sorry if my title question doesn't make sense, since I don't know how to word my problem and my English not that great. Okay, so
I have my controller
Buy::create([
'master_item_id' => $request->master_item_id,
'quantity_item' => $request->quantity_item,
'price' => $request->price
]);
I have my view
<select id="master_item_id" name="master_item_id"
class="form-control #error('master_item_id') is-invalid #enderror">
<option></option>
#foreach($item as $value)
<option {{ old('master_item_id') ? 'selected' : '' }} value="{{ $value->id }}">
{{ $value->name_item }} |
Rp {{ number_format($value->price, 0, ',', '.')}}
</option>
#endforeach
</select>
How do I create insert 'price' from one form select above? I'm guessing you use where() or maybe find()? I've been flipping through website searching and couldn't find anything. Thanks!
Using the model called MasterItem, this could work:
Buy::create([
'master_item_id' => $request->master_item_id,
'quantity_item' => $request->quantity_item,
'price' => MasterItem::find($request->master_item_id)->price
]);
This does assume that master_item_id in your request represents the id or primary key of the MasterItem model.
If not, and you cannot use find() then this would also work:
Buy::create([
'master_item_id' => $request->master_item_id,
'quantity_item' => $request->quantity_item,
'price' => MasterItem::firstWhere('whatever_the_column_is_called', $request->master_item_id)->price
]);
I work with events and years, one event can be in several years and a year can have
several events (pivot table created between the two tables).
I change my single dropdown list in multiple list :
<select id="year_id" type="text" class="form-control selectpicker #error('year_id') is-invalid #enderror" name="year_id[]" required multiple>
<option disabled>--Choisir--</option>
#foreach($years as $year)
#if(old('year') == $year->id)
<option value="{{ $year->id }}" selected>{{ $year->name }}</option>
#else
<option value="{{ $year->id }}">{{ $year->name }}</option>
#endif
#endforeach
</select>
The goal is to select several years for one same event.
I don't know why, but I think I'm not able to validate the form.
This is my controller code :
public function store(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|string|max:255',
'mnemonique' => 'required|string|max:255',
'color' => 'required|string|max:255',
'year_id[]' => 'required|array',
'year_id[].*' => 'required|integer|exists:years,id'
]);
$evenement = new Evenement();
//dd($validatedData);
// $evenement->create($validatedData);
$evenement = Evenement::create($validatedData);
$evenement->years()->sync([$validatedData['year_id[]']]);
I'm not able to insert the events into the pivot table and the new event into the event table.
If I use dd(), I still have a refresh of the form, I think I don't go into the controller...
Do you see something please ?
Thank you in advance.
Change validation rule to
'year_id' => 'required|array',
'year_id.*' => 'required|integer|exists:years,id'
To show Error message in view
#error('year_id')<div class="invalid-feedback">{{$message}}</div> #enderror
and
#if($errors->has('year_id.*')) <div class="invalid-feedback">Selected Year doesnt exists</div> #endif
While saving into database
$evenement->years()->sync($validatedData['year_id']);
I am using laravel with infyomlab generator. I want to add data as dropdown from another table.
Here is my code
ProductController.php
$supplier = Supplier::all();
$unit = Unit::all();
$category = Category::all();
$products = $this->productRepository->all();
$product = DB::table('products')->select(
'suppliers.*',
'units.*',
'categories.*')
->join('suppliers','suppliers.id', '=', 'products.supplier_id')
->join('units','units.id', '=', 'products.unit_id')
->join('categories','categories.id', '=', 'products.category_id')
->get();
return view('products.index', compact('supplier', 'unit', 'category', 'product'))
->with('products', $products);
fields.blade.php
<select class="form-control" name="supplier_id" id="supplier_id">
<option value="">Select Supplier</option>
#foreach($suppliers as $supplier)
<option value="{{ $supplier->id }}">{{ $supplier->$supplier_name }}</option>
#endforeach
but when i press add new product it does not show the form for input it shows the following error
Undefined variable: suppliers (View: C:\xampp\htdocs\qa\resources\views\products\fields.blade.php)
try This
#foreach($products as $supplier)
<option value="{{ $supplier->id }}">{{ $supplier->$supplier_name }}</option>
#endforeach
As u returning it
It's just a typo:
Here, you are returning supplier not suppliers compact('supplier', 'unit', 'category', 'product')
So you need to perform in front end like:
#foreach($supplier as $s)
<option value="{{ $s->id }}">{{ $s->$supplier_name }}</option>
#endforech
You replace it by
$supplier = Supplier::all();
to
$suppliers = Supplier::all();
return view('products.index', compact('suppliers', 'unit', 'category', 'product','products'));
So i currently have a drop down list where it will output a list of hotels based on its price. I now want to further develop the drop down list so that it can output a list of hotels based on price AND distance. How would i exactly create a drop down list that will output a list of hotels based on 2 or more criteria.
SearchController.php
public function index(Request $request)
{
$distances = DB::table('posts')->select('distance')->distinct()->get()->pluck('distance');
$prices = DB::table('posts')->select('price')->distinct()->get()->pluck('price');
$postsInRange = $request->has('distance')
? Post::where('distance', $request->distance)->get()
: [];
$postsInRange1 = $request->has('price')
? Post::where('price', $request->price)->get()
: [];
return view('Pages.search', [
'distances' => $distances,
'prices' => $prices,
'posts' => $postsInRange,
'posts' => $postsInRange1,
]);
}
public function store(Request $request)
{
// This will return all request data to your screen.
return $request->all();
return view('Pages.search');
}
Search.blade.php
<select name="distance" id="distance" class="form-control input-lg dynamic" data-dependent="state">
<option value="">Distance</option>
#foreach($distances as $distance)
<option value="{{ $distance }}">{{ $distance }}</option>
#endforeach
</select>
<br>
<select name="price" id="price" class="form-control input-lg dynamic" data-dependent="state">
<option value="">Price</option>
#foreach($prices as $price)
<option value="{{ $price}}">{{ $price}}</option>
#endforeach
So to clarify the drop down list works but only outputs one of the choices selected.
I think what you need is query filter
$post = new Post;
if ($request->has('price')) {
$post->where('price', $request->price);
}
if ($request->has('distance')) {
$post->where('distance', $request->distance);
}
return view('Pages.search', [
'distances' => $distances,
'prices' => $prices,
'posts' => $post->get()
]);
I assume that you need one list applying both filters.
I have two tables posts and category.
My controller:
public function index()
{
$posts = Post::orderBy('id', 'desc')->get(['category']);
$data = DB::table('posts')->paginate(5);
$category = DB::table('category')->pluck('cname','id');
return view('posts.index', [
'posts' => $posts,
'category'=> $category,
'posts' => $data
]);
}
My blade:
<select class="form-control" name="category" id="category_add">
<option value="">Select Category</option>
#foreach($category as $key=>$value)
<option value="{{ $key }}">{{ $value }}</option>
#endforeach
</select>
Now my code shows a drop down data from the database...
What I need is: When I select the drop down category it should only shows the particular category of data in the view dynamically
Any one help
Did you mean after selecting a category from the dropdown there will be another section which will show data about that specific category?
One way to do it is through ajax request. I dont know if i got your question right or wrong. can u explain a bit if i got it wrong?