Eloquent不会从关系中添加外键

时间:2017-09-12 19:49:58

标签: laravel-5 eloquent laravel-5.4 laravel-eloquent

我无法找到问题的答案,我希望我能正确描述它。我希望能够提供所有必要的信息。

底线:为什么在通过父模型创建新数据库条目时不会注入关系父ID。

我有一个Occasions模型,其中包含一组图片。在addPicture ($name, $filepath)方法中抛出异常。正如指出rhough代码注释

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

dump($this->pictures());的结果显示了关系和使用的列:

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

但是我收到一条错误消息,告诉我我的occasion_id(在图片表中)缺少默认值。查看构建的查询,确实缺少occasion_id。我无法弄清楚的是为什么说ID不会被注入,因为我正在通过一个场合对象创建新的图片实例。

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))

我希望提供所有必要的信息。

1 个答案:

答案 0 :(得分:1)

首先,您需要在App \ Picture模型中将“occasion_id”添加到可填充数组中 其次,你需要先创建场合对象,然后传递ID addPicture来创建图片对象,见下文

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 to check my data
    $pic = Picture::create(compact('name', 'thumbname', 'occasion_id'));
    // this line is never reached
    $pic->createThumb($filepath);
}

有一种更聪明的方法可以做到这一点,但我相信上述建议会对你有帮助。

相关问题