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
Related articles
- php - i want to get data via multiple tables in laravel
- javascript - register multiple records from form to kintone
- php - i want to register a user image with laravel
- mysql - i want to register multiple string type strings in one column with rails postgresql
- i want to make a multiple search function with laravel
- php - laravel i want to handle data from two tables
- javascript - how can i register multiple functions in onchange with react?
- jquery - send multiple laravel js records
- python - is it possible to have relations for multiple tables
- how to reference numbers from multiple tables in mysql
- how to connect multiple databases with laravel
- wordpress - i want to be able to register multiple membership levels with the same email address with simplemembership
- Using Python to export multiple tables in batches from the database to Excel
- php - laravel cannot update tables with relationships
- mysql - is it ant to give the same index name to multiple tables in sql?
- i want to log in at the same time when i newly register with register by customization with multi-authentication of laravel
- ruby - i want to register data linked with multiple data from the rails console
- laravel - what is the way to set multiple roles when creating a user?
- Add multiple verification scenarios to your Laravel validator
- Laravel implements batch update of multiple data
Check if a category with the same name is stored in the DB
where
andfirst
If not found,
create
orfill/save
firstOrCreate
is acceptableIf 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 and
save
drills first(This time is irrelevant, but it circulates)
If you
save
a drill, you should get an idTo 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