Home > Enterprise >  Attempt to read property "userID" on bool
Attempt to read property "userID" on bool

Time:07-02

I have a problem with Laravel 9 where the userID is showing bool error whenever I click on the delete, I ma keeping a list from where by using delete I can delete from the list but Attempt to read property "userID" on bool

This is my model


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Doctor_review extends Model
{
    use HasFactory;
    public $timestamps = false;
    protected $primaryKey = 'doctorReviewID';
}

This is my controller portion

    $reviews = Doctor_review::all();
    return view('admin.docreviews')->with('doctor_reviews', $reviews);
}
public function deletereview(Request $request){
    $review = Doctor_review::where('doctorReviewID', $request->doctorReviewID)->first();
    return view('admin.docreviews')->with('doctor_reviews', $review);
}
public function deletereviewSubmit(Request $request){
    $review = Doctor_review::where('doctorReviewID', $request->doctorReviewID)->first();
    $review->delete();
    return redirect()->route('docreviews');
}
}

This is my blade page

@extends('layouts.appAdmin')
@section('contentAdmin')
    <table class = "table table-border">
        
        <tr>
            <th>Patient ID</th>
            <th>Doctor ID</th>
            <th>Description</th>
            <th>Rating</th>
        
        
        </tr>
        @foreach($doctor_reviews as $review)
        <tr>
            <td>{{$review->userID}}</td>
            <td>{{$review->doctorID}}</td>
            <td>{{$review->description}}</td>
            <td>{{$review->point}}</td>
            <td><a href="/deletereview/{{$review->doctorReviewID}}">Delete</a></td>
        
        </tr>
        @endforeach
@endsection

This is my delete form page

    <head>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
    </head>
    <body>
        <div  style="width:500px;">  
        <h2>Delete review</h2> 
            <form action="{{ route('deletereview') }}" method="POST">  
                
                {{csrf_field()}}
                <input type="hidden" name="doctorReviewID" value="{{review->doctorReviewID}}">
                <div >
                    <label for="userID">Patient ID</label>
                    <input type="text"  id="userID" name="userID" placeholder="Enter Patient ID">
                   
                </div>
                <div >
                    <label for="doctorID">Doctor ID</label>
                    <input type="text"  id="doctorID" name="doctorID" placeholder="Enter Doctor ID">
                    
                </div>
               
                <div >
                    <label for="description">Description</label>
                    <input type="text"  id="description" name="description" placeholder="description">
                    
                </div>
                <div >
                    <label for="point">Rating</label>
                    <input type="text"  id="point" name="point" placeholder="point">
                   
                </div>
              
                <div >
                    <span>
                        <input type="submit" name="Delete" value="Delete" >
                    </span>
                </div>
                </div>
            </form> 
        </div>
    </body>
</html>           

CodePudding user response:

your userID value is boolean, if you want to call with -> please make sure your userID is not boolean or null. You can do {{ dd($review->userID) }} in your blade to see the value of userID.

CodePudding user response:

Without the full error message I can't really know for sure. But this is my guess:

return view('admin.docreviews')->with('doctor_reviews', $reviews);

This adds a list of reviews to the form, and when you foreach, you will loop through the list.

return view('admin.docreviews')->with('doctor_reviews', $review);

This will add a single review to the form, and when you foreach, you will loop through its properties.

return view('admin.docreviews')->with('doctor_reviews', [$review]);

Might fix your issue. But I'd suggest splitting the blade page into a list view page and a detail view page.

CodePudding user response:

In two functions you are returning to one view with different type of variables. You are using foreach loop to read the both type of variables i.e. collection and single value.

    public function deletereview(Request $request){
        $review = Doctor_review::where('doctorReviewID', $request->doctorReviewID)->first();
         return view('admin.docreviews')->with('doctor_reviews', $review);
    }

first() will return element itself and can be in blade file as:

           {{$doctor_reviews->userID}}

And all() will return a Collection.

       $reviews = Doctor_review::all();
       return view('admin.docreviews')->with('doctor_reviews', $reviews);

To access data of collection $review in blade file loop is used i.e.:

           @foreach($doctor_reviews as $review)
        <tr>
            <td>{{$review->userID}}</td>
            <td>{{$review->doctorID}}</td>
            <td>{{$review->description}}</td>
            <td>{{$review->point}}</td>
            <td><a href="/deletereview/{{$review->doctorReviewID}}">Delete</a></td>
        
        </tr>
        @endforeach
  • Related