在laravel选择框中显示多个变量

时间:2018-08-22 02:35:59

标签: laravel-5 laravel-4 laravel-5.2

我是laravel的新手。我想以laravel形式创建一个选择框,在其中要在选项中显示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

3 个答案:

答案 0 :(得分:1)

这可以为您提供帮助:

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

祝你好运!

答案 1 :(得分:0)

您可以使用const mySearch = require('./search.js'); const express = require('express'); const app = express(); app.get('/', function(req, res) { res.send('Hello World'); }) app.listen(3000, function(){ console.log("Server started on port 3000..."); console.log(mySearch.response)//prints 'undefined' }) 来实现:

Raw Expressions (DB::raw)

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

答案 2 :(得分:0)

这是我的做法,

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

在刀片中

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