I want to save image to Db and show it to my view page. When I keep the image field null that is I do not choose any image to upload then every data is saved to the Db properly. But if I choose any image then I get ' Attempt to assign property of non-object'. But a corresponding image is crated in public/images folder.
here is my controller
"
public function store(Requests\BookRequest $request){
$book=Request::all();
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::create($book);
return redirect('books');
}
"
model is
class Book extends Model
{
protected $fillable=[
'title',
'author',
'publisher',
'year',
'price',
'image',
];
create form is
{!! Form::label ('image','Icon') !!}
{!! Form::file ('image') !!}
table is
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('author');
$table->string('publisher');
$table->integer('year');
$table->string('image')->nullable();
// $table->timestamp('added_at');
$table->timestamps();
});
}