Display on select bank attributes and edit with Laravel

2

I was able to include the attributes through an auxiliary table by this question:

Save multiple attributes to the same object in Laravel

I would now need what was set in this helper table to be selected in the edit form

Controller:

<?php

namespace Imovan\Http\Controllers;
use Imovan\Property;
use Imovan\Type;
use Imovan\Attribute;
use Imovan\Owner;
use Imovan\Http\Requests\PropertyRequest;

class PropertyController extends Controller
{
public function __construct()
{
    $this->middleware('auth');
    $types = Type::all(); //Passa variaveis para todas as views
    view()->share(compact('types')); //Passa variaveis para todas as views
    $attributes = Attribute::all();
    view()->share(compact('attributes'));
    $owners = Owner::all();
    view()->share(compact('owners'));
    $properties = Property::all();
    view()->share(compact('properties'));
}
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    $properties = Property::all();
    return view('/property/index')->with('properties', $properties);
}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    return view('/property/create');
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(PropertyRequest $request)
{
    //dd($request);
    $params = $request->all();
    $property = new Property($params);
    $property->save();
    $property->attribute()->attach($request->input('atributos', []));
    return redirect()->action('PropertyController@index');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    $property = Property::with('attribute')->find($id);
    return view('/property/edit')->with('property', $property);
}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(PropertyRequest $request, $id)
{
    $params = $request->all();
    $property = Property::find($id);
    $property->attribute()->sync($request->input('atributos', []));
    $property->update($params);
    return redirect()->action('PropertyController@index');
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    $property = Property::find($id);
    $property->delete();
    return redirect()->action('PropertyController@index');
}
}

Select:

<div class="col-md-4">
  <div class="form-group">
    <label>Atributos</label>
    <select name="atributos[]" multiple="multiple" class="form-control">
      @foreach($attributes as $value)
        <option {{$property->atributo == $value->id ? 'selected' : '' }}  value="{{ $value->id }}">{{$value->nome}}</option>
      @endforeach
    </select>
  </div>
</div>
    
asked by anonymous 18.12.2016 / 16:12

1 answer

1

Do the following:

the blade

select: {{ Form::select('atributos[]', $attributes, $property->attribute->lists('chave estrangeira de atributos na tabela pivot'), ['multiple' => 'multiple', 'class' => 'form-control']) }}

or edit ():

public function edit($id)
{
    $property = Property::with('attribute')->find($id);
    $attributes = Attribute::lists('name', 'id');
    return view('/property/edit', ['property' => $property, 'attributes' => $attributes]);
}
    
19.12.2016 / 14:14