laravel 5 - Eloquent does not add foreign key from relation -


i not find answer problem , hope can describe properly. hope able provide necessary information.

bottom line: why relations parent id not injected on creating new database entry through parent model.


i have occasions model holds collection of pictures. within addpicture ($name, $filepath) method exception thrown. pointed out rhough code-comments

occasion.php

// namespace + use directives omitted class occasion extends model {     use sluggable;     protected $fillable = [ 'name', 'root-folder', 'path' ];      public function sluggable ()     {         return [             'slug' => [                 'source' => [ 'root-folder', 'name', ],             ],         ];     }      public function pictures ()     {         return $this->hasmany(picture::class);     }     public function addpicture ($name, $filepath)     {         $thumbname = $this->getthumbfilename($filepath);         dump($this,$this->pictures()); // dump check data         $pic = picture::create(compact('name', 'thumbname'));         // line never reached         $pic->createthumb($filepath);     }     ... } 

picture.php:

<?php  namespace app;  use illuminate\database\eloquent\model; use spatie\glide\glideimage;  class picture extends model {     protected $fillable = [ 'name', 'thumbname' ];      public function createthumb ($filepath)     {         $this->ppath = storage_path('app') . "/" . $filepath;         $this->tpath = storage_path('app/public/thumbs') . "/" . $this->getfilename($filepath);         glideimage::create($this->ppath)->modify([ 'w' => 100, 'h' => 100, 'fit' => 'max' ])->save($this->tpath);         $this->save();     }      public function occasion ()     {         return $this->belongsto(occasion::class);     }  /*    public function slideshow ()     {         return $this->belongstomany(slideshow::class);     }*/      private function getfilename ($path)     {         $tmp = array_slice(explode('/', $path), -3);          return str_replace(" ", "-", implode("-", $tmp));     } } 

the result of dump($this->pictures()); shows relation , columns used:

hasmany {#206 ▼   #foreignkey: "pictures.occasion_id"   #localkey: "id"   #query: builder {#205 ▶}   #parent: occasion {#215 ▶}   #related: picture {#210 ▶} } 

but i'm getting error message telling me occasion_id (in pictures table) missing default value. looking @ built query occasion_id indeed missing. can't figure out why said id not injected creating new picture instance through occasion-object.

queryexception  sqlstate[hy000]: general error: 1364 field 'occasion_id' doesn't have default value (sql: insert `pictures` (`name`, `thumbname`, `updated_at`, `created_at`) values (img_0015.jpg, 2006-pvanlage-06-img_0015.jpg, 2017-09-12 19:34:07, 2017-09-12 19:34:07)) 

i hope necessary information provided.

first need add "occasion_id" fillable array in app\picture model secondly, need create occasion object first, pass id addpicture create picture object, see below

public picture extends model{   public $fillable = ['name', 'filepath', 'occasion_id'];    public function occasion(){       $this->belongsto(app\occassion::class);     } }  public function addpicture ($name, $filepath, $occasion_id) {     $thumbname = $this->getthumbfilename($filepath);     dump($this,$this->pictures()); // dump check data     $pic = picture::create(compact('name', 'thumbname', 'occasion_id'));     // line never reached     $pic->createthumb($filepath); } 

there's smarter way believe above suggestion you.


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -