prasanna purohit
prasanna purohit

Reputation: 98

i'm using a select tag in my laravel project but when i select it's not taking any value from option

i'm trying to fetch data from database after selecting from the select tag.

this is my view code:

<div class="form-row">
    <div class="form-group col-md-6">
        <label>Name</label>
        <select name="id" class="form-control">
            @foreach($clients as $client)
                <option value="{{ $client->Cid }}" {{ $selectedclients == $client->Cid ? selected="selected" : '' }}>{{ $client->name }}</option>
            @endforeach
        </select>
    </div>
</div>

this is my controller code:

$Clients = Client::all();
$selectedClients = Client::first()->Cid;

when i run my code i'm getting the following error:

Syntax error, unexpected '=' (View: /home/prasanna/Billing-master/resources/views/Qtcreate.blade.php).

Upvotes: 2

Views: 126

Answers (3)

Nipun Tharuksha
Nipun Tharuksha

Reputation: 2567

Try with this

//Call your model in controller

use App\Client;

In your controller do like this

//fetch values from db

$Clients = Client::all();

//pass it to view

return view('welcome', compact('Clients’));

// Then in your view

@foreach($clients as $client)
    <option value="{{ $client->Cid }}"
            @if ($selectedclients == $client->Cid)
            selected
            @endif

    >{{ $client->name }}</option>
@endforeach

Upvotes: 2

Masood Khan
Masood Khan

Reputation: 635

You can use selected attribute without value.

Try this

<option value="{{ $client->Cid }}" {{ $selectedclients == $client->Cid ? 'selected' : '' }}>{{ $client->name }}</option>

Look at your variable names in controller and view

In Controller : $Clients & $selectedClients - In View : $clients & $selectedclients

Upvotes: 1

dparoli
dparoli

Reputation: 9161

You forgot quotations around selected="selected" in this line:

<option value="{{ $client->Cid }}" {{ $selectedclients == $client->Cid ? 'selected="selected"' : '' }}>{{ $client->name }}</option>

Upvotes: 3

Related Questions