1
votes

I'm trying to get the ID of eloquent for editing its columns, but when I make the changes in the column in edit page and click done it returns this error. Call to a member function fill() on string

When I use the code dd($id); it returned the word 'update' on dd page.

Route

Route::group(['prefix' => 'admin/books'], function($book){

    Route::get('edit/{id}','BookController@getUrlBook')->middleware('auth');

    Route::post('edit/{book}','EditController@update')->middleware('auth');

    Route::get('edit/{id}','BookController@getUrlBook', function ($book) {
        return view('edit',[
            'id' => $book->id,
            'name' => $book->name,
            'writer' => $book->writer_name,
            'isbn' => $book->isbn,
        ]);
    })->middleware('auth');

    Route::post('create','EditController@create');

    Route::get('create', function () {
        return view('create');
    })->middleware('auth');

});

Controller

public function update(Request $request,$id){
        $this->middleware('auth');
        $imgExtensions = ['jpg','png','svg'];
        $id->fill($request->only(['name','writer_name','isbn']));
        if(null !== $request->book_image && in_array($request->file('book_image')->getClientOriginalExtension(), $imgExtensions )){
            $id->image = $request->file('book_image')->storeAs('images', $request->id.'.jpg');
        }
        $id->save();
        return redirect('/admin');
    }
1

1 Answers

1
votes

if i understand you correctly:

$id you got from method parameters is the book's id you want to update ... so, you should use (fill) method for book model not its id ...

public function update(Request $request,$id){
        $this->middleware('auth');
        $imgExtensions = ['jpg','png','svg'];
$book=Book::findOrFail($id);
        $book->fill($request->only(['name','writer_name','isbn']));
        if(null !== $request->book_image && in_array($request->file('book_image')->getClientOriginalExtension(), $imgExtensions )){
            $id->image = $request->file('book_image')->storeAs('images', $request->id.'.jpg');
        }
        $id->save();
        return redirect('/admin');
    }

an other solution is using laravel binding:

Route::post('edit/{book}','EditController@update')->middleware('auth');

  public function update(Request $request,Book $book)  ....

now laravel will get $book from db for you, and you use the route the same way you were using it before