I'm trying to show the selected category in the edit form But it is not showing,
My view :
<select name="category_id" id="category_id" class="form-control{{ $errors->has('category_id') ? ' is-invalid' : '' }}">
{{--<option value="">Choose Category</option>--}}
#foreach($cat as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
the edit method in my PostController :
public function edit($id)
{
$post = Post::findOrFail($id);
$cat = Category::all();
return view('admin.posts.edit',['post'=>$post,'cat'=>$cat]);
}
I tried to put an if statement like this :
#foreach($cat as $category)
<option value="{{ $category->id }}" {{$category->id == App\Category::class ? 'selected="selected"' : '' }}>{{ $category->name }}</option>
#endforeach
It is showing a category However it's not the selected category for the post.
HELP
$category->id == App\Category::class
You're comparing the id of the category with the name of the class here.
What you want to do is probably this:
$category->id == $post->category_id
check request by category id and if it is same echo selected.
#foreach($cat as $category)
<option value="{{$category->id}}" #if(request()->category_id==$category->id) selected #endif>{{$category->name}}</option>
#endforeach
Just retrieve it as a the defined select:
<select name="category_id" id="category_id" class="form-control{{ $errors->has('category_id') ? ' is-invalid' : '' }}">
<option value="{{ post->category->id }}" name="{{ $post->category->name }}">
#foreach($cat as $category)
<option value="{{$category->id}}">{{$category->name}}</option>
#endforeach
</select>
This will now be the default option and the loop will still be in the dropdown
Related
I am trying to show roles which is related to user but unfortunately userRoles are not showing related to user in selection option, please help me how can I match that thanks.
Note :- I am using spaite laravel permission docs
controller
public function edit(User $user)
{
$data = [
'isEdit' => true,
'user' => $user,
'roles' => Role::where('guard_name', '=', 'web')->select(['id', 'name'])->get(),
'userRole' => $user->getRoleNames(),
// i am getting userRole in array related to user
// ['employe']
];
// return $data['userRole'];
return view('cms.user_management.add-user', $data);
}
html view
<div class="col-md-6">
<div class="form-group">
<label>Role <span class="text-danger">*</span></label>
<select class="form-control" name="roles[]">
<option selected="selected" disabled >please
select</option>
#foreach ($roles as $item)
<option value="{{ $item->name }}"{{ $userRole ==
$item->name ? ' selected' : '' }}>{{ $item->name }}</option>
#endforeach
</select>
</div>
<span class="text-danger">{{ $errors->first('roles') ?? null }}
</span>
</div>
Try the following changes in your HTML
If $userRole is array than check if $item->name exists in array, using in_array.
'userRole' => $user->getRoleNames()->toArray(),
html view
{{ in_array($item->name, $userRole) ? 'selected' : '' }}
<div class="col-md-6">
<div class="form-group">
<label>Role<span class="text-danger">*</span></label>
<select class="form-control" name="roles[]">
<option selected="selected" disabled>please select</option>
#foreach ($roles as $item)
<option value="{{ $item->name }}"{{ in_array($item->name,$userRole) ? 'selected' : '' }}>{{ $item->name }}</option>
#endforeach
</select>
</div>
<span class="text-danger">{{ $errors->first('roles') ?? null }}</span>
</div>
you can use this to get the roles asssociated with current user or
Auth::user()->roles->pluck('name');
for any specific user roles.
User::find($id)->roles->pluck('name')
How can I get selected old values of multi-select dropdown in the create form and the edit form?
<select name="team[]" id="team" class="selectpicker" multiple>
#foreach ($tdropdown as $tdrop =>$id)
<option value="{{$id}}">{{$tdrop}}</option>
#endforeach
</select>
I tried as follows in the create form:
<option value="{{ $id }}" {{ (collect(old( 'team' ))->contains($tdrop)) ? 'selected':'' }} {{ $tdrop,$employee->team ? 'selected' : ''}} > {{ $tdrop }}</option>
In the edit form:
<option value="{{ $id }}" {{ (collect(old( 'team' ))->contains($tdrop)) ? 'selected':'' }} {{ ($tdrop,$employee->team) ? 'selected' : ''}} > {{ $tdrop }}</option>
Please help me to get the multi-select old values in the edit form and create form. Thank you.
I am creating a blog in Laravel. I have two tables - posts and categories.
When i want to edit one post, i have a problem with categories. My post controller is sending categories to the view, i can see them while editing, but i don't know how to set the current category in the select option.
The tables are conected:
public function posts(){
return $this->hasMany('App\Post');
}
public function category(){
return $this->belongsTo('App\Category');
}
Controller:
public function edit(Post $post)
{
$categories = Category::all();
return view('admin.posts.edit', compact('post'))->withCategories($categories);
}
View:
<form action="{{ route('posts.update', ["post" => $post->id]) }}" method="post" enctype="multipart/form-data" class="form-horizontal">
#csrf
<div class="form-body">
<div class="form-group">
<label class="col-md-3 control-label">Naslov</label>
<div class="col-md-4">
<input type="text" name='title' value='{{ old("title", $post->title) }}' class="form-control">
#if($errors->has('title'))
<div class='text text-danger'>
{{ $errors->first('title') }}
</div>
#endif
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">Kategorija</label>
<div class="col-md-4">
<select class="form-control" name="category_id">
#if(count($categories) > 0)
#foreach($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
#endif
</select>
#if($errors->has('category_id'))
<div class='text text-danger'>
{{ $errors->first('category_id') }}
</div>
#endif
</div>
</div>
How can i make the category display the current category of the selected post in the option select like i have for title? value='{{ old("title", $post->title) }}'
To show the currently associated Category, you can set a default "selected" option based on the $post->category_id:
<option value="{{ $category->id }}" {{ old('category_id', $post->category_id) == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
This a simple ternary that sets the selected attribute of one of the options based on the old() value, or, if old() is not set, then based on the value of $post->category_id
What you are looking for is the selected property of the <option> element.
#forelse ($categories as $category)
<option value="{{ $category->id }}" {{ $category->id == old('category_id', $post->category_id) ? 'selected' : '' }}>
{{ $category->name }}
</option>
#endforelse
A minor change as well in the in you blade template to make use of the #forelse directive for less code.
You could also change your controller code a bit:
return view('admin.posts.edit', compact('post', 'categories'));
Further the laravelcollective/html package might help you a lot in creating forms.
On the Edit view I have a Select field and I want that select field to have the saved value for a given Model to be selected.
mediaController.php
public function edit($id)
{
//
$media = Media::find($id);
$categories = Category::lists('category', 'id');
return view('medias.edit-media')->with('media', $media)->with('categories', $categories);
}
edit.blade.php
<div class="form-group">
{!! Form::select('categories', $categories, $media->category ) !!}
</div>
On the index view (i.e the first Media as a Category of Video)
On the edit view (the first Media doesn't have the Category 'Video' selected)
even if I change my edit.blade.php to this: ...
<div class="form-group">
<label>Category of Upload
<select name="category" id="category" class="form-control input-sm">
#foreach($categories as $category)
<option value="{{ $category }}" {{ Input::old($media->category) == $category ? 'selected' : '' }}>{{ $category }}</option>
#endforeach
</select>
</label>
</div>
I still have the same result (the right Category is not selected)
<div class="form-group"><label>Category of Upload <select name="category" id="category" class="form-control input-sm"> #foreach($categories as $category) <option value="{{ $category->id }}" {{ $media->categories == $category->id ? 'selected' : '' }}>{{ $category }}</option>#endforeach </select> </label></div>
Trying to improve the code of my app and just migrated to L5. I used to call models in my views and I know this is not best practice - I should separate data calling and views completely.
However how do you deal with a situation like this:
<div class="field">
<label for="country">Country<sup>*</sup></label>
<select class="" id="country" name="country">
<option value="{{{ old('country') ? old('country') : '' }}}">{{{ old('country') ? App\Country::find(old('country'))->country_name : '' }}}</option>
#foreach ($countries as $studio_country)
<option value="{{ e($studio_country->id) }}">{{ e($studio_country->country_name) }}</option>
#endforeach
</select>
#if ($errors->has('country'))
<div class="alert alert-warning" role="alert">
{{ $errors->first('country') }}
</div>
#endif
<br>
</div>
Basically if there is an input and the input did not pass the validation the page refreshes with the old input and the error message. I need to extract the name of the country from the DB since I only have its ID.
You would want to do something like this:
<select id="country" name="country">
#foreach ($countries as $studio_country)
<option
value="{{ $studio_country->id }}"
{{ $studio_country->id === old('country') ? 'selected' : '' }}>
{{ $studio_country->country_name }}
</option>
#endforeach
</select>