4
I have an immovable table other of attributes and created an immovel_attribute table since an immovable can have several attributes. I created the models Immovel and Attribute, need to create a model Immovel_attribute? How could you do in the store method to save several attributes in an immovable?
Model Imovel:
<?php
namespace Imovan;
use Illuminate\Database\Eloquent\Model;
class Property extends Model
{
protected $fillable = [
'nome', 'disponivel_venda', 'valor_venda', 'disponivel_locacao', 'valor_locacao', 'descricao', 'observacao', 'dormitorios', 'garagens', 'area_util', 'area_total', 'novo', 'comercial', 'lancamento', 'cep', 'endereco', 'numero', 'complemento', 'bairro', 'cidade', 'estado', 'condominio', 'nome_condominio', 'fotos',
];
public function attribute()
{
return $this->belongsTo('App\Attribute');
}
public function type()
{
return $this->belongsTo('App\Type');
}
public function owner()
{
return $this->hasOne('App\Owner');
}
}
Model Attribute:
<?php
namespace Imovan;
use Illuminate\Database\Eloquent\Model;
class Attribute extends Model
{
protected $fillable = [
'nome',
];
public function property()
{
return $this->hasMany('App\Property');
}
}
Controller Imovel:
<?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)
{
$params = $request->all();
$property = new Property($params);
$property->save();
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::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->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');
}
}
I made the changes to the models and am passing the array to the controller store method, but I didn’t understand how to save. I also didn’t understand why of use of find().
– Marcelo
If you followed the examples, if you passed the
array
in the methodstore
and then gave aattach
to save in the relationship, it is because managed to save, you have to check in the database. When I saw your question I realized that the doubt was a little bigger, so I put a minimal example. Thefind
has the purpose of fetching an item from your bank if it is satisfactory you can save the relations as is in the reply and this is an example. In casestore
is generating a newproperty
after saving use$propery->attribute()->attach([1,2]);
to save to relation. Any doubt post 1 comment– novic
I was able to save the immovable id in the database with 2 attribute ids, but I chose 3 attributes: $Property->attribute()->attach([1,2])?
– Marcelo
@Marcelo ai que tá, as you are sending, this is a screen with
checkbox
for example and when selecting send together the information of theproperty
... What would your screen look like? Perhaps a new question would be better because it is explains you the process as it works recording and removing many for many in Eloquent.– novic
I’m using a select to send data to the controller: <select name="attributes[]" Multiple="Multiple" class="form-control"> @foreach($Attributes as $value) <option value="{$value->id}}">{{$value->name}</option> @endforeach </select>
– Marcelo
@Marcelo just assign
$b = Property::find(1);
$b->attribute()->attach($request->input('atributos', []);
– novic
It worked, thank you.
– Marcelo
I don’t know if I should open a new topic for this, but regarding the editing of attributes I use the Sync method like this: public Function Edit($id) { $Property = Property::find($id); $Property->attribute($id)->Sync([]); Return view('/Property/Edit')->with('Property',; }
– Marcelo
@Marcelo open another question and correlate with this. and something else is like this line of code:
$property->attribute()->sync([1,2,3]);
– novic
I will open another. Regarding the line that posed my problem is that I do not know the amount of attributes that will exist because the user who will register.
– Marcelo