Laravel save data from one form to different database tables - php

I want to create a student with some courses.
This is the Laravel view
I created two different tables: a students table
public function up()
{
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('student_name');
$table->string('first_name');
$table->string('last_name');
$table->string('email');
});
}
and a courses table.
public function up()
{
Schema::create('student_courses', function (Blueprint $table) {
$table->id();
$table->unsignedInteger('student_id');
$table->string('course_name');
$table->timestamps();
});
}
The students table creates the information of the student and saves it in the student table.
I want to save the courses into the courses table, with the student id. They should have a One to Many relationship. How can I save the different courses into the courses table with the id of the specific student?

You can't store course name in student courses table. You need to create separate tables for courses. After that you can use foreign key for store both the data of student and courses into the student_courses table as per below.
public function up()
{
Schema::create('student_courses', function (Blueprint $table) {
$table->id();
$table->integer('student_id')->unsigned()->nullable();
$table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');
$table->integer('course_id')->unsigned()->nullable();
$table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
$table->timestamps();
});
}
Hope this will helps you.

First create Relationship:
In User Model Create:
public function courses(){
return $this->hasMany(Course::class);
}
In Course Model Create:
public function students(){
return $this->belongsTo(Studnet::class);
}
Now in your save function:
$course = new Course($request->all());
$user->courses()->save($course);

Related

Change Primary ID in laravel Migration

I'm creating a table where I want to use the ID from this table "menu_items" to be added in another table
Table one: menu_items
Schema::create('menu_items', function (Blueprint $table) {
$table->id('menu_level_item_id');
$table->timestamps();
$table->string('menu_item_name');
$table->string('menu_item_desc');
Table 2 products
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('product_name');
$table->string('product_desc');
$table->integer('price');
$table->integer('menu_level_item_id');
$table->timestamps();
});
My aim of doing this is that I can then create a relationship between the 2 tables as they have the same key?
Is there a different approach I can take? Should I create a unique key when creating a menu item and then add this to the second table?
Thank you.
Basically Laravel Eloquent do the key handling. When you have two tables which both has as key the name id, this is not a problem. Name the keys in the relation table just like this
table1_id
table2_id
Laravel will handle this in Eloquent. You are also able to name the two columns in the relation table to what ever you want. You could define it for the relation in Eloquent. E.g.
public function otherModel () {
return $this->belongsToMany('App\Models\OtherModel', 'table_name', 'this_model_id', 'other_model_id');
}
Please have a look into:
Laravel Relationship Documentation
Schema::create('menu_items', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('menu_item_name');
$table->string('menu_item_desc');
table->timestamp();
});
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('product_name');
$table->string('product_desc');
$table->integer('price');
$table->unsignedBigInteger('menu_level_item_id');
$table->timestamps();
$table->foreign('menu_level_item_id')->references('id')->on('menu_items');
});

Use two tables on a two-to-many relationship or use three tables with a pivot table

Basically, I have 2 tables in my database: Games and Teams.
Every Game must have 2 teams, so it's a two-to-many relationship.
Should I use 2 foreign keys in my Games table pointing to the 2 teams in the Teams table and have a one-to-many relationship, or use many-to-many relationship with a third table to link the games and teams table?
Im using Laravel 6.5 for the project, so I guess im using Eloquent to implement it.
Schema::create('games', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('team_a_id');
$table->foreign('team_a_id')->references('id')->on('teams')->onDelete('restrict');
$table->unsignedBigInteger('team_b_id');
$table->foreign('team_b_id')->references('id')->on('teams')->onDelete('restrict');
$table->unsignedInteger('team_a_score');
$table->unsignedInteger('team_b_score');
$table->string('status');
$table->boolean('finished');
});
Schema::create('teams', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('abbreviation');
$table->string('country');
$table->timestamps();
});
This are the two tables I have created by now, is this the correct way to implement it?
Use a Many to Many Relationship. In that case the Game Modal could have multiple Teams and vice-versa.
Hence the migrations would be like:
Schema::create('games', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('status');
$table->boolean('finished');
});
Schema::create('teams', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('abbreviation');
$table->string('country');
$table->timestamps();
});
Schema::create('game_team', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('game_id');
$table->integer('team_id');
$table->unsignedInteger('score');
$table->timestamps();
});

laravel is there a way to make 3 tables, one table belongs to 2 different tables, with one to one relationship

laravel is there a way to make, one to one relationship, one table belongs to two different tables one to one relationship.
i'm trying to model, a laravel based project, that is voting for ministers.
these are my three models.
members migration. ex: members is working as users for authentication
public function up()
{
Schema::create('members', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('username');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
2.ministers migration file
public function up()
{
Schema::create('ministers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->mediumText('description');
$table->string('image')->default('no-image.jpg');
$table->unsignedInteger('votes')->default('0');
$table->boolean('status')->default('0');
$table->timestamps();
});
}
3.voters migration file
public function up()
{
Schema::create('voters', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('phone');
$table->enum('education', ['primary', 'intermediate', 'high-school', 'university' ] )->nullable(false)->default('primary');
$table->string('country');
$table->unsignedBigInteger('minister_id');
$table->foreign('minister_id')->references('id')->on('ministers');
$table->unsignedBigInteger('member_id');
$table->foreign('member_id')->references('id')->on('members');
$table->timestamps();
});
}
this voters migration belongs to member and minister, and mister associates one voter, user associates to one voter.
so is this correct way to model, a system like this. which i want every registered member, fill out voting form, which selects a drop down field of the minister to be voted for. and save that form creating a new instance of voter. What i want also is two increment by one minister votes field when ever a new instance of voter is created.

How to make selection query for join tables in laravel?

I have users and staffs tables. I want to display staff details after the login. The staff display will list the of staff with the same section with the user who login. For example, if Adam (section/seksyen=A) login, the staff list displayed are the staffs of section A.
How to make query for that in laravel?
table users:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->string('section');
$table->string('type');
});
}
table staffs:
public function up()
{
Schema::create('staffs', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('nama');
$table->string('emel')->unique();
$table->string('seksyen');
$table->string('jawatan');
$table->string('alamat');
$table->string('no kakitangan');
});
I have tried this query but doesn't work.
$data['data'] = DB::table('staffs')
->join('users', 'users.section', '=', 'staffs.seksyen')
->where('seksyen','=','Auth(section)')->get();
The right code to extract user login section name is :
$section = Auth::user()->section;
$data['data'] = DB::table('staffs')->where('section','=', $section)->get();

What type of relationship these two tables will have in laravel?

i have four tables in database:
users (for storing user details)
conversations (for storing conversations id).
conversationsmember (for storing conversations member)
conversationsreply (for storing conversations reply)
A user will have many conversations and each conversation will have its members and replies.
here are the details:
Users migration:
$table->increments('id');
$table->string('name', 32);
$table->string('username', 32);
$table->string('email', 320);
$table->string('password', 64);
$table->timestamps();
Conversations migration:
$table->increments('id');
$table->timestamps();
conversationsmembers migration:
$table->increments('id');
$table->integer('conversation_id');
$table->integer('user_id');
$table->timestamps();
conversationsreply migrations
$table->increments('id');
$table->integer('conversation_id');
$table->integer('user_id');
$table->timestamps();
Now in User model, i need to define relationship between users table and conversations table. As they are not directly connected, i used hasManyThrough relation.
..app/models/User.php
...
public function conversations()
{
return $this->hasManyThrough('Conversation', 'ConversationsMember', 'conversation_id', 'id');
}
...
When i 'm trying to use it, it's showing a blank array.
I would try a belongsToMany relationship, where conversationsmembers is your pivot table.
public function conversations()
{
return $this->belongsToMany('Conversation', 'conversationsmembers');
}
You may also want to define the inverse of the relationship in your Conversation model:
public function users()
{
return $this->belongsToMany('User', 'conversationsmembers');
}
I'm a bit confuse about your migration so I'm not sure that's what you want.

Categories