关于Laravel 5.7中Pluck结果的问题

时间:2018-12-08 17:36:49

标签: laravel laravel-query-builder laravel-5.7 laravel-collection

我对Laravel中的pluck方法有疑问。如何获得列表中的以下结果?

Name + Lastname + id

我在控制器中使用此行命令

行:$choferes

public function index()
{
    $asignaciones = Asignacion::orderBy('id', 'ASC')->get();
    $choferes = Chofer::orderBy('id', 'ASC')->pluck('nombre', 'id');
    $dueños = Dueño::orderBy('id', 'ASC')->pluck('nombre', 'id');
    $asignaciones->each(function ($asignaciones)
    {
        $asignaciones->chofer;
        $asignaciones->dueno;
    });

    return view('admin.asignaciones.index')
        ->with('asignaciones', $asignaciones)
        ->with('choferes', $choferes)
        ->with('dueños', $dueños);
}

列表中的结果

1 => "name 1"
2 => "name 2"

但是,我需要结合:namelastname。它必须是一个列表。如何在我的视图中显示它?

<div class="col-xs-6">
    {!! Form::label('chofer_id','Lista de Choferes') !!}
    {!! Form::select('chofer_id[]', $choferes, null,
        ['class'=>'form-control select_chofer',
        'multiple',
        'required','id'=>'list_chofer']) !!}
</div>

我有这个结果:

result

谢谢!

1 个答案:

答案 0 :(得分:0)

在您的Sub DelBlankAndZeroRowsDontKnowHowIGotOutOfMyBedThisAfternoonVersion() Dim rw As Long, i As Long, j As Long Dim rng As Range, rngU As Range rw = Cells(Rows.Count, 1).End(xlUp).Row For i = rw To 6 Step -1 Set rng = Cells(i, 3).Resize(1, 13) For j = 1 To rng.Columns.Count If rng.Cells(1, j).Value = 0 Then If Not rngU Is Nothing Then Set rngU = Union(rng.Cells(1, 1), rngU) Else Set rngU = rng.Cells(1, j) End If End If Next Next rngU.Rows.Hidden = True Set rngU = Nothing Set rng = Nothing End Sub Sub DelBlankAndZeroRowsThinkImGonnaStayInBedTodayVersion() Dim rw As Long, i As Long, j As Long Dim rng As Range, rngU As Range rw = Cells(Rows.Count, 1).End(xlUp).Row For i = rw To 6 Step -1 Set rng = Cells(i, 3).Resize(1, 13) For j = 1 To rng.Columns.Count If rng.Cells(1, j).Value = 0 Then If Not rngU Is Nothing Then Set rngU = Union(rng.Cells(1, 1), rngU) Else Set rngU = rng.Cells(1, j) End If End If Next Next rngU.Rows.Hidden = True Set rngU = Nothing Set rng = Nothing End Sub Sub DelBlankAndZeroRowsNeverGonnaGetUpVersion() Dim rw As Long, i As Long, j As Long, rng As Range, rngU As Range rw = Cells(Rows.Count, 1).End(xlUp).Row: For i = rw To 6 Step -1 Set rng = Cells(i, 3).Resize(1, 13): For j = 1 To rng.Columns.Count If rng.Cells(1, j).Value = 0 Then If Not rngU Is Nothing Then Set rngU = Union(rng.Cells(1, 1), rngU) Else: Set rngU = rng.Cells(1, j): End If: End If: Next: Next rngU.Rows.Hidden = True: Set rngU = Nothing: Set rng = Nothing: End Sub 模型中定义

Chofer

在控制器中使用此

public funcion getFullNameAttribute()
{
    return $this->attributes['first_name'] + ' ' + $this->attributes['last_name'];
}