phplaraveleloquentlaravel-query-builderlaravel-12

retrieve data using where condition and sort in descending order


I am trying to retrieve category (id, category_name, status) table data using where condition and sort in descending order for id. I am writing query like::

public function category()
    {
        $category = Category::where('status', 'true');
        return view('admin.services.category', compact('category'));
    }

this is showing error Attempt to read property "category_name" on array. How to fetch data using where condition as status == true and order id in descending order?


Solution

  • $category = Category::where('status',1)->orderBy('id','desc')->get();
    

    what is the data type of column status ?

    in mysql true/false is not supported and these are just simple string . instead datatype tinyint(1) with value 0 and 1 is used for boolean column.