之旅:
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'没有默认值' –