Laravel Form, Select from a Foreign Key

2

Table Products:

  

Primary Key- > product_code

Movements Table:

  

Primary Key- > code_movement / Foreign Key- > product_code

These would be the two tables to relate, and I use the Laravel collective

I'm supposed to stay like this:

In MovimientosController

$productos = Producto::lists('nombre','codigo_producto'); /Este a veces me da error por que no se exactamente como ponerlo si en una funcion publica o como.

In create.blade

{!! Form::label('codigo_producto', 'Código del Producto') !!}
{!! Form::text('codigo_producto', $productos, null, ['class' => 'form-control', 'placeholder' => 'Código del producto', 'required']) !!}

In edit.blade

{!! Form::label('codigo_producto', 'Código del Producto') !!}
{{ Form::model($productos, array('route' => array('admin.productos.index', $productos->codigo_producto))) }}

But the fact is that the field disappears from the form directly or appears, I leave it as it was and of course it works, but it does not show the foreign keys.

    
asked by Alberto Cepero de Andrés 07.09.2017 в 09:02
source

1 answer

0

The method lists no longer works, you have to use pluck .
link
Retrieving A List Of Column Values

If you would like to retrieve a Collection containing the values of a single column, you may use the pluck method. In this example, we'll retrieve a Collection of role titles:

$titles = DB::table('roles')->pluck('title');

foreach ($titles as $title) {
    echo $title;
}

You may also specify a custom key column for the return Collection:

$roles = DB::table('roles')->pluck('title', 'name');

foreach ($roles as $name => $title) {
    echo $title;
}
    
answered by 07.09.2017 / 14:34
source