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)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
To send a drop-down variable, you must send it as an array
- HTML Code:
<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>
- validation(in controller):
$this->validate($request, [ 'type' => 'required|...' ]);
Method 2
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>
Method 3
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'); }); }
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0