0

hope you all right. I am working on a project in laravel where i have to store user logo in MYSql database and retrieve when user demand. I successfully stored logo path in database now i want to show it on a view but unlucky i can't. Logos are stored in 'storage/app/public/'. I tried asset function and all the possible ways i find on google and stackoverflow but no luck.

Controller function to store

    public function store(Request $request)
{
    $path = $request->file('logo')->store('public');
    $company = new company;
    $company->name = $request->input('name');
    $company->email = $request->input('email');
    $company->website = $request->input('website');
    $company->logo = $path;
    $company->save();
    $request->session()->flash('status', 'New Record added successfully');
    return redirect('company');
    
}

View

  @foreach($data as $company)
<tr>
  <th scope="row">{{ $company->name }}</th>
  <td>{{ $company->email }}</td>
  <td>
  <img src="{{ asset($company->logo) }}" alt="" style="width:100px; height:100px;">
  </td>
  <td> {{ $company->website }}</td>
  <td>
  <a href="{{ URL::to('company/'. $company->id. '/edit') }}">Edit</a>
  <a href="{{ URL::to('company/'. $company->id. '/destroy') }}">Delete</a>
  </td>
</tr>
@endforeach

Where i am wrong??? Any help will be highly appreciated :)

3
  • Error is on your $company->logoits not save currectly Commented Jul 1, 2020 at 8:06
  • @STA ellaborate your comment plz Commented Jul 1, 2020 at 18:30
  • You saved the file name in logo field on companies table. do you check it correct or not? Commented Jul 1, 2020 at 18:33

1 Answer 1

1

Please try this out, let me know if it works or not.

Controller

public function store(Request $request)
{
    // Image upload
    if($request->hasFile('logo')){
        $image = $request->file('logo');
        $extension = $image->getClientOriginalExtension();
        $fileName = $image->getFilename().'_'.date('d-m-Y_h-i-s').'.'.$extension;
        Storage::disk('public')->put($fileName,  File::get($image));
    }else{
        $fileName = null;
    }
    $company = new company;
    $company->name = $request->input('name');
    $company->email = $request->input('email');
    $company->website = $request->input('website');
    $company->logo = $fileName;
    $company->save();
    $request->session()->flash('status', 'New Record added successfully');
    return redirect('company');   
}

Blade

@foreach($data as $company)
    <tr>
        <th scope="row">
            {{ $company->name }}
        </th>
        <td>
            {{ $company->email }}
        </td>
        <td>
            @if($company->logo !== null)
                <img src="{{ url('storage/'.$company->logo) }}" alt="" style="width:100px; height:100px;">
            @else
                <img src="{{ asset('path_to_url_default_logo_image') }}" alt="" style="width:100px; height:100px;">
            @endif
        </td>
        <td> 
            {{ $company->website }}
        </td>
        <td>
          <a href="{{ URL::to('company/'. $company->id. '/edit') }}">Edit</a>
          <a href="{{ URL::to('company/'. $company->id. '/destroy') }}">Delete</a>
        </td>
    </tr>
@endforeach
Sign up to request clarification or add additional context in comments.

5 Comments

i am facing " Class 'App\Http\Controllers\Storage' not found ".
@Nomanmarwat Use this at the top of the controller, use Illuminate\Support\Facades\Storage;
images are saved but not showing in blade view. I followed exactly your code
Did you ran php artisan storage:link ? to create symlink to your storage folder into public folder.
THANK YOU sir, all is working fine after running "php artisan storage:link" command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.