错误laravel SQLSTATE [23000]

时间:2017-09-06 18:14:05

标签: image laravel foreign-keys

我尝试使用与另一个表的关系创建一个函数存储,但是当我尝试在db中创建一个新值时,我有这个问题

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'album_id' cannot be null (SQL: insert into `photos` (`image`, `album_id`, `updated_at`, `created_at`) values (data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAg0AAAGQCAYAAAAtCFOUAAAgAElEQVR4nOydd3hV9f34X3fnZu9FFiQkARpkLxHBgojiaFFbH7RfpKKg1kHF/mqtWCxWoQMHimIRxNZRBUTBwQh7ySbsmQQI2fvue8/vD55zenJy7s1NEJVyXs9zntyc8xnvzzif9/szj27VqlVCWVkZ//rXvzAYDMTGxmKxWNDpdADSX0EQEBF/C4LQ4r7cj/hbdKcMT/5c/lvuRxl+IPdqcarJqBaOv/jlfwP59/e/Wp4FCku8lP78+W0rXYFoS55A8inDaG9YweapMj98Pl+LMALVUXk4er1eNe5gfvuTrS35lekIlFeB6nh78lwt/Wppl/8NVPfVCFTH1ehoXqqlX03+tvyplUegcJRh+mvf5Pirl6Lb9uRBe94ltTxSy6tg2lE1uZXyByubss7K41D7fTmQp1lMm/ydKC0tJSMjo01ZlM/U8kgQBCorK0lISPBbr5V5GKj+NzQ0oNfriYiICChDR/RNID2slPHEiROkpaVhs9lISkqie/fuF9P43nvvCdu2bcNkMjFixAj69etHWFiY34L3+XxSgfh8Pul/vV6PTqdr9VdN6LYMBjEeMQHKjA7WaGirsgeSQ/mi+VNI/sKT+2mPASJXlGrK0l94wVRGuVu1MJQNhNydwWDAYDC0UELKsALFF0h2JfIGX54X8jqhdKsWl/hMqTj9GZTyui1/Jq/T8rCDkV/uNlCdUmsY/BlqbTUO8kZSlF2tXP0ZDf7CDcSlGLb+jG9/9aCt90pMt5h2f2Go1YNg3i15WPI2UB6/Wr27HMjTJm9zxfi8Xq8kn5pBLpevrXxvKx1q9U6O8v0NVP/8pVXtt/x/pX7yer2t9JPFYuHtt9/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11qq8fsz73SYAwk06XQkbDa4ycY4yTYOqZEzHtlPeho+9eeeOW0N56OyhUoXW2NDrSnXfyujAa1d0ze8VEq5o4ijm6Iyl7O+vXrufHGG1X9NTU1sWnTJu64444W98VREDFMNQNBrZ7t27eP22+/HYCPP/4Yk8nE4MGDSU5OprCwkKioKEJCQlq1v2qdRNFokIffv39/PvnkE4wWi4Xw8HDcbrdqZoi43W4aGxuJiIiQepdy4QM1XG0RqNfVXqUpPgsUjr+epr84A/VMAo1sKBujYHp4av7UuJReiTIuf+k1GAyq6ynk1mpbcirLNtC6AX9+1H6rxSOGL0ev10svnlxWf3XAX11QLnZT3pO7D0ZxyO8HWquh9C8Ot8rzUW6sK/21lS755z9P9+7dq+eee05HjhxRR0eHwjCc6WsFAAAzaHR0VHv27FEQBFq8eLE6Ojr0/wFK/2Fk5pVpQAAAAABJRU5ErkJggg==, , 2017-09-06 17:53:01, 2017-09-06 17:53:01))

我理解这条消息,但我不明白为什么它会向我发送此错误,因为我在create方法中传递了这个值,请参阅此代码

public function store(Request $request){

    $albumVar = $this->album->create([
        'title' => $request->get('title'),
        'description' => $request->get('description'),
        'user_id' => $request->get('user_id')
     ]);

    $photosEncoded[] = null;
    $photosCount = 0;
    foreach ($request->file('photo') as $file){
        $names[$photosCount] = $file->getPathName();
        $photosEncoded[$photosCount] = (string)Image::make($names[$photosCount])->encode('data-url');
        $photosCount++;
    }

    for($i = 0; $i<$photosCount; $i++) {
        $photosVar = $this->album->photo()->create([
            'image' => $photosEncoded[$i],
            'album_id' => $albumVar->id
        ]);

        if(!$photosVar)
            return 'falhou';
    }
    if($albumVar)
        dd($albumVar);
    else{

        dd($albumVar);
        return 'falhou 1';
    }
}

当我调试值$ albumVar-&gt; id时,这工作正常......

我的模特照片

class Photo extends Model {
protected $fillable = ['image','album_id'];

public function album(){
    return $this->belongsTo('App\Album');
}

}

我的迁移

    public function up()
{
    Schema::create('photos', function (Blueprint $table) {
        $table->increments('id');
        $table->text('image');
        $table->integer('album_id')->unsigned();
        $table->foreign('album_id')->references('id')->on('albums')->onDelete('cascade');
        $table->timestamps();
    });
}

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

你能尝试这样吗,

$ this-&gt;相册替换为 $ albumVar

$photosVar = $albumVar->photo()->create([
            'image' => $photosEncoded[$i],
            'album_id' => $albumVar->id
        ]);