图像未显示在浏览器laravel上

dwbf0jvd  于 2022-11-18  发布在  其他
关注(0)|答案(2)|浏览(132)

我在我的项目工作,我必须上传一个图像在我的数据库一切都很好,图像正在上传到我的给定路径,但它没有显示在浏览器上,这里是我的代码,有人能告诉我,我犯了什么错误。

这是我的控制器文件

public function store(Request $request){

        $this->validate($request,[
            'image' =>'required|image|mimes:jpeg,png,jpg|max:4000',
        ]);

        $news = new News();

        if($request->hasFile($request->input('image'))){
            $file = $request->file('image');
            $extension = $file->getClientOriginalExtension();
            $filename = time().'.'.$extension;
            $file->move('assets_info/uploads/news/',$filename);
            $news->image = $filename;
        }

        $news->save();

        return redirect()->route('news.index')->with('success','News Added Successfully');

    }

这是我的视图文件

<section class="news-section mt-5">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-9">
          <section class="">
            <div class="row">
              <div class="col-md-12">
                @foreach ( $allnewsObj as $news)
                <?php
                  $newsTitle = ($locale === 'en')? $news->title_english:$news->title_arabic;
                  $newsContent = ($locale === 'en')? $news->content_english:$news->content_arabic;
                ?>
                <a href="{{route('news_details',$news->id)}}">
                  <div class="right-news right-news2 bg-greyish my-3">
                    <img src="{{asset('assets_info/uploads/news/',$news->image)}}" class="img-fluid">
                    <div class="content-news">
                      <h4>{{$newsTitle}}</h4>
                      <p><?php echo $newsContent; ?></p>
                    </div>
                  </div>
                </a>
    
                @endforeach
              </div>
            </div>
            <h4 class="bg-greyish p-2  mb-5 text-end h4-news">
            <a href="#" class="">More</a>
          </h4>
          </section>
      </div>

      <div class="col-md-3">
        <div class="">
          <h4>Trending News</h4>
          @foreach ( $trendingNewsObj as $item )
          <ul class="ul-news">
            <li><a href="{{route('news_details',$item->id)}}">{{$item->title_english}}</a></li>
          </ul>        
          @endforeach
        </div>
      </div>
    </div>
    
  </div>
</section>
xzv2uavs

xzv2uavs1#

你确定你的文件正在上传吗?根据你的控制器代码

$request->hasFile()

提供了错误的参数。参数应该是文件字段的名称,而不是正在上传的文件。请将其更改为

$request->hasFile('image')

请参阅此处:https://laraveldaily.com/requests-has-and-hasfile/

31moq8wy

31moq8wy2#

首先更改控制器

$file->move('assets_info/uploads/news/',$filename);

$file->move(public_path('uploads/news/'), $filename);

这里是你的控制器的完整源代码,你应该像这样更新你的控制器,

public function store(Request $request){

    $this->validate($request,[
        'image' =>'required|image|mimes:jpeg,png,jpg|max:4000',
    ]);

    $news = new News();

    if ($request->file('image')) {
        $file = $request->file('image');
        $filename = date('YmdHi').$file->getClientOriginalName();

        $file->move(public_path('uploads/news/'), $filename);

        $news->image = $filename;
    }

    $news->save();

    return redirect()->route('news.index')->with('success','News Added Successfully');
}

然后更新视图文件

<img src="{{asset('assets_info/uploads/news/',$news->image)}}" class="img-fluid">

<img src="{{url('uploads/news/'.$news->image)}}" class="img-fluid">

下面是更新视图文件的完整源代码,

<section class="news-section mt-5">
  <div class="container-fluid">
    <div class="row">
      <div class="col-md-9">
          <section class="">
            <div class="row">
              <div class="col-md-12">
                @foreach ( $allnewsObj as $news)
                <?php
                  $newsTitle = ($locale === 'en')? $news->title_english:$news->title_arabic;
                  $newsContent = ($locale === 'en')? $news->content_english:$news->content_arabic;
                ?>
                <a href="{{route('news_details',$news->id)}}">
                  <div class="right-news right-news2 bg-greyish my-3">

                    <img src="{{utl('uploads/news/'.$news->image)}}" class="img-fluid">

                    <div class="content-news">
                      <h4>{{$newsTitle}}</h4>
                      <p><?php echo $newsContent; ?></p>
                    </div>
                  </div>
                </a>
    
                @endforeach
              </div>
            </div>
            <h4 class="bg-greyish p-2  mb-5 text-end h4-news">
            <a href="#" class="">More</a>
          </h4>
          </section>
      </div>

      <div class="col-md-3">
        <div class="">
          <h4>Trending News</h4>
          @foreach ( $trendingNewsObj as $item )
          <ul class="ul-news">
            <li><a href="{{route('news_details',$item->id)}}">{{$item->title_english}}</a></li>
          </ul>        
          @endforeach
        </div>
      </div>
    </div>
    
  </div>
</section>

相关问题