Php Laravel返回UploadedFile对象而不是图像路径
在我尝试Php Laravel返回UploadedFile对象而不是图像路径,php,laravel,Php,Laravel,在我尝试dump请求的数据时,在控制器中的update方法中 所有属性都正常,但图像打印为对象而不是保存在数据库中的图像路径 更新方法 public function update(Request $request, SlideShow $slideshow) { $slideshow->update($request->validate([ 'title_en' => 'required', '
dump
请求的数据时,在控制器中的update
方法中
所有属性都正常,但图像
打印为对象
而不是保存在数据库中的图像路径
更新方法
public function update(Request $request, SlideShow $slideshow)
{
$slideshow->update($request->validate([
'title_en' => 'required',
'title_ar' => 'required',
'link' => 'nullable|url',
'image' => 'image'
]));
dd($slideshow);
$slideshow->uploadImage();
session()->flash('success', __('dashboard.slideshow.edit_success'));
return redirect()->route('admin.slideshow.index');
}
幻灯片放映
模型
class SlideShow extends Model
{
protected $fillable = ['title_en', 'title_ar', 'link', 'image'];
public function uploadImage($imageName = 'image')
{
if(request()->has($imageName)){
\Storage::delete($this->image);
$uploadedImage = request()->$imageName->store('slideshow/');
\Image::make('storage/'.$uploadedImage)->resize(870, null, function ($constraint) {
$constraint->aspectRatio();
})->save();
$this->update(['image' => $uploadedImage]);
}
}
public static function search($request)
{
return static::where(lang('title'), 'like', '%' . $request->search . '%')->paginate(10);
}
}
这是正常的行为。发出请求时,Laravel将请求中的文件转换为UploadedFile类。由于您正在访问来自请求的已验证数据,因此将返回以下内容。您直接将这些值填充为属性,而UploadedFile不会自动转换为路径,因此第一次更新中可能不包括这一点 无论如何,在您的问题中实际包含的代码中,在调用
uploadImage
方法之前有dd()
方法,该方法保存路径,并且不会尝试保护整个UploadedFile类。因此,如果您在它可能具有正确的值之后转储它,您能验证这一点吗
我会这样做:
public function update(Request $request, SlideShow $slideshow)
{
// validate first
$data = $request->validate([
'title_en' => 'required',
'title_ar' => 'required',
'link' => 'nullable|url',
'image' => 'image'
]);
// only pull the data that we can update right now
$slideshow->update($request->only(['title_en', 'title_ar','link']));
// handle and store image if needed
$slideshow->uploadImage();
session()->flash('success', __('dashboard.slideshow.edit_success'));
return redirect()->route('admin.slideshow.index');
}
然后是模型上的方法:
public function uploadImage($imageName = 'image')
{
if(request()->has($imageName)){
\Storage::delete($this->image);
$uploadedImage = request()->$imageName->store('slideshow/');
\Image::make('storage/'.$uploadedImage)->resize(870, null, function ($constraint) {
$constraint->aspectRatio();
})->save();
$this->update(['image' => $uploadedImage->path()]);
}
}
这是正常的行为。发出请求时,Laravel将请求中的文件转换为UploadedFile类。由于您正在访问来自请求的已验证数据,因此将返回以下内容。您直接将这些值填充为属性,而UploadedFile不会自动转换为路径,因此第一次更新中可能不包括这一点 无论如何,在您的问题中实际包含的代码中,在调用
uploadImage
方法之前有dd()
方法,该方法保存路径,并且不会尝试保护整个UploadedFile类。因此,如果您在它可能具有正确的值之后转储它,您能验证这一点吗
我会这样做:
public function update(Request $request, SlideShow $slideshow)
{
// validate first
$data = $request->validate([
'title_en' => 'required',
'title_ar' => 'required',
'link' => 'nullable|url',
'image' => 'image'
]);
// only pull the data that we can update right now
$slideshow->update($request->only(['title_en', 'title_ar','link']));
// handle and store image if needed
$slideshow->uploadImage();
session()->flash('success', __('dashboard.slideshow.edit_success'));
return redirect()->route('admin.slideshow.index');
}
然后是模型上的方法:
public function uploadImage($imageName = 'image')
{
if(request()->has($imageName)){
\Storage::delete($this->image);
$uploadedImage = request()->$imageName->store('slideshow/');
\Image::make('storage/'.$uploadedImage)->resize(870, null, function ($constraint) {
$constraint->aspectRatio();
})->save();
$this->update(['image' => $uploadedImage->path()]);
}
}
您的
幻灯片放映模式是什么样子的?是否有影响您的图像
属性的访问者/变异者?@whitwhoa我更新我的问题并添加模型您的幻灯片
模型是什么样子的?是否存在影响您的image
属性的访问器/变异器?@whitwhoa我更新我的问题并添加模型当我在上传图像后转储它时它也转储对象但它修复了当我使用path
方法时它返回给我我将保存在数据库中的路径是的,模型中的方法丢失了。但是我会从第一个方法中删除图像
,如果由于某种原因在调用uploadImage方法时出现了问题,那么最终数据库中会出现脏数据。顺便问一下,它真的节省了吗?当您尝试在其中存储包含完整上载文件的模型时,更新是否返回true?是的,它返回temp
路径如果解决方案适合您,您是否可以将问题标记为已回答?谢谢当我上传图像后转储它时,它也会转储对象。但是当我使用path
方法时,它会将我将保存在数据库中的路径返回给我。是的,模型中的方法。但是我会从第一个方法中删除图像
,如果由于某种原因在调用uploadImage方法时出现了问题,那么最终数据库中会出现脏数据。顺便问一下,它真的节省了吗?当您尝试在其中存储包含完整上载文件的模型时,更新是否返回true?是的,它返回temp
路径如果解决方案适合您,您是否可以将问题标记为已回答?谢谢