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>
Related
I'm trying to create a json that collects all the information from the other columns, but the following error happens when I create a new record:
SQLSTATE[HY000]: General error: 1364 Field 'properties' doesn't have a default value
Migration
public function up()
{
Schema::create('hunters', function (Blueprint $table) {
$table->id();
$table->string('name_hunter', 50);
$table->integer('year_hunter');
$table->decimal('height_hunter', 3,2);
$table->decimal('weight_hunter', 5,2);
$table->string('type_hunter', 30);
$table->string('type_nen', 30);
$table->string('type_sangue', 3);
$table->timestamp('register_date')->useCurrent();
$table->timestamp('data_updated')->useCurrent()->useCurrentOnUpdate();
$table->json('properties');
});
}
HunterModel.php
protected $casts = [
'properties' => 'array'
];
HunterController.php
public function create()
{
return view('create');
}
public function store(HunterRequest $request)
{
$validations = $request->validated();
HunterModel::create($validations);
return redirect('/');
}
create.php
<form action="{{ url("create") }}" method="POST">
{{ csrf_field() }}
<div class="form_group">
<div for="nome_hunter">Name:
<input type="text" class="form-control" name="name_hunter" maxlength="50" value="{{ old('name_hunter') }}">
</div>
</div>
<br>
...
<div class="form_group">
<div for="type_blood">Type blood:
<select class="form-control" name="type_blood">
<option {{ old('type_blood') == '' ? 'selected' : ''}} value="">Choose the type blood</option>
<option {{ old('type_blood') == 'A+' ? 'selected' : ''}} value="A+">A+</option>
<option {{ old('type_blood') == 'A-' ? 'selected' : ''}} value="A-">A-</option>
<option {{ old('type_blood') == 'B+' ? 'selected' : ''}} value="B+">B+</option>
<option {{ old('type_blood') == 'B-' ? 'selected' : ''}} value="B-">B-</option>
<option {{ old('type_blood') == 'AB+' ? 'selected' : ''}} value="AB+">AB+</option>
<option {{ old('type_blood') == 'AB-' ? 'selected' : ''}} value="AB-">AB-</option>
<option {{ old('type_blood') == 'O+' ? 'selected' : ''}} value="O+">O+</option>
<option {{ old('type_blood') == 'O-' ? 'selected' : ''}} value="O-">O-</option>
</select>
</div>
</div>
<br>
</form>
What is the necessary change in the form so that it is possible for the JSON to collect the information?
You need a default value for the properties field. Add this to your migration and re-run it (be sure to revert the last migration).
$table->json('properties')->default(json_encode([]));
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 want to show previously selected options as selected, I have two data sets.
blocks that are JSON encoded in a column with country codes.
countries list from the country table
I want to compare both and want to show countries selected that are present in users blocked colmn JSON encoded
$user = User::find(auth()->user()->id);
$blocks = json_decode($user->blocked);
#foreach( Countries::orderBy('country_name')->get() as $country )
<option value="{{$country->country_code}}" >{{ $country->country_name }}</option>
#endforeach
i tried following but it selects on 1 value either 1st or last
#foreach ($sundaysArray as $key => $value)
#foreach( Countries::orderBy('country_name')->get() as $country )
<option #if( $value == $country->country_code ) selected="selected" #endif value="{{$country->country_code}}" >{{ $country->country_name }}</option>
#endforeach
#endforeach
Thanks to #gert-b
$user = User::find(auth()->user()->id);
$sundaysArray = json_decode($user->blocked);
#foreach( Countries::orderBy('country_name')->get() as $country )
<option #if (in_array($country->country_code,$sundaysArray)) selected="selected" #endif value="{{$country->country_code}}" >{{ $country->country_name }}</option>
#endforeach
$selected = explode(",", $products->supplier_id);
<select name="supplier_id[]" multiple="multiple">
#foreach($suppliers as $supplier)
<option value="{{ $supplier->id }}" {{ (in_array($supplier->id, $selected)) ? 'selected' : '' }}>{{ $supplier->name}}</option>
#endforeach
</select>
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>
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