Laravel 8:如何将图像存储到DB(使用正确的文件路径)并将其显示在视图中



我很难将图像存储在数据库中,将图像的文件名(原始名称或路径?(存储到数据库中,用其原始名称将图像存储到公共存储文件夹中,然后显示图像?现在我得到了以下代码的错误:对null调用成员函数move((。

create_books_table迁移

<?php
use IlluminateDatabaseMigrationsMigration;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;
class CreateBooksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->string('author');
$table->string('title');
$table->string('book_image');
$table->string('amazon');
$table->string('goodreads');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('books');
}
}

web.php

<?php
use AppHttpControllersBookController;
use IlluminateSupportFacadesApp;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesRoute;

Auth::routes();
/*--------------- Website Views ---------------*/
Route::get('/', function () {
return view('index');
});
Route::get('/about', function () {
return view('pages.about');
});
Route::get('/books', function () {
return view('pages.books');
});

/*--------------- Admin Views ---------------*/
/*--- Home Controller ---*/
Route::get('/home', [AppHttpControllersHomeController::class, 'index'])->name('admin.home');
Route::get('/home/create', [AppHttpControllersHomeController::class, 'create'])->name('admin.create');
Route::post('/home', [AppHttpControllersHomeController::class, 'store'])->name('home.store');

HomeController.php

<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsBook;

class HomeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$books = Book::all();
return view('admin.home', compact('books'));
}
public function create()
{
return view('admin.create');
}

public function store(Request $request,)
{
// Validate User Input
$inputs = request()->validate([
'author' => ['required', 'string', 'max:255'],
'title' => ['required', 'string', 'max:255'],
'book_image' => ['required', 'file', 'mimes:jpeg,bmp,png'],
'amazon' => ['required', 'string', 'max:4068'],
'goodreads' => ['required', 'string', 'max:4068']
]);
// Change book_image name to original name and store in public storage folder.
if ($request->hasFile('book_image')) {
$imageName = time() . '.' . $request->book_image->extension();
$request->image->move(public_path('book-images'), $imageName);
}
// Create Book Object
$book = new Book([
"author" => $request->get('author'),
"title" => $request->get('title'),
"book_image" => $request->file($imageName),
"amazon" => $request->get('amazon'),
"goodreads" => $request->get('goodreads')
]);
// Save the Record to DB
$book->save();
return redirect()->route('admin.home');
}
}

home.blade.php

@extends('layouts.app')
@section('content')
<div class="container-fluid">
{{-- If Authorized: Show This Page --}}
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
<div>
<h2 style="margin-bottom: 1rem; text-align:center">Kara's Home Page</h2>
<a href="{{ route('admin.create') }}" class="btn btn-outline-secondary" style="margin-bottom: 1.5rem">Create
Book</a>
</div>
<div class="card shadow mb-4">
<div class="card-header py-3">
<h2 class="m-0 font-weight-bold text-secondary">Edited Books</h2>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered" id="dataTable" width="100%" cellspacing="0">
<thead>
<tr>
<th>Id</th>
<th>Author</th>
<th>Title</th>
<th>Image</th>
<th>Amazon Url</th>
<th>Goodreads Url</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Id</th>
<th>Author</th>
<th>Title</th>
<th>Image</th>
<th>Amazon Url</th>
<th>Goodreads Url</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</tfoot>
<tbody>
@foreach ($books as $book)
<tr>
<td>{{ $book->id }}</td>
<td>{{ $book->author }}</td>
<td>{{ $book->title }}</td>
<td><img src="{{ $book->book_image }}" alt="Book Image" height="40px"></td>
<td>{{ $book->amazon }}</td>
<td>{{ $book->goodreads }}</td>
{{-- Form Buttons Are More Secure --}}
<td>
<a href="{{ route('admin.edit', $book->id) }}"
class="btn btn-outline-secondary">Edit</a>
</td>
<td>
<form method="post" action="{{ route('home.destroy', $book->id) }}"
enctype="multipart/form-data">
@csrf
@method('DELETE')
<button type="submit" class="btn btn-outline-danger">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
<div class="pagination justify-content-center">
{{-- {{ $posts->links('pagination::bootstrap-4') }} --}}
</div>
</div>
@endsection

信息

我已经尝试了一百万种不同的方法来更改文件路径名。我只想用正确的方法来做这件事,这样图像就会显示出来。

更新

$request->book_image->getRealPath()->store('book', 'public');

添加这个

getRealPath()

并锁定这个

$name = $request->book_image->getClientOriginalName()
$filename = $request->book_image->getRealPath() 
$extension = $request->book_image->extension() 

$name = $request->book_image->getClientOriginalName() ;
$filename = pathinfo($request->book_image, PATHINFO_FILENAME);
$extension = pathinfo($request->book_image, PATHINFO_EXTENSION);

最新更新