Home>

I am currently making a typing game with laravel.

I'm thinking of updating/adding two tables and dividing them into 6 tables.

I used to register POST data inone tablebefore
Now you need to register in3 tables.
What should I do with the registration action in the controller at this time? I do n’t know.

In order to register data in drills, the category ID to be linked must be determined.

It is good to register the category_name part in the category table from among multiple POST values.
After that, how do I enter ids related to other tables in the drills table?

How can I get and register the ID of the drills that will be the main table for the problem table?

The id column of the drills table, category table, and problems table is auto-increment&primary key.

Here are the details of the app.

Table configuration
before ↓

・ Registration action in the current controller

public function store (Request $request)
    {
        $drill = new Drill;
        Auth :: user ()->drills ()->save ($drill->fill ($request->all ()));
        return redirect ('/ drills')->with ('flash_message', __ ('Registered.'));
    }


・ Current post form


                                <label for = "title">{{__ ('Title')}}</label>
                                
                                    <input type = "text" name = "title" value = "{{old ('title')}}" autocomplete = "title" autofocus>
                                    @error ('title')
                                    <span role = "alert">
                                        <strong>{{__ ("$message")}}</strong>
                                    </span>@enderror
                                
                            
                            
                                <label for = "category_name">{{__ ('Category')}}</label>
                                
                                    <input type = "text" name = "name" value = "{{old ('category_name')}}" autocomplete = "category_name" autofocus>
                                    @error ('category_name')
                                    <span role = "alert">
                                        <strong>{{__ ("$message")}}</strong>
                                    </span>
                                    @enderror
                                
                            
                            @for ($i = 1;$i<= 10;$i ++)
                            
                                <label for = "problem {{$i-1}}">{{__ ('Problem'). $i}}</label>
                                
                                    <input type = "text" name = "problem {{$i-1}}" value = "{{old ('problem'. ($i-1))}}" autocomplete = "problem {{$i- 1}} "autofocus>
                                    @error ('problem'. ($i-1))
                                    <span role = "alert">
                                    <strong>{{__ ("$message")}}</strong>
                                </span>
                                    @enderror
                                
                            
                        @endfor

after ↓

public function store (Request $request)
    {$category = new Category;
        $category->name = $request->name;
        $category->save ();
        $drill = new Drill;
        $drill->title = $request->title;
        $drill->user_id = Auth :: user ()->id;
        $drill->category_id = $category->id;
        $drill->save ();
        $problem = new Problem;
        $problem->drill_id = $drill->id;
        $problem->problem0 = $request->problem0;
        $problem->problem1 = $request->problem1;
        $problem->problem2 = $request->problem2;
        $problem->problem3 = $request->problem3;
        $problem->problem4 = $request->problem4;
        $problem->problem5 = $request->problem5;
        $problem->problem6 = $request->problem6;
        $problem->problem7 = $request->problem7;
        $problem->problem8 = $request->problem8;
        $problem->problem9 = $request->problem9;
        $problem->save ();
        // $drill = new Drill;
        // Auth :: user ()->drills ()->save ($drill->fill ($request->all ()));
        return redirect ('/ drills')->with ('flash_message', __ ('Registered.'));
    }


Based on the advice, I was able to register above!
The reason why category was not implemented as advised is that if you think about sharing category data with other users, you can change the value of name without permission.
I will use $guarded to make it unchangeable, and I will follow along with the advice.

  • Answer # 1

      

    In order to register data in drills, the category ID to be linked must be determined.

    Check if a category with the same name is stored in the DBwhereandfirst
    If not found,createorfill/save
    firstOrCreateis acceptable

      

    It is good to register the category_name part in the category table from among multiple POST values.
      After that, how do I enter ids related to other tables in the drills table?

    If you save, it contains the id of the model you saved, so use it.
    Save from the person who needs it first.
    If it is absolutely inconvenient, you can nullabe andsavedrills first
    (This time is irrelevant, but it circulates)

      

    How can I get and register the ID of the drills that will be the main table for the problem table?

    If yousavea drill, you should get an id

    To summarize, in order

    Category existence check

    Category registration if not

    save the drill

    save problem

    The order is

    .

    It ’s a digression
    Related Model Insert/Update Section
    If you understand, you can save cooler