Warum werden mir Rezepte als leeres Array zurückgegeben?

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: Warum werden mir Rezepte als leeres Array zurückgegeben?

by Guest » 30 Dec 2024, 16:37

Ich erstelle eine App in Laravel und Vue. Wenn ich die Zutaten in meine Suchleiste eingebe, wird mir eine leere Liste mit Rezepten zurückgegeben, obwohl ich absichtlich so eingestellt habe, dass mir eines zurückgegeben wird. Das ist mein SearchController.php

Code: Select all

public function search(Request $request)
{
$query = $request->query('query');
$ingredients = explode(",", $query);
$ingredients = array_map('strtolower', $ingredients);
$ingredients = array_map('trim', $ingredients);
$ingredientIds = Ingredient::whereIn('name', $ingredients)->pluck('id');
$recipeIds = RecipeIngredient::whereIn('ingredient_id', $ingredientIds)->pluck('recipe_id');
$recipes = Recipe::whereIn('id', $recipeIds)->get();

return response()->json([
'ingredients' => $ingredients,
'recipes' => $recipes,

]);
}
Recipe.php

Code: Select all

public function ingredients()
{
return $this->belongsToMany(Ingredient::class, 'recipes_ingredients', 'recipe_id', 'ingredient_id');
}

public function recipeIngredients()
{
return $this->hasMany(RecipeIngredient::class);
}
Ingredient.php

Code: Select all

protected $fillable = ['name'];

public function recipes()
{
return $this->belongsToMany(Recipe::class, 'recipes_ingredients', 'ingredient_id', 'recipe_id');
}

public function ingredientRecipes()
{
return $this->hasMany(RecipeIngredient::class);
}
RecipeIngredient.php

Code: Select all

protected $table = 'recipes_ingredients';

public $timestamps = false;

public function recipe()
{
return $this->belongsTo(Recipe::class);
}

public function ingredient()
{
return $this->belongsTo(Ingredient::class);
}
Es soll mir alle Rezepte mit dieser Zutat anzeigen, aber ich bekomme nichts. Ich habe ChatGPT die Chance gegeben, es zu debuggen, und es hat zu einer zehnfachen Fehlerquote geführt. Bei Bedarf stelle ich Ihnen zusätzlichen Code zur Verfügung

Top