-1

How can I use Null coalescing ?? operator of PHP inside of an if statement with Laravel Eloquents to check an object exists or not? Currently, it is showing this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'posts.categories_id' in 'where clause' (SQL: select * from posts where posts.categories_id = 2 and posts.categories_id is not null and posts.deleted_at is null)

I tried to delete category freely when that category is not associated with a post. if it's associated with a post, only then it should show the error message

public function destroy(Categories $category)
{
    if ($category->posts ?? 0) {
        if ($category->posts->count() > 0 ?? 0) {
            session()->flash('warning', 'Category have related post. Can\'t be deleted!');
            return redirect()->back();
        }
    }

    $category->delete();
    session()->flash('success', 'Category deleted successfully!');
    return redirect(route('categories.index'));

}

By the way, I have these relationships right now:

    App\Post.php Model 
    public function category()
    {
        return $this->belongsTo(Categories::class);
    }
    App\Categories.php Model 
    public function posts()
    {
        return $this->hasMany(Post::class);
    }

It's working if I remove $category->post from my if condition. But then it deletes all types of categories including those associated with posts. Note: isset($category->post) also given error.

Please help me if you can. Thanks.

  • Well the error does not seem to be associated with how you are using ??. Have you set up the foreign keys correctly? Also, you might be over thinking here. if($category->posts ?? 0) is basically saying true or false / false or false. You could just say $category->posts()->exists() to check if there is data in the relation ship. – user3532758 Nov 16 '19 at 12:21
  • Can you confirm if there is a categories_id column in your posts table? – user3532758 Nov 16 '19 at 12:27
  • Please show database table structure & full model. – ARIF MAHMUD RANA Nov 16 '19 at 12:33
  • Okay, I got it now. I named my model as Categories instead of Category and using category_id instead of categories_id in my schema. Thank you guys for your help. I solved my problem now. – Sunwarul Islam Nov 16 '19 at 16:34

1 Answers1

0

I think I found the solution here. I just renamed the column name category_id to categories_id by this SQL column renaming statement here:

alter table posts change category_id categories_id int(11);

and I can now access $category->posts inside of my if statement. I think my model name should be Category instead of Categories. that was the problem so I get a default lookup for categories_id instead of category_id. By the way, it's working fine here now.

   public function destroy(Categories $category)
   {
       if ($category->posts) {
           if ($category->posts->count() > 0) {
               session()->flash('warning', 'Category have related post. Can\'t be deleted!');
               return redirect()->back();
           }
       }
       $category->delete();
       session()->flash('warning', 'Category deleted successfully!');
       return redirect(route('categories.index'));
   }

This method is working fine now.

Thank you so much, for your help