How can i show in my edit.blade.php the selected category of my product. you can only choose 1 category so is not many to many relation.
Product model :
public function category(){
return $this->belongsTo(Category::class);
}
Category model :
public function products(){
return $this->hasMany(Product::class);
}
Db products:
<div class="form-group my-2">
<label for="category">Choose Category :</label>
<select name="category_id" class="form-control custom-select">
#foreach($categories as $category)
<option value="{{$category->id}}" #if($product->category->contains($category->id)) selected #endif>{{$category->name}}</option>
#endforeach
</select>
</div>
I keep getting errors like :
Call to undefined method App\Models\Category::contains()
Laravel version < 9
<option value="{{ $category->id }}" {{ ($product->category_id == $category->id) ? 'selected' : '' }}>{{ $category->name }}</option>
Larvel version > 9
<option value="{{ $category->id }}" #selected($product->category_id == $category->id)>{{ $category->name }}</option>
Ref: https://laravel.com/docs/9.x/blade#checked-and-selected
Your product belongsTo category:
<option value="{{$category->id}}" #if($product->category_id == $category->id) selected #endif>{{$category->name}}</option>
You could try this
<option value="{{$category->id}}" {{($product->category_id == $category->id) ? 'selected' : ''}}>{{$category->name}}</option>
Related
I have 2 selects on a page and i want to show the 2nd only when in the first select a specific option was chosen, is that possible in laravel? here the code:
<select class="form-select my-2" name="category_id">
<option disabled selected>Seleziona una categoria</option>
#foreach ($categories as $category)
<option value="{{ $category->id }}" #if ($category->id == old('category_id')) selected #endif>
{{ $category->name }}
</option>
#endforeach
</select>
<select class="form-select my-2" name="type_id">
<option disabled selected>Seleziona un tipo di sushi</option>
#foreach ($types as $type)
<option value="{{ $type->id }}" #if ($type->id == old('type_id')) selected #endif>
{{ $type->name }}
</option>
#endforeach
</select>
Using JQuery :
<script>
$('select[name="category_id"]').change(function (){
let categoryId = $(this).val();
if (categoryId == null)
{
$('select[name="type_id"]').hide();
}else{
$('select[name="type_id"]').hide();
}
});
</script>
how to make the old value selected in Laravel 8 blade with select input in edit form.... i tried this way but didn't work:
<select name="category" id="category" class=" #error('category') border-red-500 #enderror rounded-md shadow-sm mt-1 block w-full">
#foreach($categories as $category)
<option value="{{$category->id}}" {{ old('category', $category->id) ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
and this is what inside controller:
public function edit(Product $product)
{
$categories = Category::all();
return view('dashboard.products.edit', compact('categories', 'product'));
}
I think your product should have a category_id so in the loop you need to check it also $product->category_id === $category->id.
#foreach($categories as $category)
<option value="{{$category->id}}" {{ old('category', $category->id) || $product->category_id === $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
Based on the code in edit() method old() wont work. Because there is no old values since your code just return a view.If you need to use that function then you need to redirect with inputs like
return redirect()->back()->withInput();
I want to retrieve database values in category name and i want to show default value in selection. This is my controller for my edit view.
I have a category_product table.
<div class="col-md-4">
<div class="form-group">
<label for="category">category</label>
<select class="form-control" name="category" id="category">
#foreach($categories as $category)
<option value="{{ $category->id }}" {{ $product->categories()->category_id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
ProductController.php
public function edit(Product $product)
{
$categories = Category::all();
return view('Admin.products.edit', compact('product', 'categories'));
}
Product.php
public function categories()
{
return $this->belongsToMany(Category::class);
}
I get this error.
Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$category_id
You need to add default value in the select tag above foreach() loop.
<div class="col-md-4">
<div class="form-group">
<label for="category">category</label>
<select class="form-control" name="category" id="category">
<option value="xyz">xyz</option> /* set default option value */
#foreach($categories as $category)
<option value="{{ $category->id }}" {{$category->id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
</div>
If you want set category name selected then you need to match category in product collection. ( if you have catetory_id in product table )
<select class="form-control" name="category" id="category">
#foreach($categories as $category)
<option value="{{ $category->id }}" {{$product->catetory_id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
why all categories appear in one post, I just want to display the value category that matches the post.
public function edit(Post $post)
{
$categories = Category::all();
$tags = Tag::all();
return view('admin.post.edit', compact('post', 'categories', 'tags'));
}
<select id="category" name="categories[]" style="width: 100%" multiple>
#foreach($categories as $category)
<option
#foreach ($categories as $postCategory)
{{ $postCategory->id == $category->id ? 'selected' : '' }}
#endforeach
value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
<script>
$(document).ready(function () {
$("#category").select2({
placeholder: "Please Select"
});
});
</script>
Assuming $post will have a category_id you just need to check that field against the id of the current $category being iterated:
<select id="category" name="categories[]" style="width: 100%" multiple>
#foreach($categories as $category)
<option {{ $post->category_id == $category->id ? 'selected' : '' }}
value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
Let's say that I select a field the record is added to my database and my form, but when I want to change this field that I selected by another the first field is always by default, it does not return the selected field
Here was my code of create
<select name="father_id" class="form-control" required>
<option value="">--selectionner le parent svp --</option>
#foreach($fathers as $father)
<option value="{{ $eleve->father_id }}" >{{ $father->nom }}
{{ $father->prenom }}</option>
#endforeach
</select>
this is my edit form i tried this code
<select name="father_id">
#foreach($fathers as $father)
<option value="{{ $father->id }}"#if(old($father->father_id) ==
"father_id") selected #endif >{{ $father->nom }} {{ $father->prenom }}
</option>
#endforeach
</select>
my controller
public function update(Request $request, $id)
{
$note = Note::findOrFail($request->note_id);
$note->update($request->all());
session()->flash('success','Cet éléve a été modifié avec succés');
return redirect()->back();
}
the route
Route::resource('eleves','EleveController');
please help I am really stuck in this error
try it.
<select name="father_id">
#foreach($fathers as $father)
<option value="{{ $father->id }}" {{ old('father_id', '') === $father->id ? 'selected' : '' }}>
{{ $father->nom }} {{ $father->prenom }}
</option>
#endforeach
</select>