我在 Windows 10 上通過WSL2和Sail將Laravel 8與Docker一起使用。我創建了一個 CRUD 來在我的資料庫中存盤一些帶有影像的文章。我正在嘗試顯示來自 laravel 存盤的影像,但它們沒有出現在瀏覽器中。我怎樣才能解決這個問題?
文章控制器.php
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.articles.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required',
'body' => 'required',
'author' => 'required',
'image' => 'nullable | image | max:1024'
]);
$image = Storage::put('images', $request->image);
$validatedData['image'] = $image;
Article::create($validatedData);
return redirect()->route('admin.articles.index');
}
我按照Laravel 8 檔案使用公共磁盤存盤我的影像。
檔案系統.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Default Filesystem Disk
|--------------------------------------------------------------------------
|
| Here you may specify the default filesystem disk that should be used
| by the framework. The "local" disk, as well as a variety of cloud
| based disks are available to your application. Just store away!
|
*/
'default' => env('FILESYSTEM_DRIVER', 'public'),
/*
|--------------------------------------------------------------------------
| Filesystem Disks
|--------------------------------------------------------------------------
|
| Here you may configure as many filesystem "disks" as you wish, and you
| may even configure multiple disks of the same driver. Defaults have
| been setup for each driver as an example of the required options.
|
| Supported Drivers: "local", "ftp", "sftp", "s3"
|
*/
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
'secret' => env('AWS_SECRET_ACCESS_KEY'),
'region' => env('AWS_DEFAULT_REGION'),
'bucket' => env('AWS_BUCKET'),
'url' => env('AWS_URL'),
'endpoint' => env('AWS_ENDPOINT'),
'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
],
],
/*
|--------------------------------------------------------------------------
| Symbolic Links
|--------------------------------------------------------------------------
|
| Here you may configure the symbolic links that will be created when the
| `storage:link` Artisan command is executed. The array keys should be
| the locations of the links and the values should be their targets.
|
*/
'links' => [
public_path('storage') => storage_path('app/public'),
]
];
我還用命令列創建了鏈接sail artisan storage:link
一切正常,因為我可以在正確的路徑中看到影像:storage/app/images/my_image如下:
在此 輸入圖片說明 在此輸入圖片說明
當我嘗試在 laravel 刀片中顯示我的影像時遇到問題,如下所示:
<div class="flex justify-center flex-wrap">
@foreach ($articles as $article)
{{-- Articles --}}
<div class="max-w-sm rounded overflow-hidden shadow-lg my-5 mx-2">
<img class="w-full" src="{{ asset( 'storage/' . $article->image) }}" alt="瀏覽器不顯示來自 Laravel 存盤的影像">
<div hljs-number">6 py-4">
<div hljs-number">2">
{{ $article->title }}
</div>
<p hljs-number">700 text-base overflow-hidden h-16">{{ $article->body }}</p>
<span hljs-number">200 rounded-full px-3 py-1 mt-3 text-sm font-semibold text-gray-700">{{ $article->author }}</span>
</div>
</div>
@endforeach
</div>
我認為問題出在路徑上,因為在瀏覽器上我可以看到:
<img src="http://localhost/storage/images/9D1GCN9XDEwRWq8zN2wiA6eBSazgg657f9uUs0Uj.png" alt="瀏覽器不顯示來自 Laravel 存盤的影像">并且沒有顯示影像。
我嘗試了一切,更改檔案系統,將路徑存盤更改為控制器等。但我找不到解決方案。
uj5u.com熱心網友回復:
我想你應該把 get()
$image = Storage::put('images', $request->image->get());
如果您沒有像這樣定義磁盤:
Storage::disk('public')->put('images', $request->image->get());
它將在默認磁盤(“本地”)中。并檢索也許你可以像這樣:
<img class="w-full" src="{{ Storage::url('storage/' . $article->image) }}" alt="瀏覽器不顯示來自 Laravel 存盤的影像">
更好的方法是:
$path = "image_" . time() . '.' . $request->image->extension();
Storage::disk('public')->put($path, $request->image->get());
Article::create([...,
'image'=>$path,
]);
取回
<img class="w-full" src="{{ Storage::url($article->image) }}" alt="瀏覽器不顯示來自 Laravel 存盤的影像">
uj5u.com熱心網友回復:
由于瀏覽器中的影像路徑看起來是正確的,我認為是使用sail artisan storage:link.
我遇到了同樣的問題并解決了它php artisan storage:link從容器內部運行的問題。
對我來說,解決方案是直接在集裝箱內建立鏈接,而不是通過帆。
# Get inside the container. Replace "your-project-container" with the actual name.
docker exec -it your-project-container /bin/sh
# Change to user sail
su sail
# Run artisan storage:link
php artisan storage:link
# Exit twice to get out the container
exit
exit
這樣做sail artisan storage:link會創建一個與容器外不存在的 /var/www/html 路徑的錯誤鏈接。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/312833.html
