0

my code in HomeController.php is

//function to register new issue
public function registerNewIssue(Request $request){
    $issue = new bookIssue();
    $bookIssued = Library::find($request -> issuedBook);
    $issue -> name = $request -> issuedName;
    $issue -> bookName = $request -> issuedBook;
    $issue -> dateIssued = $request -> issuedDate;
    $issue -> dateReturn = $request -> returnDate;
    $bookIssued -> Availability = "issued"; 
    $issue -> save();
    $data = User::all();
    return view('page.RegisterIssues' , compact('data'));
}

The idea is to find the issuedBook from the bookIssued model and update the Availability column to "issued". When i submit the form, its shown an error

Attempt to assign property "Availability" on null

I've follow some answer from here but the error still persist

  • 1
    Do you get a value from the variable `$request->issuedBook`? Try logging it out first. – Harshana Oct 30 '22 at 16:55
  • @Harshana I did 'dd($request)' and its return the value picked by the user – Muhammad Zarith Oct 30 '22 at 17:03
  • 1
    because `Library::find($request -> issuedBook);` is null, so `$bookIssued ->Availability = "issued";` throw that error, you can use `findOrFail()` instead of `find()` method – STA Oct 30 '22 at 17:08
  • @sta does that mean the code can't find the name of `issuedBook` in the Library? – Muhammad Zarith Oct 30 '22 at 17:10
  • 1
    Since you’re using find(issued) first with the book thats not issued **$bookIssued** is returning null and then you’re changing availability of a null object which is throwing the error so better use **updateOrCreate()** or **firstOrCreate()** method to do this as it will create an record if book is not available (null) in database. Reference: https://laravel.com/docs/9.x/eloquent#upserts and https://laravel.com/docs/9.x/eloquent#retrieving-or-creating-models – Sumit kumar Oct 30 '22 at 17:26
  • Write the migrations of Library and bookIssue I’ll write the solution if you still need. Lmk – Sumit kumar Oct 30 '22 at 17:34
  • @MuhammadZarith something like that, the `$bookIssued` is null, so `$bookIssued ->Availability` is also null, thats why you're getting this error – STA Oct 30 '22 at 18:26
  • @Sumitkumar yeah `$bookIssued` is returning null. i dont understand the part "Since you’re using find(issued) first with the book thats not issued $bookIssued is returning null" – Muhammad Zarith Oct 31 '22 at 01:59

1 Answers1

-1

I've change the code statement from ::find to ::where and returning first record by using first(). From there, I assigned the updated value. The whole code is:

 public function registerNewIssue(Request $request) {
    $issue = new bookIssue();
    $bookIssued = library::where('name' , $request -> issuedBook) -> first();
    $issue -> name = $request -> issuedName;
    $issue -> bookName = $request -> issuedBook;
    $issue -> dateIssued = $request -> issuedDate;
    $issue -> dateReturn = $request -> returnDate;
    $bookIssued -> Availability = "Issued"; 

    $bookIssued -> save();
    $issue -> save();
    $data = User::all();
    return view('page.RegisterIssues' , compact('data' , 'bookIssued'));
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • What happens if the book is already there in Library? Set the availability again ?? – Sumit kumar Oct 31 '22 at 05:05
  • 1
    the migration file for `Library` already created for `Availability` and set the default value as 'Available`. So if new record created, the value for Availability is available. My bad didn't include the code on the question @Sumitkumar – Muhammad Zarith Oct 31 '22 at 06:12
  • 1
    You can simply use this as well: **Library::updateOrCreate([“name”=>$request->issuedBook], [“Availability”=> “issued”]);** and you can use boolean instead of **issued** saves space in db if there is only issued value in availability. – Sumit kumar Oct 31 '22 at 17:04