vendredi 26 juin 2020

SQLSTATE[3000]: Integrity constrain violation Laravel relations model

Good, I do not understand why when saving a record in my bd, this error appears to me, I have my migrations and my models, if I remove that key from my bd it saves the record well

model propertie

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Propertie extends Model
{
    //
    public function characteristic()
    {

        return $this->HasMany('App\Characteristic', 'property_id');
        
    }
    public function departament()
    {

        return $this->belongsTo('App\Departament');
        
    }
    
    public function municipality()
    {

        return $this->belongsTo('App\Detail');
        
    }
    
    public function detail()
    {

        return $this->belongsTo('App\Detail');
        
    }
    
    public function offer_type()
    {

        return $this->belongsTo('App\Offer_type','offer_type_id');
        
    }
    
    public function property_type()
    {

        return $this->hasOne('App\Property_type','id','property_type_id');
        
    }
    public function space()
{

    return $this->hasOne('App\Space', 'property_id');
    
}
    
     public function ImgProperties()
    {
        return $this->hasMany('App\ImgProperties');
    }
    
    protected $fillable = [
        'name', 'description', 'property_type_id', 'offer_type', 'spaces_id', 'departaments_id',
        'municipalities_id', 'details_id', 'characteristics_id', 'images','url', 'lat', 'lng','direction','price'

    ];
    
    public function scopeName($query, $name)
    {
        if($name){
            
             return $query->where('name', 'LIKE',"%$name%");
        }
       
    }
    
    public function scopeLatest($query)
    {
        return $query->latest();
    }
    
    public function scopeTypeOf($query, $type)
    {
        
        return $query->where('offer_type_id', 1);
    }
    
     public function scopeSortType($query, $type)
    {
        
        if($type == 'new'){
            return $query->latest();
        }
     }
    
    
    
}

model property_type

namespace App;
use App\Propertie;
use Illuminate\Database\Eloquent\Model;

class Property_type extends Model
{
        public $table = "property_type";

    protected $fillable = [
        'type_property'

    ];
    
    public function properties()
    {
        return $this->hasMany('App\Propertie');
    }

as you can see I have the relationships, a property can have a property type

property migration

    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->string('images')->nullable;
            $table->float('lat')->nullable;
            $table->float('lng')->nullable;
            $table->string('address')->nullable;
            
            $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')->onDelete('cascade');

property type migration

 public function up()
    {
        Schema::create('property_type', function (Blueprint $table) {
            $table->id();
          
            $table->string('type_property');
            $table->timestamps();
        });
    }

    /**

if you can help me find the error i would appreciate it enter image description here



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire