View in select database attributes and edit with Laravel

Asked

Viewed 1,335 times

2

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

Save multiple attributes to the same object in Laravel

It would now need that what was set in this auxiliary table is 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>
  • You failed to resolve this change problem when attributes come for editing?

  • 1

    What is your email?

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']) }}

the Edit():

public function edit($id)
{
    $property = Property::with('attribute')->find($id);
    $attributes = Attribute::lists('name', 'id');
    return view('/property/edit', ['property' => $property, 'attributes' => $attributes]);
}
  • Badmethodcallexception in Builder.php line 2450: Call to Undefined method Illuminate Database Query Builder::lists()

  • What version of the Standard ta using?

  • I was able to make it work by putting the controller up as it is. The only thing missing is that in the edit form appears selected what is set in the database

  • that makes selecting: $property->attribute->lists('chave estrangeira de atributos na tabela pivot')

  • What would this pivot table be? The lists are giving the above error. The Laravel is 5.3. I gave a dd($Property) before returning the edit view and the parameters from the bank are already correct. It would not only be in the Blade foreach that it should change?

  • in 5.3 changed, change the lists() for **all()->pluck('name', 'id')->all()**.

  • I put it that way, but it didn’t work out: public Function Edit($id) { $Property = Property::with('attribute')->find($id); $Property->attribute->Pluck('id_imovel', 'id_attribute'); $Property->all(); Return view('/Property/Edit')->with('Property', $Property); }

  • public function edit($id)&#xA;{&#xA; $property = Property::with('attribute')->find($id);&#xA; $attributes = Attribute::all()->pluck('name', 'id')->all();&#xA; return view('/property/edit', ['property' => $property, 'attributes' => $attributes]);&#xA;}

  • Errorexception in fc19f02995d308d94a071f0bad6cbf62125b051e.php line 237: Trying to get Property of non-object (View: /var/www/html/imovan/Resources/views/Property/Editblade.php)

  • Would just fiddling with the Edit foreach not work? Because the attributes are being passed to the form

  • and in the view: {{ Form::select('atributos[]', $attributes, $property->attribute->all()->pluck('id')->all(), ['multiple' => 'multiple', 'class' => 'form-control']) }}

  • But this is some plugin?

  • https://laravelcollective.com/docs/5.3/html

  • There is no way to just change the foreach without having to use it and touch the controller?

Show 9 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.