Can someone tell me how can I get selected value when I try to edit post in Laravel project.
So here are parts of code
Post.php:
public function category(){
return $this->belongsTo('App\Category');
}
Category.php:
public function posts(){
return $this->hasMany('App\Post');
}
PostController.php:
$post = Post::findOrFail($id);
$categories = Category::all();
$tags = Tag::all();
return view('admin.posts.edit', ['post' => $post, 'categories' => $categories, 'tags' => $tags]);
posts/edit.blade.php:
<div class="form-group">
<label for="category_id">Selecet category</label>
<select name="category_id" id="category_id" class="form-control">
<option disabled>List of available post categories</option>
#foreach($categories as $category)
<option value="{{ $category->id }}" {{$category->id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
Like #Hardood mentioned. $category->id == $category->id will always return true. In fact, if you just want to get the value from form submission. You can just remove that. HTML will handle selected value itself.
You can simply use $request->category_name in your backend.
Problem solved with this part of code (for future users who will read this :) )
#foreach($categories as $category)
<option value="{{ $category->id }}"
#if($post->category->id == $category->id)
selected
#endif
>{{ $category->name }}</option>
#endforeach
Related
In my laravel application I have a simple form with a dropdown.
Dropdown options are getting filled with DB data.
I want to populate my dropdown with user previously selected option on the edit.
Following is what I have done so far..
Blade
<select name="company_id" class="form-control">
#foreach($companies as $company)
#if (old('company_id') == $employee->company_id)
<option value="{{ $company->id }}" selected>{{ $company->name }}</option>
#else
<option value="{{ $company->id }}">{{ $company->name }}</option>
#endif
#endforeach
</select>
Controller.
public function edit(Employee $employee)
{
$companies = Company::all(['id','name']);
return view('employees.edit', compact('employee','companies'));
}
Employee Model
{
use HasFactory, Notifiable;
protected $fillable = [
'first_name', 'last_name', 'email', 'phone', 'company_id'
];
public function company()
{
return $this->belongsTo(Company::class);
}
}
My company table structure
Employee table structure
When I tried with these, it kept showing me the values in the dropdown on the edit but not setting the old value properly.....
Try this as your loop:
#foreach ($companies as $company)
<option value="{{ $company->id }}" {{ $company->id == old('company_id') ? 'selected' : '' }}>
{{ $company->name_en }}</option>
#endforeach
The reason could be typecasting.
#if (old('company_id') === $employee->company_id)
This will never be true. You see old holds string values and you're comparing it against an id which is of type int.
So, this will work for you:
#if ( (int)old('company_id') === $employee->company_id )
Also, there is a new directive you can use instead of conventional if-else statements checked :
#checked( old('key', $employee->key) )
#selected( old('key') === $employee->key )
In Laravel 9 you don't have to use if-else to check the value just use the #selected() blade function/directive.
<select name="company_id" class="form-control">
#foreach($companies as $company)
<option value="{{ $company->id }}" #selected(old('company_id') == $company->id)>{{ $company->name }}</option>
#endforeach
</select>
Also, we have the #checked() function as well:
<input type="checkbox" name="active" value="active" #checked(old('active', $user->active)) />
I have three tables: products, categories and category_product.
In my edit form page I'm displaying the categories and subcategories values in a select box.
The chosen category options are saved in the pivot table category_product. This table has product_id and category_id.
Here's my code.
Product model
public function category() {
return $this->belongsToMany('App\Models\Category');
}
Category model:
public function products()
{
return $this->belongsToMany(Product::class);
}
edit product page view:
<select name="category_id[]" class="form-control" multiple size = 10>
#foreach ($parentCategories as $parentCategory)
<option value="{{ $parentCategory->id }}">{{ $parentCategory->name }}</option>
#if(count($parentCategory->subcategory))
#include('includes.subcats-select',['subcategories' => $parentCategory->subcategory])
#endif
#endforeach
</select>
subcats-select view
#foreach($subcategories as $subcategory)
<option value="{{ $subcategory->id }}">---{{ $subcategory->name }}</option>
#if(count($subcategory->subcategory))
#include('includes.subcats-select',['subcategories' => $subcategory->subcategory])
#endif
</div>
#endforeach
How can I put "selected" attribute to the chosen category options, so when I edit the page to apply the changes properly?
You can get pivot table columns the following way:
Product model
public function category() {
return $this->belongsToMany('App\Models\Category')->withPivot('selected');
}
edit product page View:
<select name="category_id[]" class="form-control" multiple size = 10>
#foreach ($parentCategories as $parentCategory)
<option value="{{ $parentCategory->id }}" selected="{{ $parentCategory->pivot->selected }}">
{{ $parentCategory->name }}
</option>
#if(count($parentCategory->subcategory))
#include('includes.subcats-select',['subcategories' => $parentCategory->subcategory])
#endif
#endforeach
</select>
EDIT: While the above answer might be helpful for others, it doesn't answer the question. So here's my second try after some clarification in the chat.
<select name="category_id[]" class="form-control" multiple size = 10>
#foreach ($parentCategories as $parentCategory)
<option value="{{ $parentCategory->id }}"{{ $product->category->contains($parentCategory->id) ? 'selected' : '' }}>
{{ $parentCategory->name }}
</option>
#if(count($parentCategory->subcategory))
#include('includes.subcats-select',['subcategories' => $parentCategory->subcategory])
#endif
#endforeach
</select>
i want to edit user profile which has a country, country field is a select option, i want to get a user's country value in select field, i tried the answer in stack but it didn't work for me knowing there is relationship between user and country belongsTo hasMany.
edit.blade.php
<select id="country" name="country_id" class="form-control" >
<option value="" selected disabled>Select</option>
#foreach($countries as $country)
<option value="{{$country->id}} {{ $country->id == $users->country_id ? 'selected' : '' }}"> {{ $country->name }}</option>
#endforeach
</select>
usersController.php
public function edit($id)
{
$users = Auth::user();
$countries = Country::all();
return view('users.edit')->with([
'users' => $users,
'countries' => $countries
]);
}
Instead of this:
<option value="{{$country->id}} {{ $country->id == $users->country_id ? 'selected' : '' }}"> {{ $country->name }}</option>
I believe you want this:
<option value="{{$country->id}}"{{ $country->id == $users->country_id ? ' selected' : '' }}> {{ $country->name }}</option>
I am trying to show my categories on the site map page with the controller below.
CategoryController.php
class CategoriesController extends Controller
{
public function create()
{
$categories = Category
::orderBy('name','desc')
->where('parent_id', NULL)
->get();
return view('admin.category.create', compact('categories'));
}
}
The following is the part of my Blade file where I use the categories variable template.
create.blade.php
<div class="form-group">
<label for="exampleInputPassword1">Parent Category</label>
<select name="parent_id" class="form-control">
#foreach ($main_categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
I use every way to get variable and passing but none the didn't work do you have any suggestion?
You've got two options here.
Option A:
You named the collection categories in your Controller and pass it to the view as such. To access it in your view, you need to reference it by the same name.
Change this:
// Your view is looking for a collection titled `main_categories`, which does not exist
#foreach ($main_categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
To this:
#foreach ($categories as $c)
<option value="{{ $c->id }}">{{ $c->name }}</option>
#endforeach
Option B:
Pass the data back using main_categories as the collection name
Change this:
return view('admin.category.create', compact('categories'));
To this:
return view('admin.category.create', [
'main_categories' => $categories
]);
You can read up more on passing data to views here.
To be more clear Ill break your code as below and add some details
class CategoriesController extends Controller
{
public function create()
{
$categories = Category
::orderBy('name','desc')
->where('parent_id', NULL)
->get();
// Your are passing variable but I change it as below to be more clear
// return view('admin.category.create', compact('categories'));
//now you are passing the value to the view
return view('admin.category.create', ['categories' => $categories]);
}
}
Now lets catch it in the view. Note that now $categories available in the view. If you pass ['A' => $categories] then view has $A variable so you should call for the relevant variable that you define in the controller.
<div class="form-group">
<label for="exampleInputPassword1">Parent Category</label>
<select name="parent_id" class="form-control">
**{{-- In here you should pass $categories --}}**
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
I made a admin panel with laravel 5.4 . I want to show my category list using category table in products items edit view page. This is my product items edit page controller.
public function edit($id)
{
$item = Item::findOrFail($id);
//$sub_cat = SubCat::all();
$sub_cat = SubCat::with('category')->get();
return view('admin.items.edit', compact(['item', 'sub_cat']));
}
And this is my product items edit page view selection box values show
<div class="form-group">
<label>Main Category</label>
<select class="form-control" id="main_category" name="main_category">
#if(!empty($sub_cat))
#foreach ($sub_cat as $pages)
<option value="{{ $item->id }}">{{ $pages->name }}</option>
#endforeach
#endif
</select>
</div>
I used a model relationship like this
public function category(){
return $this->belongsTo('App\SubCat');
}
How i show my saved category name with other category names into my selection box.
first of all get all the categories like this
public function edit($id)
{
$item = Item::findOrFail($id);
$sub_cat = SubCat::all(); //get all the categories
return view('admin.items.edit', compact(['item', 'sub_cat']));
}
in the blade file run loop on $sub_cat collection and replace $item->id to $pages->id
<div class="form-group">
<label>Main Category</label>
<select class="form-control" id="main_category" name="main_category">
#if(!empty($sub_cat))
#foreach ($sub_cat as $pages) //loop on $sub_cat
<option value="{{ $pages->id }}" {{ $item->category_id == $pages->id ? 'selected="selected"' : '' }}>{{ $pages->name }}</option>
#endforeach
#endif
</select>
</div>