简体   繁体   中英

Access image into storage and display into blade view

I am new in laravel intervention image below is code. I want to access image from storage to blade view how can I achieve to do that?

    //In my Controller

      //Save to the database
        $post = new Post;
        $post->title = $request->input('title');
        $post->body = $request->input('body');
        $post->subcategory_id = implode(',',$request->input('subcategory_id') );
        $post->user_id = auth()->user()->id;
        $post->content = $fileNameToStore;
        $post->save();

     //Save to the storage
       $post = Image::make(
       $request->file('content')->getRealPath())->save('public/content',$fileNameToStore);
       $post->resize(300, 300);
       $post->text('The quick brown fox jumps over the lazy dog.', 10, 10);
       $post->save();


    //Blade view
    <img src="'public/content'.$post->content" style="width: 100%">

Try.

Write this code in your blade.php file

@php $image = url('public/content'.$post->content); @endphp
<img src="{{ $image }}" class="image_preview" width="auto" height="150px">

or

<img src="{{URL::to('public/content').'/'.$post->content}}" alt="">

Change your .blade.php file like this:

@php
    echo'<img src="content/'.$post->content.'" style="width: 100%">';
@endphp
  • No need to write public directory name
  • You saved your image in content folder

Use Asset function as below.

<img src="{{ asset('/content'.$post->content)" style="width: 100%">

I am not aware about Main Aim of your Code but the general best practice to store image in Laravel to Store it in Storage Disk(Storage Folder of your Laravel Project), This Is a sample of Selecting and Uploading Multiple Files that I have Used in my Project, Might help You,

$files = [];
      if($request->hasfile('files_to_upload')){
         foreach($request->file('files_to_upload') as $file){
             $name = time().rand(1,100).'.'.$file->extension();
             $files[]= storage_path().'/app/'.$file->storeAs('files', $name);  
         }
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM