store image in folder and get them to the frontend

In this Article we will see how we are able to add images or other files to our projects folder and show them to the frontend.

  1. The Controller,

[code]public function store(Request $request)
{
$store = new Post;

//image
if($request->hasFile(‘file’)){
$filename = $request->file->getClientOriginalName();
$filename = $request->file(‘file’)->storeAs(‘/images’, $filename, ‘public’);
$store->image = $filename;
}…[/code]

2. The model

[code]

class Post extends Model
{

public function getImageAttribute($value)
{
return asset(“storage/$value”);
}

}

[/code]

3. The view

[code]<img src=”{{ $posts->image }}” alt=”post image” width=”50px” id=”imediateImage”>[/code]

CategoriesUncategorized

Leave a Reply

Your email address will not be published.