laravel 反向一对一_更新在一对一的关系表laravel

之旅:

id|title|content

featured_image:

id|tour_id|name|path

我模特FeaturedImage.php:

class FeaturedImage extends Model

{

protected $fillables = [

'name',

'path',

'tour_id',

];

public function tour()

{

return $this->belongsTo('App\Tour');

}

}

Tours.php

class Tour extends Model

{

protected $fillables = [

'title',

'content',

];

public function featuredImage()

{

return $this->hasOne('App\FeaturedImage');

}

}

我试图更新featured_image表时为旅游新形象被上传:

更新path列featured_image表与新的文件的路径

删除旧图像

下面是我更新featured_image的路径列方法:

// update featured image

if ($request->hasFile('featured_image')) {

$featured_image= new FeaturedImage;

// add the new photo

$image = $request->file('featured_image');

$filename = $image->getClientOriginalName();

$location = 'images/featured_image/'.$filename;

//dd($location);

Image::make($image)->resize(800, 400)->save($location);

$oldFilename= $tour->featuredImage->path;

// update the database

$featured_image->path = $location;

// Delete the old photo

File::delete(public_path($oldFilename));

}

上面的代码成功地删除旧的图像,并上传新的,但它无法更新的路径列。我运行了dd($location);,它给出了新图像的路径,但没有;保存在db列中。

+0

'$ featured_image->保存()'分配新的位置到'path'财产之后。 '$ featured_image-> path = $ location; $ featured_image-> save();' –

+0

我这样做,并得到错误'SQLSTATE [HY000]:一般错误:1364字段'tour_id'没有默认值' –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值