Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FrancineHuang's avatar

Forms not passing data to database but with no error (Laravel9)

I am new to laravel and now I am making a recipe create form and gonna insert multiple tables on laravel 9. In my recipe create form, I need to insert 3 tables into the database. These tables are "Recipes(main part to create a recipe)", "ingredients(where users can write down the food materials and quantities)" and "steps(where users can explain how to cook in each step)". Now the problem is, I try to submit the form and insert the data into DB but the page just return to the form create page. No error occored and nothing inserted into DB.

I tried two ways to fix the problem. First, I write down every column I need to use by "protected $fillable" in the "recipe", "step" and "ingredient" models. Second, I defined the relationships into the recipe model. But the problem haven't been solved yet and the same error still happened.

Here are what I code in models and controllers.

Recipe model:

class Recipe extends Model
{
    use HasFactory;

    protected $fillable = [
        'cover_photo_path',
        'title',
        'introduction',
        'person',
        'tip'
    ];


    public function getRecipe() {
        $query_step = Request::query('step');
        $query_ingredient = Request::query('ingredient');
        $query_recipes = Recipe::query()
            ->select('recipes.*')
            ->where('user_id', '=', Auth::id())
            ->whereNull('deleted_at');
    
        if(!empty($query_step)) {
            $query_recipes->leftJoin('recipe_steps', 'recipe_steps.recipe_id', '=', 'recipes.id')
                ->where('recipe_steps.step_id', '=', $query_step);
        }
    
        if(!empty($query_ingredient)) {
            $query_recipes->leftJoin('recipe_ingredients', 'recipe_ingredients.recipe_id', '=', 'recipes.id')
                ->where('recipe_ingredients.ingredient_id', '=', $query_ingredient);
        }
    
        $recipes = $query_recipes->get();
    
        return $recipes;
    }

    /**
     * The steps that belong to the recipe.
     */
    public function steps() {
        return $this->belongsToMany(Step::class, 'recipe_steps', 'recipe_id', 'step_id');
    }

    /**
     * The ingredients that belong to the recipe.
     */
    public function ingredients() {
        return $this->belongsToMany(Ingredient::class, 'recipe_ingredients' ,'recipe_id', 'ingredient_id');
    }

}

Step model:

class Step extends Model
{
    use HasFactory;

    protected $fillable = [
        'step_photo_path',
        'content'
    ];

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

}

Ingredient Model:

class Ingredient extends Model
{
    use HasFactory;

    protected $fillable =[
        'material',
        'quantity'
    ];

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

}

Recipe Controller:

    public function storeNewRecipe(Request $request) {
        $request->validate([
            'cover_photo_path' => 'required|image|max:5120',
            'title' => 'required',
            'introduction' => 'required', 
            'person' => 'required',
            'tip' => 'required',

            'ingredients' => 'required|array',
            'ingredients.*.material' => 'required',
            'ingredients.*.quantity' => 'required',
            
            'steps' => 'required|array',
            'steps.*.content' => 'required',
            'steps.*.step_photo_path' => 'nullable|image|max:5120',

        ]);
    
        // Begin transaction
        DB::beginTransaction();

        try {
            $user = User::find(auth()->id());
    
            // Save the recipe details
            $recipe = new Recipe();
            $recipe->title = strip_tags($request->input('title'));
            $recipe->introduction = strip_tags($request->input('introduction'));
            $recipe->person = strip_tags($request->input('person'));
            $recipe->tip = strip_tags($request->input('tip'));
            $recipe->user_id = $user->id;
    
            // Save the cover photo
            $filename = 'cover-' . $user->id . '-' . uniqid() . '.jpg';
            $coverImg = Image::make($request->file('cover_photo_path'))->fit(800, 600)->encode('jpg');
            Storage::put('public/cover_image/' . $filename, $coverImg);
            $recipe->cover_photo_path = $filename;
    
            // Save the recipe
            $recipe->save();
    
            // Save the ingredients
            foreach ($request->ingredients as $ingredient) {
                $newIngredient = new Ingredient();
                $newIngredient->material = $ingredient['material'];
                $newIngredient->quantity = $ingredient['quantity'];
                $newIngredient->recipe_id = $recipe->id;
                $newIngredient->save();
            }
    
            // Save the steps
            foreach ($request->steps as $step) {
                $newStep = new Step();
                $newStep->content = $step['content'];
                $newStep->recipe_id = $recipe->id;
    
                if ($stepPhoto = $step['step_photo_path']) {
                    $filename = 'step-' . $user->id . '-' . uniqid() . '.jpg';
                    $stepImg = Image::make($stepPhoto)->fit(300, 300)->encode('jpg');
                    Storage::put('public/step_image/' . $filename, $stepImg);
                    $newStep->step_photo_path = $filename;
                }
    
                $newStep->save();
            }
    
            // Commit the transaction
            DB::commit();
    
            return redirect()->route('recipes.show', ['id' => $recipe->id]);
        } catch (Exception $e) {
            // Rollback the transaction on error
            DB::rollback();
            Log::error($e->getMessage());
            return back()->withInput()->withErrors(['message' => 'There was an error creating the recipe.']);
        }
    }

So, I wonder if there is something strange in my model or controller, and how to fix the problem. I really appreciate for your help. Thank you so much!

0 likes
4 replies
AddWebContribution's avatar

@francinehuang first check whether the route which are defined for storing it, is actually in this file or not and you find it put check dd($request->all()) and check whether data is there or not

1 like
FrancineHuang's avatar

@saurabhd Thanks for your solution! Do you know where should I put the dd($request->all())? I can't use dd() as well because every time when try to submit the form, it doesn't gave me a response to show those array data. Sorry for my weird question.

Snapey's avatar

do you have a mechanism to display validation errors in your view?

You might not be getting past the first validation step?

1 like

Please or to participate in this conversation.