jeudi 7 septembre 2017

Call to undefined method whereHas() in laravel 5.5

I am upgrading and refactoring my site to laravel 5.5, and this code is giving me a problem at the moment. I searched in the laravel github docs and didn't find any breaking changes that might affect this.

What I am trying to do is Related To section in my site, In every recipe page I want to display some more recipes that has the same category.

Here is my code:

    public static function getRelatedRecipes($recipe){

      $related_category_ids  = $recipe->category()->pluck('categories.id');

        return $relatedRecipes =
        Recipe::whereHas('categories', function ($q)use($related_category_ids){
        $q->whereIn('category_id', $related_category_ids);
        })
        ->where('id', '<>', $recipe->id)
        ->take(4)
        ->inRandomOrder()
        ->get();
}

This is the recipe model:

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Recipe extends Model
{
    protected $guarded=[];

    /**
     * Get the route key name for Laravel.
     *
     * @return string
     */
    public function getRouteKeyName()
    {
        return 'slug';
    }



    public function category()
    {
        return $this->belongsToMany('App\Category');
    }

}

What could be the problem?

Thanks,

P.S

If any other code that you think is needed to resolve this, please tell me and I will post it here. :)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire