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 Illuminate\\Database\\QueryException: 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:\\xampp\\htdocs\\canvas\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Connection.php:647
You didn't stablish alter_comp_id
to be nullable
, and later you try to create a row with that column null. So... Either specify that value
$ci->alternativecompetitors()->associate($keyCanvas);
, or re-migrate the table to allow that field to be null, like this:
$table->integer('alter_comp_id')->unsigned()->nullable();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.