John Davidson

php - Image not showing on browser laravel

0 comments
Message:


I am working on my project and i have to upload an image in my DB everything is working fine the the image is being uploaded to my given path but it not showing on browser here is my code can somebody please tell me what is the mistake that i am making.


This is my controller file


  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');


}

This is my View file


<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>


My Answer:
>validate($request, [__ 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',__ ]);__ if ($request->hasFile('image')) {__ $image = $request->file('image');__ $imageName = time().'.'.$image->getClientOriginalExtension();__ $image->move(public_path('images'), $imageName);__ }__ $data = new Image();__ $data->image = $imageName;__ $data->save();__ return redirect()->back();__ }_This is my view file_ ``_Make sure that the `public` directory is accessible from the browser. You can check this by trying to access the image directly in the browser using the URL `http://yourdomain.com/images/imagename.jpg`. If you can't access the image, then there might be an issue with the permissions of the `public` directory or the image file itself._

Rate this post

3 of 5 based on 8637 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us