一对多关系laravel

时间:2018-03-28 05:51:37

标签: php laravel

我正在建立一对多的关系,当我尝试保存它时,它会要求我输入FK,不应该自动执行吗?

class AlternativesCompetitorsImage extends Model
{
    public function alternativecompetitors()
    {
        return $this->belongsTo(AlternativesCompetitor::class,'id');
    }
}



class AlternativesCompetitor extends Model
{

    public function alternativescompetitorsimages(){
        return $this->hasMany(AlternativesCompetitorsImage::class,'alter_comp_id');
    }
}

控制器

$ci = isset($id_image) ? $step->alternativescompetitorsimages :  new AlternativesCompetitorsImage();

        if( $request->hasFile('fileImg')){
            $fileRequests = request()->file('fileImg');
            $count = 0;
            foreach ($fileRequests as $fileRequest) {
            $keyCanvas = $c->key;
            $stepKey = $stepType->key;
            $public= public_path();
            $directory =DIRECTORY_SEPARATOR."canvas".DIRECTORY_SEPARATOR.$keyCanvas.DIRECTORY_SEPARATOR.$stepKey;
            $newName = "image".$count.".png";
            Storage::deleteDirectory($directory);
            $path = $fileRequest->storeAs("public".$directory,$newName);
            $str = str_replace("\\", '/', $path);
            $ci->url_img = $str;
             !isset($id_image) ? $step->alternativescompetitorsimages()->save($ci) : $step->alternativescompetitorsimages()->update($ci->toArray());           
            DB::commit();
            $count++;
            }

迁移

class CreateAlternativesCompetitorsImages extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
         Schema::create('alternatives_competitors_images',function(Blueprint $table){
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->string('url_img',100);
            $table->integer('alter_comp_id')->unsigned();            
            $table->timestamps();


            $table->foreign('alter_comp_id')->references('id')->on('alternatives_competitors');
            });
    }


class CreateAlternativesCompetitors extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('alternatives_competitors',function(Blueprint $table){
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->integer('step_id')->unsigned();
            $table->string('valueA',10);
            $table->string('valueB',10);
            $table->timestamps();

            $table->foreign('step_id')->references('id')->on('steps');
            });
    }
  

Next Illuminate \ Database \ QueryException:SQLSTATE [23000]:完整性   约束违规:1048列'alter_comp_id'不能为空(SQL:   插入alternatives_competitors_imagesurl_img,   alter_comp_idupdated_atcreated_at)值   (public / canvas / de939a01-1438-4aff-bb23-eb4f68653f5f / TEAM / image0.png ,,,   2018-03-27 23:31:12,2018-03-27 23:31:12)in   C:\ XAMPP \ htdocs中\帆布\厂商\ laravel \框架\ SRC \照亮\数据库\ Connection.php:647

1 个答案:

答案 0 :(得分:0)

您没有将alter_comp_id固定为nullable,稍后您尝试创建一个该列为null的行。所以...要么指定该值

$ci->alternativecompetitors()->associate($keyCanvas);

,或者重新迁移表以允许该字段为null,如下所示:

$table->integer('alter_comp_id')->unsigned()->nullable();