mardi 23 juin 2020

SQL error when saving Laravel form SQLSTATE[42S22]: Column not found: 1054 Unknown column 'bedrooms' in 'field list' (SQL: insert into `properties`

Good I have a form where I keep a property (house, hotel, etc.) but saving the form gives me the following error

SQLSTATE [42S22]: Column not found: 1054 Unknown column 'bedrooms' in 'field list' (SQL: insert into properties and I don't understand why I have my keys to the other tables and everything related

Controller

public function create()
    {
         $offer = Offer_type::all()->pluck('name','id');
         $detail = Detail::all()->pluck('name','id');
         $characteristics = Characteristic::all()->pluck('name','id');
         $property_type = Property_type::all()->pluck('name','id');
         $departamento = Departament::all()->pluck('name', 'id');
         $spaces = Space::all()->pluck('name','id');

        return view('properties.create', compact('departamento','offer','detail','characteristics','property_type','spaces'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $properti=$request->except('_token');
        
             if($request->hasFile('images')){
                 $properti['images']=$request->file('images')->store('uploads','public');
             }
        Propertie::insert($properti);

        flash("Se ha registrado su propiedad De forma exitosa ")->success();

        return redirect()->route('properties.index');
    }
    

my view with the form

@extends('layouts.app')


@section('content')

<div class="container">
      <div class="card">
         <div class="card-body">
           <form class="form-horizontal" action='' method="POST" enctype="multipart/form-data">
               @foreach ($errors->all() as $error)
                <p class="alert alert-danger"></p><br>
                @endforeach
                @csrf
              <div class="form-group row">
                <label for="nameinput" class="col-sm-2 col-form-label">Name</label>
                <div class="col-sm-5">
                  <input type="text" class="form-control" id="nameinput" placeholder="Name" name="name">
                </div>
              </div>

                  <div class="form-check">
                  <input class="form-check-input" type="radio" value="" name="offer_type_id">
                  <label class="form-check-label" for="rent">
                    Rent
                  </label>
                   </div>
                <div class="form-check">
                    <input class="form-check-input" type="radio" value="" name="offer_type_id">
                  <label class="form-check-label" for="sale">
                    Sale
                  </label>
                </div>
             
                 <div class="form-group col-md-6">
                  <label for="inputEmail4">Price</label>
                  <input type="number" class="form-control" id="price" placeholder="price" name="price">
                 </div>
                 
                 
                 
                  <div class="form-row">
                   <div class="form-group col-md-3">
                   
                      <select id="bedrooms" class="form-control" name="bedrooms">
                        <option selected>Habitaciones...</option>
                        <option>1</option>
                      </select>
                    </div>
                    <div class="form-group col-md-3">
              
                      <select id="bathrooms" class="form-control" name="bathrooms">
                        <option selected>Baños...</option>
                        <option>1</option>
                      </select>
                    </div>
                    <div class="form-group col-md-3">
                
                      <select id="parking" class="form-control" name="parking">
                        <option selected>Estacionamientos...</option>
                        <option>2</option>
                      </select>
                    </div>
                        <div class="col-sm-3">
                          <label class="sr-only" for="inlineFormInputName">area</label>
                          <input type="text" class="form-control" id="inlineFormInputName" placeholder="Area" name="area">
                        </div>
                     </div>
                     
                     <div class="form-row">
                         <div class="form-group col-md-3">
                         <select id="year" class="form-control" name="antiquity">
                             <option selected>Antiguedad ...</option>
                            <option value=""></option>
                            
                            @for ($year = -2020; $year <= -1990; $year++)
                            <option value=""></option>
                            @endfor
                        </select>
                        </div>
                        <div class="form-group col-md-3">
                        <select class="form-control" name="furnished">
                            <option value="" selected >Amobado</option>
                            <option value="Si">Si</option>
                            <option value="No">No</option>
                        </select>
                        </div>
                        <div class="form-group col-md-3">
                
                          <select id="floor" class="form-control" name="floor">
                            <option selected>Pisos...</option>
                            <option>1</option>
                          </select>
                        </div>
                        
                    </div> 
                     <div class="form-group">
                        <label for="exampleFormControlTextarea1">Example textarea</label>
                        <textarea class="form-control" id="exampleFormControlTextarea1" rows="3" name="description"></textarea>
                      </div>
                      <div class="input-group mb-3">
                      <div class="input-group-prepend">
                        <span class="input-group-text">Upload</span>
                      </div>
                      <div class="custom-file">
                        <input type="file" class="custom-file-input" id="inputGroupFile01" name="images">
                        <label class="custom-file-label" for="inputGroupFile01">Choose file</label>
                      </div>
                    </div>
                 
               
                   <div id="map" style="display:block;height:100vh;width:70vw"></div>
    <div class="form-group col-md-6">
        <label for="formGroupExampleInput">Ciudad</label>
        <select name="departaments" id="departaments" class="custom-select mr-sm-2">
            @foreach($departamento as $key => $departament)
            <option hidden selected>Selecciona un Departamento</option>
            <option value="">  </option>
            @endforeach

        </select>
    </div>
    <div class="form-group col-md-6">
        <label for="formGroupExampleInput">Municipio</label>
        <select name="municipalities" id="municipalities" class="custom-select mr-sm-2">
            <option hidden selected>Selecciona un Municipio</option>
        </select>
    </div>
    <div class="form-group col-md-6">
        <label for="formGroupExampleInput2">Direccion</label>
        <input type="text" class="form-control" id="address" placeholder="Direccion" name="address">
        <button type="button" name="search" id="search">Buscar</button>
    </div>
    <div class="form-group col-md-6">
        <input type="hidden" class="form-control" id="lat" placeholder="lat" name="lat">
    </div>
    <div class="form-group col-md-6">
        <input type="hidden" class="form-control" id="long" placeholder="long" name="long">
    </div>
    
     <button type="submit" class="btn btn-primary my-1">Crear</button>
              
            </form>
            </div>
        </div>
    </div>


@endsection

@section('scripts')
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDOoFVMgp-Ydw4EkWQa1HgNYlLDaTzVaFw" async defer></script>
<script>
    var geocoder;
    var map;

    function initMap() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(4.570868, -74.2973328);
        var mapOptions = {
            zoom: 7,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            mapTypeControl: true,
            mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
            },
            navigationControl: true,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById('map'), mapOptions);
    }

    function codeAddress(address) {

        geocoder.geocode({
            'address': address
        }, function(results, status) {
            if (status == 'OK') {
                if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
                    map.setCenter(results[0].geometry.location);
                    map.setZoom(18);
                    var infowindow = new google.maps.InfoWindow({
                        content: '<b>' + address + '</b>',
                        size: new google.maps.Size(150, 50)
                    });
                    var marker = new google.maps.Marker({
                        position: results[0].geometry.location,
                        map: map,
                        title: address,
                        animation: google.maps.Animation.DROP,
                        icon: "/images/location-propertiess.png",
                    });
                    google.maps.event.addListener(marker, 'click', function() {
                        infowindow.open(map, marker);

                    });
                    $("#lat").val(results[0].geometry.location.lat())
                    $("#long").val(results[0].geometry.location.lng())
                } else {
                    alert('No se encontraron resultados');
                }
            } else {
                alert('Geocode was not successful for the following reason: ' + status);
            }
        });
    }
    $(function() {
        initMap();
        $('#search').on('click', function(e) {
            e.preventDefault();
            if ($('#address').val() == '' || $('#address').val() == NaN) {
                alert('Ingrese una direccion valida');
                return;
            }

            codeAddress($('#address').val() + ', ' + $('#city').val() + ' Colombia');

        })


    })
</script>

@endsection

migration

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePropertiesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('properties', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->nullable;
            $table->string('price')->nullable;
            $table->text('description')->nullable;
            $table->unsignedBigInteger('property_type_id')->nullable();
            $table->unsignedBigInteger('offer_type_id')->nullable();
            $table->unsignedBigInteger('spaces_id')->nullable();
            $table->unsignedBigInteger('departaments_id')->nullable();
            $table->unsignedBigInteger('municipalities_id')->nullable();
            $table->unsignedBigInteger('details_id')->nullable();
            $table->unsignedBigInteger('characteristics_id')->nullable();
            $table->integer('images')->nullable;
            $table->string('url');
            $table->float('lat');
            $table->float('lng');
            $table->string('direction');
            
            $table->timestamps();
            
            $table->foreign('property_type_id')->references('id')->on('property_type');
            $table->foreign('offer_type_id')->references('id')->on('offer_type');
            $table->foreign('spaces_id')->references('id')->on('spaces');
            $table->foreign('departaments_id')->references('id')->on('departaments');
            $table->foreign('municipalities_id')->references('id')->on('municipalities');
            $table->foreign('details_id')->references('id')->on('details');
            $table->foreign('characteristics_id')->references('id')->on('characteristics');

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('properties');
    }
}

I have a one-to-many relationship from my model properties to the other models

I don't understand what causes that error if said table is in my migration of spaces

     *
     * @return void
     */
    public function up()
    {
        Schema::create('spaces', function (Blueprint $table) {
            $table->id();
            $table->string('bedrooms');
            $table->string('bathrooms');
            $table->string('parking');
            $table->string('area');
            $table->timestamps();
        });
    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire