我从数据库和本地存储中删除图像的方法。
public function destroy($id) {
$image = DB::table('images')->where('id', '=', $id)->first();
//print_r($image);
//return 'end';
File::delete(public_path() . '/images/gallery/thumb-' . $image->path);
File::delete(public_path() . '/images/gallery/' . $image->path);
$image->delete();
return Redirect::back()
->with('form_success', 1)
->with('form_message', 'Image successfully deleted!');
}
如果我尝试返回值$image
I get:
stdClass Object ( [id] => 49 [site_id] => 1 [page_id] => [location] => gallery [alt] => [path] => 60e52a2755ffe8923d5ac1232f5d9154.jpg )
那么我的代码有什么问题吗?现在我的 Laravel 版本是 4.2.1,但我尝试将他降级到 4.1.17,但没有任何变化。
问题是一旦你打电话first()
,执行查询并返回结果。你必须打电话delete()
在你打电话之前first()
.
DB::table('images')->where('id', $id)->delete();
由于您正在使用查询生成器,因此您将返回stdClass
正如您所看到的那样,对象(没有删除方法)而不是使用 Eloquent 时通常获得的模型。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)