Wednesday, March 15, 2017

LARAVEL - Correct way to insert data in multiple tables

I have a question about how to structure the code the right way when I add data to related tables.

I have this, and it works. But is that the correct way? Seems a bit messy

Relations tables:

Property:

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();
        });

House:

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();
        });

Photos

 Schema::create('fotos', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('imovel_id');
            $table->string('nome',15);
            $table->timestamps();
        });

And to insert a House i have that function in HouseController, and works... but seem kind a messy...

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');


    }



via D. Lopes

Advertisement