0

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

这是我的 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() .'"  />';

但你知道它在刀片中不起作用。我该怎么办?

4

1 回答 1

1

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

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

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

资产()的例子

url()函数为给定路径生成一个完全限定的 URL :

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

带有 url() 的示例

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

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

您使用asset()的问题解决方案

于 2018-09-26T17:03:47.033 回答