在laravel数据库中上传后,在浏览器中显示图像文件

时间:2018-09-26 16:14:57

标签: laravel image laravel-5 laravel-blade image-file

我有一个创建表单,其中包含一些字段,例如:名称,价格,类别...,以及图像文件的输入字段。

这是我的create.blade.php:

@extends('layouts.app')
@section('content')
    <div class="container">

        <div class="card mt-5">
            <div class="card-body">
                <div class="card-title">
                    create product
                </div>
            </div>
        </div>
        <form method="post" action="{{route('books.store')}}" enctype="multipart/form-data">
            {{csrf_field()}}
            <div class="form-group">
                <label for="name">Name</label>
                <input type="text" class="form-control" name="name" id="name" aria-describedby="emailHelp" value="{{old('name')}}" placeholder="Enter name">

            </div>
            <div class="form-group">
                <label for="pages">pages</label>
                <input type="text" class="form-control" id="pages" name="pages" value="{{old('pages')}}" placeholder="pages">
            </div>



            <div class="form-group">
                <label for="ISBN">ISBN</label>
                <input type="text" class="form-control" id="ISBN" name="ISBN" value="{{old('ISBN')}}" placeholder="ISBN">
            </div>


            <div class="form-group">
                <label for="price">price</label>
                <input type="text" class="form-control" id="price" name="price" value="{{old('price')}}" placeholder="price">
            </div>

            <div class="form-group">
                <label for="published_at">published_at</label>
                <input type="date" class="form-control" id="published_at" name="published_at" value="{{old('published_at')}}" placeholder="published_at">
            </div>

            <div class="form-group">
                <label for="published_at">انتخاب تصویر</label>
                <input type="file" name="file" id="file">
            </div>

            <div class="form-group">
                <label for="category_id">CATEGORY ID</label>
                <select name="category_id[]" class="form-control" multiple>
                    @foreach($categories as $category)
                        <option value="{{$category->id}}">
                            {{$category->name}}
                        </option>
                    @endforeach
                </select>
            </div>
            <div class="form-group">
                <label for="author_id"> Author</label>
                <select name="author_id[]" class="form-control" multiple>
                    @foreach($authors as $author)
                        <option value="{{$author->id}}">
                            {{$author->name}}
                        </option>
                    @endforeach
                </select>
            </div>

            @if ($errors->any())
                <div class="alert alert-danger">
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif
            <button type="submit" class="btn btn-success">Submit</button>
        </form>
    </div>

@endsection

这是我的控制器:

<?php

namespace App\Http\Controllers;

use App\author;
use App\book;
use App\category;
use App\File;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Storage;

class BookController extends Controller
{
    public $book;
    public $file;
    /**
    public function __construct()
    {
        $this->middleware('auth')->only('create');
    }
*/
    public function index()
    {
        $books = Book::with(['user'])->get();
        return view('books.index', compact('books'));

    }


    public function show($id)
    {
        $book = Book::with('categories','authors')->find($id);
        return view('books.show', compact('book'));
    }


    public function create()
    {
        $categories = Category::all();
        $authors = Author::all();
        $files= File::all();
        return view('books.create', compact('categories','authors','files'));
    }
    public function store(Request $request)
    {
        $this->validate($request,
            [
                'name'=>'required|string|max:256',
                'pages'=>'required|integer|numeric',
                'ISBN'=>'required|string|numeric',
                'price'=>'required|integer|numeric',
                'published_at'=>'required|date|date',
                'file'=>'required',

            ]
        );


        if ($request->hasFile('file')) {
            $filename = $request->file('file')->getClientOriginalName();
            $request->file->storeAs('public', $filename);
            $file = new File;
            $file->name = $filename;

            $file->save();

        }

        $book =  Auth::User()->books()->create($request->except('_token'));
        $book->categories()->attach($request->get('category_id'));
        $book->authors()->attach($request->get('author_id'));
        $book->files()->attach($request->get('book_id'));
        return redirect('/books');
    }

}


its web.php:

<!-- begin snippet: js hide: false console: true babel: false -->

它是index.blade.php,我们可以在浏览器中看到字段:

@extends('layouts.app')
@section('content')


    <div class="container">
        <table class="table">
            <tr>
                <th>
                    Book Name
                </th>

                <th>
                    Creator
                </th>
            </tr>

            @foreach($books as $book)
                <tr>
                    <td><a href={{"/books/".$book->id}}>{{$book->name}}</a></td>
                    <td>{{$book->user->name}}</td>
                    @can('update',$book)
                        <td><a href="{{Route('books.edit',['id'=>$book->id])}}" class="btn btn-primary">edit</a></td>

                        <!-- Button trigger modal -->
                 <td>      <button type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModal">
                           delete
                        </button>
                 </td>

                  
                    @else
                        <td>

                        </td>
                    @endcan

                    <td>

                    </td>

                </tr>

            @endforeach
        </table>

        <div class="mt-3"></div>
        <a href="{{'/books/create'}}" class="btn btn-success btn-block">Create</a>

    </div>


@endsection

我所有的代码都正确运行,但是我的问题是我该如何显示图像文件到浏览器?我的意思是;如何在index.blade.php中使用img标签将图像显示在浏览器中? (我没有编写所有代码,因为它太长了,我只想在img标签中写一个短代码,就像这样:

'<img  src="public/' . $file->getClientOriginalName() .'"  />';

但是您知道它不适用于刀片服务器。我该怎么办?

1 个答案:

答案 0 :(得分:1)

您可以使用Laravel的 assets() url()功能获取图像路径。

asset()函数使用请求的当前方案(HTTP或HTTPS)生成资产的URL:

<img  src="{{ asset('img/photo.jpg') }}">

Example with asset()

url()函数生成指向给定路径的标准URL:

<img  src="{{ url('img/photo.jpg') }}">

Example with url()

如果您的文件直接位于公用文件夹中,而不是您的代码,

<img  src="{{ asset( $file->getClientOriginalName() ) }}"  >

Your problem solution with asset()