我有一个关于如何在向相关表添加数据时以正确的方式构建代码的问题。
我有这个,它有效。但这是正确的方法吗? 看起来有点凌乱
关系表:
属性:
public function up()
{
Schema::create('imoveis', function (Blueprint $table) {
$table->increments('id');
$table->integer('tipoImovel_id');
$table->string('finalidade',20);
$table->string('titulo',100);
$table->date('data')->nullable();
$table->integer('concelho_id');
$table->string('freguesia',50);
$table->string('rua',150);
$table->decimal('long', 10, 7)->nullable();
$table->decimal('lat', 10, 7)->nullable();
$table->boolean('destaque');
$table->boolean('estado')->default(true);
$table->string('descricao',500);
$table->string('preco',40);
$table->integer('empresa_id')->default(1);
$table->timestamps();
});
行:
public function up()
{
Schema::create('moradias', function (Blueprint $table) {
$table->integer('imovel_id');
$table->integer('nrPisosConstrucao');
$table->integer('nrQuartos');
$table->integer('nrWcs');
$table->integer('areaConstrucao');
$table->integer('areaTerreno');
$table->smallInteger('anoConstrucao');
$table->timestamps();
});
照片
Schema::create('fotos', function (Blueprint $table) {
$table->increments('id');
$table->integer('imovel_id');
$table->string('nome',15);
$table->timestamps();
});
要插入一个House我在HouseController中有这个功能,并且工作......但看起来有点混乱......
public function create(){
//save Property(BASE)
$this->imovel->titulo = Input::get('titulo');
$this->imovel->finalidade = Input::get('finalidade');
$this->imovel->data = Input::get('data');
$this->imovel->concelho_id = Input::get('concelho');
$this->imovel->freguesia = Input::get('freguesia');
$this->imovel->rua = Input::get('rua');
$this->imovel->long = Input::get('longitude');
$this->imovel->lat = Input::get('latitude');
$this->imovel->descricao = Input::get('descricao');
$this->imovel->preco = Input::get('preco');
$this->imovel->tipoimovel_id = 1; //Empresa-
$this->imovel->save();
//save House
$moradia= new Moradia;
$moradia->imovel_id = $this->imovel->id;
$moradia->nrQuartos = Input::get('nrQuartos');
$moradia->nrPisosConstrucao = Input::get('nrPisos');
$moradia->nrWcs = Input::get('nrWcs');
$moradia->areaConstrucao = Input::get('areaConstrucao');
$moradia->areaTerreno = Input::get('areaTerreno');
$moradia->anoConstrucao = Input::get('anoConstrucao');
$moradia->save();
//upload photos
$files = Input::file('images');
$contaFotos=0;
foreach ($files as $file) {
$rules = array('file' => 'required'); //'required|mimes:png,gif,jpeg,txt,pdf,doc'
$validator = Validator::make(array('file'=> $file), $rules);
if($validator->passes()){
$extension = $file->getClientOriginalExtension();
$destinationPath = 'uploads'; //folder
$filename = $this->imovel->id . '_'. $contaFotos++ . '.' . $extension;
$upload_success = $file->move($destinationPath, $filename);
// SAVE DB
$extension = $file->getClientOriginalExtension();
$foto= new Foto();
$foto->imovel_id = $this->imovel->id;
$foto->nome = $filename;
$this->imovel->fotos()->save($foto);
}
}
return view('admin.imoveis.index');
}
模型中的关系
public function atributos(){
//se for moradia
if ($this->tipoimovel_id == 1) {
return $this->hasOne(Moradia::class);
}
//se for apartamento
else if ($this->tipoimovel_id == 2) {
return $this->hasOne(Apartamento::class);
}
//se for loja
else if ($this->tipoimovel_id == 3) {
return $this->hasOne(Loja::class);
}
//se for armazem
else if ($this->tipoimovel_id == 4) {
return $this->hasOne(Armazem::class);
}
//se for terreno para construção
else if ($this->tipoimovel_id == 5) {
return $this->hasOne(TerrenoConstrucao::class);
}
// se for terreno para outros fins
else if ($this->tipoimovel_id == 6) {
return $this->hasOne(TerrenoOutrosFins::class);
}
答案 0 :(得分:1)
这主要取决于您的关系,以下方式适用于one to many
和one to one
。
$moradia= new Moradia;
//Removed insertion for id from here.
$moradia->nrQuartos = Input::get('nrQuartos');
$moradia->nrPisosConstrucao = Input::get('nrPisos');
$moradia->nrWcs = Input::get('nrWcs');
$moradia->areaConstrucao = Input::get('areaConstrucao');
$moradia->areaTerreno = Input::get('areaTerreno');
$moradia->anoConstrucao = Input::get('anoConstrucao');
// This is what i added
$this->imovel->atributos() -> save($moradia);
图像也是如此。