I want to show the old select value in this select field when I want to edit. How can I show my old value in these fields?
https://github.com/shakibzaman/book-library-laravel
you can show my GitHub repo-
Here is my rules edit page:
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
<label for="chap_id">{{ trans('cruds.chapters.title_singular') }}*</label>
<select name="chap_id" id="chap_id" class="form-control select2">
#foreach($chapters as $id => $chapter)
<option value="{{ $id }}">{{ $chapter }}</option>
#endforeach
</select>
#if($errors->has('name'))
<em class="invalid-feedback">
{{ $errors->first('name') }}
</em>
#endif
<p class="helper-block">
{{ trans('cruds.permission.fields.title_helper') }}
</p>
</div>
editController
public function edit($id)
{
if (!Gate::allows('users_manage')) {
return abort(401);
}
$rule = Rule::find($id);
$chapters = Chapter::all()->pluck('name', 'id')->prepend(trans('global.pleaseSelect'), '');
return view('admin.rules.edit', compact('rule', 'chapters'));
}
You can use the old() function to get the old value of a field. This function requires one input and that is the name of the field: old('chap_id').
You could use this one:
#foreach($chapters as $id => $chapter)
#if(null !== old('chap_id') && old('chap_id') === $id)
<option value="{{ $id }}" selected>{{ $chapter }}</option>
#else
<option value="{{ $id }}">{{ $chapter }}</option>
#endif
#endforeach
Or the shorter one:
#foreach($chapters as $id => $chapter)
<option value="{{ $id }}" {{ old('chap_id') === $id ? 'selected' : '' }}>{{ $chapter }}</option>
#endforeach
Related
i have filter form using select dropdown , but i cant get this old value after i submit this filter like input like type text . someone can help ?
<div class="form-group">
<label>Status</label>
<select class="form-control select2 select2-hidden-accessible" style="width: 100%;" data-select2-id="1" tabindex="-1" aria-hidden="true"
name="user_id" id="user_id" required
>
#foreach($unit as $id => $nama_unit )
<option value="{{ $id }}">{{ $nama_unit }}</option>
#endforeach
</select>
<div class="help-block with-errors"></div>
</div>
i add my function controller :
public function search_filter_alkes(Request $request)
{
$unit = User::where('roles_id' , 1)->pluck('nama_unit', 'id');
$user_id = $request->user_id;
$alat = Alat::with('users')->where('user_id',$user_id)
->where('jenis', 'Alkes')->
get();
session()->put('user_id',$user_id);
return view('sarpras.alkes',['user_id' => $user_id , 'unit' => $unit,'alat' => $alat ])
->with('user_id', $user_id)
;
}
as per your code use this and make sure you use withInput()
return redirect()->back()->withErrors($validator)->withInput();
<div class="form-group">
<label>Status</label>
<select class="form-control select2 select2-hidden-accessible" style="width: 100%;" data-select2-id="1" tabindex="-1" aria-hidden="true"
name="user_id" id="user_id" required
>
#foreach($unit as $id => $nama_unit )
<option value="{{ $id }}" {{ old('user_id') == $id ? "selected" :""}}>{{ $nama_unit }}</option>
#endforeach
</select>
<div class="help-block with-errors"></div>
</div>
EDITED
When you use redirect() after post method you have to use withInput() as i mention above example
return redirect('route')->withInput();
when you use view() you have to pass data like as array like your code then use same variable name
return view('sarpras.alkes',['user_id' => $user_id , 'unit' => $unit,'alat' => $alat ])
<option value="{{ $id }}" {{ $user_id == $id ? "selected" :""}}>{{ $nama_unit }}</option>
You wil have to compare old value with input key. So replace the below code inside your for loop
#if (Input::old('user_id') == $id)
<option value="{{ $id }}" selected>{{ $nama_unit }}</option>
#else
<option value="{{ $id }}">{{ $nama_unit }}</option>
#endif
You have multiple options:
<select name="tags[]" class="form-control select-tag" multiple>
#foreach($tags as $tag)
<option value="{{$tag->id}}" {{in_array($tag->id, old("tags") ?: []) ? "selected": ""}}>{{$tag->name}}</option>
#endforeach
</select>
OR
<select name="gender" class="form-control" id="gender">
<option value="">Select Gender</option>
<option value="M" #if (old('gender') == "M") {{ 'selected' }} #endif>Male</option>
<option value="F" #if (old('gender') == "F") {{ 'selected' }} #endif>Female</option>
</select>
This worked for me in laravel 8:
<select class="form-control" id="gender" name="gender" required>
<option value="">Select gender</option>
<option value="M" #if (old('gender') == "M") {{ 'selected' }} #endif>Male</option>
<option value="F" #if (old('gender') == "F") {{ 'selected' }} #endif>Female</option>
<option value="O" #if (old('gender') == "O") {{ 'selected' }} #endif>Other</option>
</select>
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>
I would like to know if it is possible use a blade directive to make some custom options. I want when the admin is authenticated appears all users, and if it is a student, the options available reduces to one.
<div class="form-group {{ $errors->has('course_id') ? 'has-error' : '' }}">
<label for="user_id" class="col-md-2 control-label">User</label>
<div class="col-md-10">
<select class="form-control" id="user_id" name="user_id">
<option value="" style="display: none;" {{ old('user_id', optional($registeredCourse)->user_id ?: '') == '' ? 'selected' : '' }} disabled selected>Select user</option>
#foreach ($users as $key => $user)
<option value="{{ $key }}" {{ old('user_id', optional($registeredCourse)->user_id) == $key ? 'selected' : '' }}>
{{ $user }}
</option>
#endforeach
</select>
{!! $errors->first('course_id', '<p class="help-block">:message</p>') !!}
</div>
</div>
#if( auth()->user()->type == 'admin' )
<option value="" style="display: none;" {{ old('user_id', optional($registeredCourse)->user_id ?: '') == '' ? 'selected' : '' }} disabled selected>Select user</option>
#foreach ($users as $key => $user)
<option value="{{ $key }}" {{ old('user_id', optional($registeredCourse)->user_id) == $key ? 'selected' : '' }}>
{{ $user }}
</option>
#endforeach
#else
// Here I just need the user_id of the guest
#endif
Thanks.
If you use different guards, you can do like this:
#auth('admin')
Otherwise you can make your own blade directives.
Do something like this in app/Providers/AppServiceProvider.php:
public function boot()
{
Blade::if('admin', function () {
return (auth()->user()->type == 'admin') ? true : false;
});
}
Then in your blade file:
#admin
// Code...
#else
// Code...
#endadmin
You can also pass a value to the directive like this: function ($userType)
And then in blade: #admin(auth()->user()->type)
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>