Tasmin's avatar

Attempt to assign property of non-object

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();
    });
}
0 likes
2 replies
Kemito's avatar
Kemito
Best Answer
Level 9

Because ::all() returns array not object.

   $i = collect([1,2,3,4,5]); 
    var_dump($i); // This is what would ::get() return - a collection ^
    $x = $i->all(); // all returns array
    var_dump($x);
    $x->item = 1; // You cannot use -> to an array because it is not an object.  throws Attempt to assign property of non-object
1 like
mstnorris's avatar

It looks like you're saving the filename to the database, but you may not be including the '/images' directory in your view.

Please or to participate in this conversation.