在 laravel select 框中显示多个变量

Showing multiple variable in laravel select box

我是 laravel 的新手。我想在 laravel 表单中创建一个 select 框,我想在其中的选项中显示 first name and last name 。我的控制器是:

 $clients = Client::pluck('first_name','id')->toArray();
 $clients2 = Client::pluck('last_name','id')->toArray();
 view()->share('clients', $clients);
 view()->share('clients2', $clients2);

这是我的视图文件

 {!! Form::select('client_id', [''=>'--Select Client--']+$clients+ $clients2, null, ['id'=>'client_id', 'class' => 'form-control select2']) !!}

当我执行这段代码时,它只显示 first_name。我怎样才能同时显示 last_name

这可能对您有帮助:

    $clients = Client::select([
        DB::raw('CONCAT(first_name, " ", last_name) AS full_name'),
        'id'
    ])
    ->pluck('full_name','id');

祝你好运!!!

您可以使用 Raw Expressions (DB::raw) 来实现:

$clients = Client::select(
           DB::raw("CONCAT(first_name,' ',last_name) as name"), 'id')
           ->pluck('name', 'id');

更多信息:https://laravel.com/docs/5.6/queries#raw-expressions

这是我的做法,

View::share('selectDestination', Posts::where('post_type','=','destination')->pluck("post_title","id")->all());
//pluck("post_title","id") That's you Option KEY and VALUE

在blade

{!! Form::select('destination',[''=>'--- Select Destination ---']+$selectDestination,null,['class'=>'form-control']) !!}