在我的项目中,我想将图像添加到数据库中。
一切都很好。
现在,当我添加新产品并为其选择图像时,图像不会保存到数据库中。
但更新功能可以添加图像。
我的添加功能,
$books=Request::all();
$book= new Book;
$book->title=$books['title'];
$book->author= $request->author;
$book->publisher= $request->publisher;
$book->year= $request->year;
$book->price= $request->price;
if($request->hasFile('image')){
$image=$request->file('image');
$filename=time() . '.' .$image->getClientOriginalExtension();
$location=public_path('images/' .$filename);
Image::make($image)->resize(200,100)->save($location);
$book->image=$filename;
}
$book->save();
更新方法是,
public function update($id, Requests\BookRequest $request){
$book= Book::findorfail ($id);
$book->title= $request->title;
$book->author= $request->author;
$book->publisher= $request->publisher;
$book->year= $request->year;
$book->price= $request->price;
if($request->hasFile('image')){
$image=$request->file('image');
$filename=time() . '.' .$image->getClientOriginalExtension();
$location=public_path('images/' .$filename);
Image::make($image)->resize(200,100)->save($location);
$book->image=$filename;
}
$book->update();
add.blade.php
@extends('pages.app')
@section('content')
<h1>Add Books</h1>
</hr>
{{--{!! Form::open(['url'=>'books/add','files'=>true])!!}--}}
{!! Form::model(['method'=>'POST','action'=>['BooksController@store'],'files'=>true]) !!}
<div class="form-group">
{!! Form::label ('title','Title') !!}
{!! Form::text('title',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label ('author','Author') !!}
{!! Form::text('author',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('publisher', 'Publisher') !!}
{!! Form::text('publisher',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('year', 'Publishing Year') !!}
{{ Form::selectRange('year', 1950, 2050,2016, ['class' => 'field']) }}
</div>
<div class="form-group">
{!! Form::label ('image','Icon') !!}
{!! Form::file ('image') !!}
</div>
<div class="form-group">
<div >
<img src="" align="middle" height="136" width="144" >
</div>
</div>
<div class="form-group">
{!! Form::label ('price','Price') !!}
{!! Form::text('price',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Add',['class'=>'btn btn-primary btn-lg' ]) !!}
</div>
@if($errors->any())
<ul class="alert alert-danger">
@foreach($errors->all()as $error)
<li>{{$error}}</li>
@endforeach
</ul>
@endif
@stop
我的编辑视图是:
@extends('pages.app')
@section('content')
<h1>Edit Book's Details</h1>
</hr>
{!! Form::model($book, ['method'=>'PATCH','action'=>['BooksController@update',$book->id],'files'=>true])!!}
<div class="form-group">
{!! Form::label ('title','Title') !!}
{!! Form::text('title',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label ('author','Author') !!}
{!! Form::text('author',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('publisher', 'Publisher') !!}
{!! Form::text('publisher',null,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('year', 'Publishing Year') !!}
{{ Form::selectRange('year', 1950, 2050,$book->year,['class' => 'field']) }}
</div>
<div class="form-group">
{!! Form::label ('image','Icon') !!}
{!! Form::file ('image') !!}
</div>
<div class="form-group">
{!! Form::label ('price','Price') !!}
{!! Form::text('price',$book->price,['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Update',['class'=>'btn btn-primary btn-lg' ]) !!}
</div>
{!! Form::close() !!}
@if($errors->any())
<ul class="alert alert-danger">
@foreach($errors->all()as $error)
<li>{{$error}}</li>
@endforeach
</ul>
@endif
@stop
这是edit.blade.php。