简体   繁体   中英

Image not retrieved from database

Recently I have been trying to retrieve a image from the database, but it will not work. I am trying to do this with Laravel but I am struggling so I decided to ask the Stack Overflow community to see if they had a solution for me.

There are similar questions like this one on Stack Overflow. But I could not find one where the answers were the one I was looking for. I have also looked on the Laracast website, it helped me a bit but still not enough. Finally, I have read Stack Overflow question: Display BLOB image Laravel 4 . But this did not help me as well.

My controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\gallerijs;

class GalerijController extends Controller
{
    public function Retrieve(){
    $g = gallerijs::find(1);
    dd($g);
    $image = chunk_split(base64_encode($g->afbeelding));
    return view('gallerij')->with('image', $image);
}
}

My view:

@extends ('Layout')
@section('title')
    Galerij
@endsection

@section('content')


<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

</head>
<body>

<div id="demo" class="carousel slide" data-ride="carousel">

  <!-- Indicators -->
  <ul class="carousel-indicators">
    <li data-target="#demo" data-slide-to="0" class="active"></li>
    <li data-target="#demo" data-slide-to="1"></li>
    <li data-target="#demo" data-slide-to="2"></li>
  </ul>

  <!-- The slideshow -->
  <div class="carousel-inner">
    <div class="carousel-item active">
      <img src="{{asset('Pictures/'.$image)}}" alt="Los Angeles" width="1100" height="500">
    </div>
    <div class="carousel-item">
      <img src="{{asset('Pictures/loremipsum2.jpg')}}" alt="Chicago" width="1100" height="500">
    </div>
    <div class="carousel-item">
      <img src="{{asset('Pictures/loremipsum3.jpg')}}" alt="New York" width="1100" height="500">
    </div>
  </div>

  <!-- Left and right controls -->
  <a class="carousel-control-prev" href="#demo" data-slide="prev">
    <span class="carousel-control-prev-icon"></span>
  </a>
  <a class="carousel-control-next" href="#demo" data-slide="next">
    <span class="carousel-control-next-icon"></span>
  </a>
</div>

</body>
</html>


@endsection

What my dd($g) returns:

gallerijs {#232 ▼
  #fillable: array:2 [▶]
  #connection: "mysql"
  #table: "gallerijs"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:4 [▶]
  #original: array:4 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

Ì hope to have given you enough information so that you can help me solve the problem.

Changing you blade template image tag to something similar to this, should solve your problem. Usually i would always save images in traditional folders. This does the trick for your domain.

    <img src="data:image/jpg;base64, {{ $image }}" />

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