图片上传和获取出错,Method App\Image::__toString() must not throw an exception, caught Illuminate\Database\Eloquent\JsonEncodingException
Error Image Upload and Get, Method App\Image::__toString() must not throw an exception, caught Illuminate\Database\Eloquent\JsonEncodingException
我正在上传正在上传并移动到 storage/app/upload/images 文件夹的用户个人资料图片,但是当我尝试显示该图片时,出现以下错误。
Method App\Image::__toString() must not throw an exception, caught Illuminate\Database\Eloquent\JsonEncodingException
这是我用于显示的控制器函数
public function userProfile() {
$image = Image::all();
return view('frontend.layouts.Profile',compact('image'));
}
我正在显示图像的视图
@foreach($image as $images)
<img style="width:210px ; height: 230px " src="/storage/app/upload/images/{{$images->image}}" >
@endforeach
请将您的图片上传到 Public 目录,然后尝试访问它,它将正常工作
可通过三种方式向用户提供图像:
1.作为 public 资产
在这里,图像可供所有人使用。例如,所有人都会访问您的网站徽标或着陆页图像。因此,图像有一个 url,所有人都可以轻松访问。这些文件将直接进入 public/img/
文件夹。
2。作为受保护的图片仅在请求特定 url 时可用
此处用户特定图像将由特定人员访问。想一想您希望仅供成员本人或某些特定人员使用的成员的个人照片。在这种情况下,您可以将图像存储在 storage/app/public
中,并使用 artisan 命令 php artisan storage:link
创建一个符号链接。您可以阅读更多关于此 here. Assuming that you store your files using random names using str_random() 的内容,然后您将生成 urls 到您的使用 asset() helper 的图像,例如: echo asset('storage/X3jf5j5b2j3n.jpg');
鉴于文件名是随机的,除了那些使用 asset() helper 生成 url 的人之外,每个人都很难访问此图像。
3。作为使用干预库提供的受保护图像
在这种情况下,您将首先检查用户是否已登录,然后使用 Intervention 通过另一个受保护的路由动态加载图像。因此,在您的网络路由中,您首先要使用 auth 中间件获得用户授权:
Route::group(['middleware' => 'auth'], function () {
Route::get('user', 'UserController@userProfile');
Route::get('images/{image}', 'UserController@serveImage'); // this route serves the image only if user is logged in
});
然后一旦您使用 composer 安装了干预库,我们的 UserController 将如下所示:
use Intervention;
class UserController extends Controller
{
public function userProfile()
{
$images = Image::all();
return view('frontend.layouts.Profile', compact('images'));
}
public function serveImage($image)
{
$filename = storage_path('app/images/'.$image);
return Intervention::make($filename)->response();
}
}
您可以看到图像现在是从 storage
文件夹而不是 public
文件夹提供的。所以这个方法 serveImage() 只有在前面定义的路由被授权时才会被调用。然后,干预会发挥其魔力来读取图像并将其作为 http 响应发送。
您的视图会发生一点点变化,以适应我们定义的名为 images
的新路线终点。我在这里假设您通过名为 filename
:
的字段将图像的文件名存储在数据库中
@foreach($images as $image)
<img style="width:210px ; height: 230px " src="{{ url('/images/'.$image->filename) }}" >
@endforeach
注意: 请记住,提供图像的首选方法是使用方法 2,因为它要快得多。如果您真的不想让任何人偶然发现使用 url 的文件,您可以谨慎使用方法 3。
我正在上传正在上传并移动到 storage/app/upload/images 文件夹的用户个人资料图片,但是当我尝试显示该图片时,出现以下错误。
Method App\Image::__toString() must not throw an exception, caught Illuminate\Database\Eloquent\JsonEncodingException
这是我用于显示的控制器函数
public function userProfile() {
$image = Image::all();
return view('frontend.layouts.Profile',compact('image'));
}
我正在显示图像的视图
@foreach($image as $images)
<img style="width:210px ; height: 230px " src="/storage/app/upload/images/{{$images->image}}" >
@endforeach
请将您的图片上传到 Public 目录,然后尝试访问它,它将正常工作
可通过三种方式向用户提供图像:
1.作为 public 资产
在这里,图像可供所有人使用。例如,所有人都会访问您的网站徽标或着陆页图像。因此,图像有一个 url,所有人都可以轻松访问。这些文件将直接进入 public/img/
文件夹。
2。作为受保护的图片仅在请求特定 url 时可用
此处用户特定图像将由特定人员访问。想一想您希望仅供成员本人或某些特定人员使用的成员的个人照片。在这种情况下,您可以将图像存储在 storage/app/public
中,并使用 artisan 命令 php artisan storage:link
创建一个符号链接。您可以阅读更多关于此 here. Assuming that you store your files using random names using str_random() 的内容,然后您将生成 urls 到您的使用 asset() helper 的图像,例如: echo asset('storage/X3jf5j5b2j3n.jpg');
鉴于文件名是随机的,除了那些使用 asset() helper 生成 url 的人之外,每个人都很难访问此图像。
3。作为使用干预库提供的受保护图像 在这种情况下,您将首先检查用户是否已登录,然后使用 Intervention 通过另一个受保护的路由动态加载图像。因此,在您的网络路由中,您首先要使用 auth 中间件获得用户授权:
Route::group(['middleware' => 'auth'], function () {
Route::get('user', 'UserController@userProfile');
Route::get('images/{image}', 'UserController@serveImage'); // this route serves the image only if user is logged in
});
然后一旦您使用 composer 安装了干预库,我们的 UserController 将如下所示:
use Intervention;
class UserController extends Controller
{
public function userProfile()
{
$images = Image::all();
return view('frontend.layouts.Profile', compact('images'));
}
public function serveImage($image)
{
$filename = storage_path('app/images/'.$image);
return Intervention::make($filename)->response();
}
}
您可以看到图像现在是从 storage
文件夹而不是 public
文件夹提供的。所以这个方法 serveImage() 只有在前面定义的路由被授权时才会被调用。然后,干预会发挥其魔力来读取图像并将其作为 http 响应发送。
您的视图会发生一点点变化,以适应我们定义的名为 images
的新路线终点。我在这里假设您通过名为 filename
:
@foreach($images as $image)
<img style="width:210px ; height: 230px " src="{{ url('/images/'.$image->filename) }}" >
@endforeach
注意: 请记住,提供图像的首选方法是使用方法 2,因为它要快得多。如果您真的不想让任何人偶然发现使用 url 的文件,您可以谨慎使用方法 3。