Wednesday, March 28, 2018

one to many relationship laravel - development

one to many relationship laravel

I am making a one to many relationship, when I try to save it, it asks me to enter the FK should not I do it automatically?

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

Controller

$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++;
            }

Migrations

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 IlluminateDatabaseQueryException: SQLSTATE[23000]: Integrity
constraint violation: 1048 Column ‘alter_comp_id’ cannot be null (SQL:
insert into alternatives_competitors_images (url_img,
alter_comp_id, updated_at, created_at) values
(public/canvas/de939a01-1438-4aff-bb23-eb4f68653f5f/TEAM/image0.png, ,
2018-03-27 23:31:12, 2018-03-27 23:31:12)) in
C:xampphtdocscanvasvendorlaravelframeworksrcIlluminateDatabaseConnection.php:647



from Laravel Questions and Answers https://laravelquestions.com/php/one-to-many-relationship-laravel/
via Lzo Media

No comments:

Post a Comment