Add more fields in a select with pluck laravel

1

Hi, I want to show more data in a select with the pluck method, does anyone know how to do it?

It turns out that I have a Course table, which has a name and type as attributes. I want to give a course to a student, but with the pluck that I have only shows me the name of the course, I want it to appear with the type also in the select.

This is my controller

public function edit($id) 
{
    $alumno = Alumno::find($id);

    $cursos = Curso::orderBy('id','ASC')->pluck('nombre','id');

    $alumno->curso; 

    $apoderados = Apoderado::orderBy('id','ASC')->pluck('nombre','id'); 
    $alumno->apoderado;  

    return view('alumnos.edit')->with('alumno', $alumno)->with('cursos',$cursos)->with('apoderados',$apoderados);
}

How can I concatenate that course name with the type ?? help

    
asked by Edgardo Escobar 11.04.2017 в 22:09
source

1 answer

1

For that, the best thing is to create an Accessor with the attributes you need: link

/**
 * Obtener el nombre y tipo de curso.
 *
 * @return string
 */
public function getNameAndTypeAttribute()
{
    return $this->tipo . ' ' . $this->nombre;
}

In pluck you call it like this:

...->pluck('name_and_type','id');
    
answered by 11.04.2017 / 22:38
source