Laravel-4 Jak wypełnić pole wyboru z bazy danych wartością id i wartością name

Chcę wypełnić pole wyboru clients z mojej bazy danych w kontrolerze projektów, ponieważ projekt będzie należał do client, ale należy również do użytkownika, który jest zalogowany.

Chcę utworzyć pole wyboru podobne do tego poniżej:

<select>
  <option value="$client->client_id">$client->client_name</option>
  <option value="$client->client_id">$client->client_name</option>
</select>

Mam to w laravel, które wypełnia moje pole select nazwami klientów, jednak atrybut value ma nazwę klienta i wolałbym, aby to miało client_id.

Sposób, w jaki to zrobiłem, jest jak poniżej:

ProjectController.php

public function create()
{
    //find logged in users clients
     $clients = Auth::user()->clients;
    $client_selector = array();
    foreach($clients as $client) {
    $client_selector[$client->client_name] = $client->client_name;
    }        
        // load the create form (app/views/projects/create.blade.php)
    return View::make('projects.create', array('client_selector' => $client_selector));
}

Utwórz.blade.php

{{ Form::open(array('action' => 'ProjectController@store', 'id' => 'createproject')) }}
  <div class="form-group">
 @if(count($client_selector)>0)

   {{ Form::label('select_client', 'Select Client', array('class' => 'awesome'));   }}

   <!-- SELECT IS CREATED HERE -->
   {{Form::select('client', $client_selector, array_values($client_selector)[0])}}

 @endif 

</div>
<div class="form-group">
    {{ Form::label('project_name', 'Project Name') }}
    {{ Form::text('project_name', Input::old('project_name'), array('class' => 'form-control')) }}

</div>

Jak widać po sposobie tworzenia select używa client_name do wypełniania atrybutów values i nie jestem ekspertem od Laravela, więc nie jestem pewien, jak zmienić te atrybuty.

Jeśli ktoś mógłby mi pokazać, jak to się robi lub ma lepszą metodę osiągnięcia tego, proszę podaj mi kilka przykładów!

Z góry dzięki.
Author: 001221, 2013-11-16

3 answers

Znalazłem na to sposób

ClientController.php
public function create() {

  // queries the clients db table, orders by client_name and lists client_name and id
  $client_optons = DB::table('clients')->orderBy('client_name', 'asc')->lists('client_name','id');

    return View::make('projects.create', array('client_options' => $client_options));
}

Utwórz.blade.php

// this form is now populated with the value as id and the option names as the client_name
{{ Form::select('clients', $client_options , Input::old('clients')) }}
Mam nadzieję, że to pomoże innym, którzy mają takie problemy.
 25
Author: 001221,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-16 14:56:21

Też się nad tym zastanowiłem i wymyśliłem:

W moim modelu:

public function scopeSelect($query, $title = 'Select') {
    $selectVals[''] = $title;
    $selectVals += $this->lists('name', 'id');
    return $selectVals;
}

To mogę to po prostu umieścić w moich poglądach:

{{ Form::select('select_id', Model::Select('Blank option'), '', array()) }}
 4
Author: Wally,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-04-17 00:44:10

Tutaj możesz użyć metody lists . In your client controller do

$clients = Client::lists('name', 'id');

I w widoku wystarczy użyć zmiennej typu

{{Form::select('client', $clients)}}
 1
Author: Jyothu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-28 12:30:24