无法添加或更新子行:外键约束失败(Laravel)

问题描述:

我有错误,并且我不知道如何正确使用Eloquent关系:c 类别编号采取正确,但用户标识为错误。我认为这是从表modelsid,但需要modelsuser_id 这里是我的表:无法添加或更新子行:外键约束失败(Laravel)

photoset_categories目录同photoset类型,例如:id = 1, name = 'Studio'; id = 2, name = 'Portrait'

Schema::create('photoset_categories', function (Blueprint $table) { 
    $table->increments('id'); 
    $table->string('name')->comment('Category name'); 
}); 

模型模型数据:眼睛的颜色,身高,体重等。

Schema::create('models', function (Blueprint $table) { 
    $table->increments('id'); 
    $table->integer('user_id')->comment('Model id from users'); 
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); // Foreign key 

    $table->timestamps(); 
}); 

model_photosets在该照片拍摄模式拍摄

Schema::create('model_photosets', function (Blueprint $table) { 
    $table->increments('id'); 
    $table->integer('model_id')->unsigned()->index(); 
    $table->foreign('model_id')->references('user_id')->on('models')->onDelete('cascade'); // Foreign key 
    $table->integer('category_id')->unsigned()->index(); 
    $table->foreign('category_id')->references('id')->on('photoset_categories')->onDelete('cascade'); // Foreign key 
}); 

下面是DB型号:

class PhotosetCategory extends Model 
{ 
    protected $table = 'photoset_categories'; 
    protected $guarded = ['name']; 

    public function modelPhotoset() 
    { 
     return $this->belongsToMany('App\Models'); 
    } 
} 

...

class Models extends Model 
{ 
    protected $table = 'models'; 
    protected $fillable = ['user_id', 'd_birth', 'birth', 'm_birth', 'y_birth', 'hair_color', 'eyes_color', 'growth', 'weight']; 
    protected $dates = ['created_at', 'updated_at']; 


    public function user() { 
     return $this->belongsToMany('App\User'); 
    } 

    public function photosets() 
    { 
     return $this->belongsToMany('App\PhotosetCategory', 'model_photosets', 'model_id', 'category_id'); 
    } 
} 

...

class ModelPhotoset extends Model 
{ 
    protected $table = 'model_photosets'; 
    protected $fillable = ['user_id', 'category_id']; 
} 

控制器ModelsController

class ModelsController extends Controller 
{ 
    public function editData(Request $request) 
    { 
     $title = 'Model data'; 

     $data = Models::where('user_id', Auth::id())->first(); 

     $all_types = PhotosetCategory::all(); // List of photo shoot catagories 

     if ($request->has('save')) { 
      $this->validate($request, [ 
       // ... 
      ]); 

      // UPDATE USER PRO SHOOTS DATA 
      $data->photosets()->sync($request->get('ps_types')); 
     } 

     return view('model.edit', [ 
      'title' => $title, 
      'data' => $data, 
     ]); 
    } 
} 

错误:

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (delavour . model_photosets , CONSTRAINT model_photosets_model_id_foreign FOREIGN KEY (model_id) REFERENCES models (user_id) ON DELETE CASCADE) (SQL: insert into model_photosets (category_id , model_id) values (2, 2))

错误表明你正在尝试插入model_photosets与MODEL_ID 2,但在车型纪录ID 2行得退出。

+0

我知道。 “2” - 模型表中的字段“id”。但我需要获取字段'user_id':c 屏幕截图: https://drive.google.com/open?id=0B8BExJ-ydlwLUVVBM3BkYk5LOFk – MyZik