0

I have a resource Controller with this index method like this:

public function index()
{
        $args = [];
        $args = array_merge($args, $this->data_creator(35, 12, 'book'));
        $args = array_merge($args, $this->data_creator(37, 12, 'kit'));
        $args = array_merge($args, $this->data_creator(38, 12, 'game'));

        $args['menu_links'] = [
            'books'     => route('shopping-products.category', Category::find(25)->slug),
            'videos'    => route('shopping-products.category', Category::find(24)->slug),
            'kits'      => route('shopping-products.category', Category::find(23)->slug),
            'games'     => route('shopping-products.category', Category::find(22)->slug),
        ];
    
        return view('frontend.shop.products.index', $args);
}

But it returns this error:

Trying to get property 'slug' of non-object

And when I dd(Category::find(25), Category::find(24), Category::find(23), Category::find(22)); I get NULL results.

Meaning that it can not find data with specified ids.

However there are 25 records stored at the categories table:

enter image description here

So what is going wrong here? How can I fix this issue?

I would really appreciate any idea or suggestion from you guys...

Thanks in advance.

Here is Category.php Model:

class Category extends Model
{
    use Sluggable, SoftDeletes;

    protected $table = 'categories';
    protected $primaryKey = 'cat_id';
    protected $guarded = [];

    /**
     * Return the sluggable configuration array for this model.
     *
     * @return array
     */
    public function sluggable()
    {
        return [
            'slug' => [
                'source' => 'cat_name'
            ]
        ];
    }

    public function path()
    {
        return "/products/categories/$this->slug";
    }

    public function children()
    {
        return $this->hasMany(Category::class, 'cat_parent_id', 'cat_id');
    }

    public function parents()
    {
        return $this->hasMany(Category::class, 'cat_id', 'cat_parent_id');
    }

    public function products()
    {
        return $this->belongsToMany(Product::class, 'category_products', 'ctp_cat_id', 'ctp_prd_id');
    }

    public function news()
    {
        return $this->belongsToMany(News::class, 'category_news', 'ctn_cat_id', 'ctn_nws_id');
    }

    public function galleries()
    {
        return $this->belongsToMany(Gallery::class, 'category_galleries', 'ctg_cat_id', 'ctg_gly_id');
    }

    public function uploaded()
    {
        return $this->hasMany(UploadedFile::class, 'upf_object_id', 'cat_id')->where('upf_object_type_id', '=', '107');
    }

    public function articles()
    {
        return $this->belongsToMany(Article::class, 'article_category', 'act_cat_id', 'act_art_id');
    }

    public function olympiadExam()
    {
        return $this->belongsToMany(OlympiadExam::class, 'olympiads_exams_categories', 'oec_ole_id', 'oec_cat_id');
    }

    public function olympiadExamQuestion()
    {
        return $this->belongsToMany(OlympiadExamQuestion::class, 'olympiads_exams_questions_categories', 'oes_cat_id', 'oes_oeq_id')->orderBy('oeq_number', 'asc');
    }

    public function attr_attributes()
    {
        return $this->hasMany(CategoryAttribute::class, 'category_id', 'cat_id');
    } //

    public function attr_product()
    {
        return $this->hasMany(Product::class, 'prd_cat_att_id', 'cat_id');
    } //

    public function couponRelation()
    {
        return $this->hasMany(couponRelation::class, 'object_id', 'cat_id')->where('object_type', 'product_category');
    }

    public function magazines()
    {
        return $this->belongsToMany(Magazine::class, 'category_magazine', 'category_id', 'magazine_id');
    }

}

And when I do: dd(Category::where('cat_id', 25), Category::where('cat_id', 24), Category::where('cat_id', 23), Category::where('cat_id', 22)); I get this as result:

enter image description here

4
  • Can you provide some more debug information, that includes the actual queries being run? Running Category::where('cat_id', 25)->dd() can help. Commented Jun 29, 2021 at 8:31
  • @erikgaal "select * from categories where cat_id = ? and categories.deleted_at is null" array:1 [▼ 0 => 25 ] Commented Jun 29, 2021 at 8:45
  • Does Category have the same behavior with php artisan tinker? $cat = Category::find(10); shows the expected output? I fail to detect an obvious error.. maybe the model name Category is ambiguous? Commented Jun 29, 2021 at 8:53
  • Category::where('cat_id', 25) is just the Eloquent query. You should call Category::where('cat_id', 25)->first() to make a comparison with ->find(25) Commented Jul 2, 2021 at 15:56

2 Answers 2

2
+50

The problem is because you are using SoftDeletes so soft deleted models will automatically be excluded from query results. In your case, look like Category with id 22, 23, 24, 25 are soft deleted. To get it, you need to use withTrashed() as mentioned in the doc. For example:

Category::withTrashed()->find(22)->slug
Sign up to request clarification or add additional context in comments.

2 Comments

But Category::where works and they should not return trashed records
@imaginabit OP just called dd(Category::where('cat_id', 25)), not dd(Category::where('cat_id', 25)->first()) so it's not actually return the result.
0

per an answer above: if you are using soft deletes you need to add Category::withTrashed()

However, you can wrap the command in an optional() helper function.

optional(Category::find(22))->slug

// if you are using soft delete
optional( Category::withTrashed()->find(22) )->slug

this will return null if 22 does not exist instead of throwing an exception error.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.