I have a property table of other properties and created a property_table_table since an property can have several attributes. I have created the models Imovel and Attribute, I need to create an Imovel_atributo model? How could I do in the store method to save multiple attributes on a property?
Model Property:
<?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 Property:
<?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');
}
}