Reputation: 527
In my view method, my selected dropdown like this. My problem are:-
How do I retrieve data from a database if I'm using the dropdown field? (Update Method)
How do I want to make the dropdown selected? (Validation & Show Method)
<div>
<x-label for="type" :value="__('Choose pizza type:')" />
<select name="type" id="type" class="form-control">
<option selected disabled>Please choose</option>
<option value="Chicken" {{ $value->type =="Chicken" ? 'selected' : '' }}>Chicken</option>
<option value="Seafood" {{ $value->type =="Seafood" ? 'selected' : '' }}>Seafood</option>
<option value="Beef" {{ $value->type =="Beef" ? 'selected' : '' }}>Beef</option>
<option value="Prawn" {{ $value->type =="Prawn" ? 'selected' : '' }}>Prawn</option>
<option value="Tuna" {{ $value->type =="Tuna" ? 'selected' : '' }}>Tuna</option>
</select>
<span style="color:red">@error('type'){{ $message }} @enderror</span>
</div>
I got an error as shown below:-
Attempt to read property "type" on string (View: /home/vagrant/Projects/firstBlog/resources/views/pizzas/index.blade.php)
Upvotes: 1
Views: 696
Reputation: 527
Here are my migration tables:-
public function up()
{
Schema::create('pizzas', function (Blueprint $table) {
$table->id();
$table->timestamp('updated_at')->useCurrent();
$table->timestamp('created_at')->useCurrent();
$table->string('name');
$table->string('type');
$table->string('base');
});
}
Upvotes: 0
Reputation: 349
To send a drop-down variable, you must send it as an array
<div>
<x-label for="type" :value="__('Choose pizza type:')" />
<select name="type[]" id="type" class="form-control">
<option selected disabled>Please choose</option>
<option value="Chicken" {{ $value->type =="Chicken" ? 'selected' : '' }}>Chicken</option>
<option value="Seafood" {{ $value->type =="Seafood" ? 'selected' : '' }}>Seafood</option>
<option value="Beef" {{ $value->type =="Beef" ? 'selected' : '' }}>Beef</option>
<option value="Prawn" {{ $value->type =="Prawn" ? 'selected' : '' }}>Prawn</option>
<option value="Tuna" {{ $value->type =="Tuna" ? 'selected' : '' }}>Tuna</option>
</select>
<span style="color:red">@error('type'){{ $message }} @enderror</span>
</div>
$this->validate($request, [
'type' => 'required|...'
]);
Upvotes: 1
Reputation: 527
I get it correct this ways:-
<div>
<x-label for="type" :value="__('Choose pizza type:')" />
<select name="type" id="type" class="form-control">
<option selected disabled>Please choose</option>
<option value="Chicken"{{ $Info->type =="Chicken" ? 'selected':''}}>Chicken</option>
<option value="Seafood"{{ $Info->type =="Seafood" ? 'selected':''}}>Seafood</option>
<option value="Beef"{{ $Info->type =="Beef" ? 'selected':''}}>Beef</option>
<option value="Prawn"{{ $Info->type =="Prawn" ? 'selected':''}}>Prawn</option>
<option value="Tuna"{{ $Info->type =="Tuna" ? 'selected':''}}>Tuna</option>
</select>
<span style="color:red">@error('type'){{ $message }} @enderror</span>
</div>
Upvotes: 0