dqc18251 2017-07-06 10:05
浏览 337
已采纳

Laravel集体形式和foreach循环

I want to connect my dropdown select form with database, currentli I have sth like this:

                 @foreach( $clients as $client)
                    {!! Form::select('connected_with',
                     ['name' => $client->name . $client->surname
                      ]) !!}
                    @endforeach

And this is my controller:

        $clients = Client::all();

        return view('report_create')->with('clients', $clients);

and i get much fields. I want only one with items from db. How to do it?

  • 写回答

1条回答 默认 最新

  • duanjiao8871 2017-07-06 10:08
    关注

    If you want to create select list of clients, use pluck():

    $clients = Client::pluck('full_name', 'id');
    return view('report_create')->with('clients', $clients);
    

    To make it work, you'll also need to define an accessor in the Client model:

    public function getFullNameAttribute()
    {
        return $this->name.' '.$this->surname;
    }
    

    Then just create the list:

    {!! Form::select('connected_with', $clients) !!}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?