mardi 12 septembre 2017

Eloquent does not add foreign key from relation

I could not find an answer to my problem and I hope I can describe it properly. I do hope to be able to provide all necessary information.

Bottom line: Why does the relations parent ID not get injected on creating a new database entry through the parent model.


I have an Occasions model which holds a collection of pictures.

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 to check my data
        $pic = Picture::create(compact('name', 'thumbname'));
        // this line is 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 the relation and the columns used:

HasMany {#206 ▼
  #foreignKey: "pictures.occasion_id"
  #localKey: "id"
  #query: Builder {#205 ▶}
  #parent: Occasion {#215 ▶}
  #related: Picture {#210 ▶}
}

But I'm getting an error message telling me that my occasion_id (in pictures table) is missing a default value. Looking at the built query the occasion_id is indeed missing. What I can't figure out is why said ID does not get injected as I am creating the new picture instance through an occasion-object.

QueryException

SQLSTATE[HY000]: General error: 1364 Field 'occasion_id' doesn't have a default value (SQL: insert into `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 that all necessary information is provided.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire