Home > Net >  Laravel whereLike join query - undefined method from relationship?
Laravel whereLike join query - undefined method from relationship?

Time:05-04

I was wondering whether anyone can please help me understand where I am going wrong with the following:

I am trying to write a query which errors and I am unsure why?

    MealNutritionalInformation::join('meals', 'meal_nutritional_information.id', '=', 'meals.id')
            ->whereLike(['meals.id'], $this->search)
            ->orderBy($this->sortField, $this->sortDirection)
            ->paginate(10)

It appears that the whereLike gives the following:

Call to undefined method App\Models\MealNutritionalInformation::meals()

I have set up the following relationships on my meal and meal nutritional information models.

Nutritional:

public function meal()
{
    return $this->hasOne(Meal::class, 'id', 'meal_id');
}

Meals:

public function meal_nutritional_information()
{
    return $this->hasMany(MealNutritionalInformation::class, 'meal_id', 'id');
}

CodePudding user response:

You make mistake in the join query :- meal_nutritional_information.id insteadad of meal_nutritional_information.meal_id.

 MealNutritionalInformation::join('meals','meal_nutritional_information.meal_id', '=', 'meals.id')
        ->whereLike(['meals.id'], $this->search)
        ->orderBy($this->sortField, $this->sortDirection)
        ->paginate(10)

CodePudding user response:

I have updated my whereLike() to the following which resolves the issue:

->where('meals.name', 'LIKE', '%' . $this->search . '%')
  • Related