php 我无法解决Laravel图像存储和显示的问题

cuxqih21  于 5个月前  发布在  PHP
关注(0)|答案(1)|浏览(59)

我已经使用Laravel几个月了,我认为我已经很好地掌握了它。最近,我正在为自己建立一个投资组合网站,我想让它从我的数据库中获取数据,并在一个“项目”页面中显示一大堆数据。在我换电脑之前,它一直工作得很好(从台式机到工作专用笔记本电脑)我转移了我的应用程序文件,以便我可以在笔记本电脑设置后立即继续使用它,它似乎工作正常,但我现在面临着一个我无法调试的问题。
虽然我的页面显示项目正确地从数据库中获取数据,但每个项目的图像并没有显示。相反,我得到了一个小图标,当图像被破坏时(白色和绿色)
当我说我不能调试它为我的生活,这里是我遵循的步骤:
初始图像存储来自将其注册到数据库中的函数

public function storeProject(Request $request)
    {
        //dd($request->all());
        // Valider les données du formulaire
        $validatedData = $request->validate([
            'name' => 'required|string|max:191',
            'image_1' => 'required|file|image|mimes:jpg,jpeg,png|max:4000',
            'description' => 'required|string',
            'url' => 'required|url',
            'customer' => 'required|string|max:191',
            'mission' => 'required|string|max:191',
            'tags' => 'nullable|array',
        ]);
        //dd($validatedData);

        // convert string to array
        $mission = $validatedData['mission'] ? [$validatedData['mission']] : [];

        //Enregistrer les image sur notre serveur

        $imagePaths = [];
        $images = [$request->file('image_1'), $request->file('image_2'), $request->file('image_3')];
        foreach ($images as $image) {
            if ($image) {
                $filename = time() . '-' . $image->getClientOriginalName();
                $path = $image->storeAs('public/images/' . $filename);
                $imagePaths[] = $filename;
            }
        }

        // Nouveau projet avec les données validées du formulaire
        $project = new projet;
        $project->name = $validatedData['name'];
        $project->image_1 = $imagePaths[0] ?? null;
        $project->description = $validatedData['description'];
        $project->url = $validatedData['url'];
        $project->customer = $validatedData['customer'];
        $project->mission = $validatedData['mission'];
        $project->user_id = Auth::user()->id;
        $project->save();

        // tags
        $tags = $validatedData['tags'];
        $project->addtags($tags);
        // categories

        $project->addCategory($mission);
        return redirect()->route('moderate')->with('success', 'Projet mis en Ligne avec succès.');

        // redirection avec errors
    }

字符串
这个函数可以工作,数据库正确地注册数据,并且在刀片视图获取数据时正确地显示数据(减去图像)。

@extends('layouts.app')

@section('title', 'Portfolio')

@section('content')

    <div class="containers">
        <div class="mobilemenubar">

        </div>
        <div class="column">

            <div class="BackgroundImage">
                <img src="{{ Storage::url('/images/Homeimage.jpg') }}" alt="Background-Image" />

                <div class="project-container">
                    {{--                 We use the Modulo operator here : passing projects to the view in an array, we check the array's index with the modulo operator to see if the index is dividable by 2, if so we give it a value of 0 if not a value of 1, then use an odd/even system to alternate layouts --}}
                    <div class="addbutton">
                        <a href="{{ route('add-project') }}">Ajouter un nouveau Projet</a>
                    </div>
                    @foreach ($projects as $index => $project)
                        @if ($index % 2 === 0)
                            <div class="project-first-layout">
                                <div class="first-layout-image-box"> <img
                                        src="{{ asset('storage/images/' . $project->image_1) }}" alt="{{ $project->name }}">
                                </div>
                                <div class="first-layout-text-area"> {{ $project->description }} </div>
                                <div class="externals">
                                    <a href="{{ $project->url }}" class="external-link">Visiter le Site</a>
                                    <a href="#" class="external-link">Git</a>
                                </div>
                                <div class="buttons">
                                    <form action="  {{ route('edit-project', $project->id) }}" method="GET">
                                        @csrf

                                        <button type="submit">Modifier</button>
                                    </form>
                                    <form action=" {{ route('delete-project', $project->id) }}"class="Delete"
                                        method="POST">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit">Supprimer</button>
                                    </form>
                                </div>
                            </div>
                        @else
                            <div class="project-second-layout">
                                <div class="second-layout-image-box"><img
                                        src="{{ asset('storage/images/' . $project->image_1) }}" alt="{{ $project->name }}">
                                </div>
                                <div class="second-layout-text-area">{{ $project->description }}</div>
                                <div class="externals">
                                    <a href="{{ $project->url }}" class="external-link">Visiter le Site</a>
                                    <a href="#" class="external-link">Git</a>
                                </div>
                                <div class="buttons">
                                    <form action=" {{ route('edit-project', $project->id) }}" method="GET">
                                        @csrf

                                        <button type="submit">Modifier</button>
                                    </form>
                                    <form action=" {{ route('delete-project', $project->id) }}"class="Delete"
                                        method="POST">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit">Supprimer</button>
                                    </form>
                                </div>
                            </div>
                        @endif
                    @endforeach
                </div>
                <div class="Navbuttons">
                    <div class="AdminWelcomeBox">
                        <h1>Mes Projets</h1>
                    </div>
                    @include('components.admin-navmenu')
                    <div id="adminsocials">
                        <a href="#"><i class="fa-brands fa-facebook fa-4x" id="Facebook"></i></a>
                        <a href="#"><i class="fa-brands fa-twitter fa-4x" id="Twitter"></i></a>
                        <a href="#"><i class="fa-brands fa-linkedin fa-4x" id="Linkedin"></i></a>
                    </div>
                </div>



            </div>
        </div>



    @endsection


在这种情况下,错误来自first-layout-image-box和second-layout-image-box,它们没有正确返回图像(而HomeImage显示正确)

  • 我试过了using Storage::url helper,using asset()helper,using just url()helper,and none of these worked. -我确保我的php artisan storage:link设置好了,它是-我已经清除了缓存
    这是我的文件系统设置:
'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
            'throw' => false,
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
            'throw' => false,
        ],

        '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),
            'throw' => false,
        ],

    ],


值得注意的是,我(目前)通过WAMP在本地托管它,在我切换PC之前,它似乎没有任何问题

n3h0vuf2

n3h0vuf21#

在公共空间中删除存储文件夹然后使用
第一个月
再次

相关问题