Insert data from multistep form with Laravel Relationships - laravel

I have a question for you.
I have a multisteps form:
Loan (this is sent from the calculator)
Create an account (email + password)
Personal data (first name, last name, phone number etc ...)
Addresses (street, city, state + if have correspondence address etc ...)
Employment (name of employer, address of employer, etc ...)
Finish (Review your data before sending ...)
the total number of inputs that are in the form is 60
I would like to split it into more tables
users
loans
personal
addresses
eployment
While I tried this, but something tells me that this method is not very safe even though it is working.
Therefore, I am turning here for advice and help as you would have done something like this?
Model User.php
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function loans() {
return $this->belongsToMany(Loan::class)->withTimestamps();
}
public function personal() {
return $this->belongsTo(Personal::class);
}
public function adress() {
return $this->belongsTo(Adress::class);
}
public function employment() {
return $this->belongsTo(Eployment::class);
}
}
Model Loan.php
class Loan extends Model
{
protected $hidden = ['amount', 'month', 'payment'];
public function users() {
return $this->belongsToMany(User::class)->withTimestamps();
}
}
Model Personal.php
class Personal extends Model
{
protected $fillable = [
'first_name', 'last_name', 'identification_number', 'identity_card_number', 'date_of_birth', 'phone'
];
public function users() {
return $this->hasMany(User::class);
}
}
Model Adress.php
protected $fillable = [
'adress', 'adress_number', 'city', 'postcode', 'country', 'correspond_adress', 'correspond_adress_number', 'correspond_city', 'correspond_postcode', 'correspond_country', 'type_of_housing', 'since_year', 'marital_status', 'number_of_kids'
];
public function users() {
return $this->hasMany(User::class);
}
Model Employment.php
class Eployment extends Model
{
protected $fillable = [
'type_of_occupation', 'client_ico', 'client_dic', 'employer_name', 'employer_ico', 'employment_adress', 'employment_city', 'month_of_arrival', 'year_of_arrival', 'net_monthly_income', 'other_income', 'payment_method', 'expenditure_payments', 'loan_repayments', 'wage_deductions', 'other_expenditure', 'have_bank_account', 'iban_account'
];
public function users() {
return $this->hasMany(User::class);
}
}
DB: (users, loans, personal, adresses, eployment
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::create('loans', function (Blueprint $table) {
$table->id();
$table->string('amount');
$table->string('month');
$table->string('payment');
$table->timestamps();
});
Schema::create('loan_user', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('loan_id');
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('loan_id')->references('id')->on('loans')->onUpdate('cascade')->onDelete('cascade');
$table->timestamps();
});
Schema::create('adresses', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('adress');
$table->string('adress_number');
$table->string('city');
$table->string('postcode');
$table->string('country');
$table->string('correspond_adress')->nullable();
$table->string('correspond_adress_number')->nullable();
$table->string('correspond_city')->nullable();
$table->string('correspond_postcode')->nullable();
$table->string('correspond_country')->nullable();
$table->string('type_of_housing');
$table->string('since_year');
$table->string('marital_status');
$table->string('number_of_kids');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
Schema::create('eployments', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('type_of_occupation');
$table->string('client_ico')->nullable();
$table->string('client_dic')->nullable();
$table->string('employer_name')->nullable();
$table->string('employer_ico')->nullable();
$table->string('employment_adress')->nullable();
$table->string('employment_city')->nullable();
$table->string('month_of_arrival')->nullable();
$table->string('year_of_arrival')->nullable();
$table->string('net_monthly_income');
$table->string('other_income')->nullable();
$table->string('payment_method');
$table->string('expenditure_payments');
$table->string('loan_repayments')->nullable();
$table->string('wage_deductions')->nullable();
$table->string('other_expenditure')->nullable();
$table->string('have_bank_account');
$table->string('iban_account');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
LoanController.php
public function store(Loan $loan, User $user, Personal $personal, Adress $adress, Eployment $eployment, Request $request)
{
$user = User::create([
'email' => $request->email,
'password' => Hash::make($request->password),
]);
$data = new Loan;
$data->amount = $request->amount;
$data->month = $request->month;
$data->payment = $request->payment;
$personal = new Personal;
$personal->user_id = $user->id;
$personal->first_name = $request->first_name;
$personal->last_name = $request->last_name;
$personal->identification_number = $request->identification_number;
$personal->identity_card_number = $request->identity_card_number;
$personal->date_of_birth = $request->date_of_birth;
$personal->phone = $request->phone;
$adress = new Adress;
$adress->user_id = $user->id;
$adress->adress = $request->adress;
$adress->adress_number = $request->adress_number;
$adress->city = $request->city;
$adress->postcode = $request->postcode;
$adress->country = $request->country;
$adress->correspond_adress = $request->correspond_adress;
$adress->correspond_adress_number = $request->correspond_adress_number;
$adress->correspond_city = $request->correspond_city;
$adress->correspond_postcode = $request->correspond_postcode;
$adress->correspond_country = $request->correspond_country;
$adress->type_of_housing = $request->type_of_housing;
$adress->since_year = $request->since_year;
$adress->marital_status = $request->marital_status;
$adress->number_of_kids = $request->number_of_kids;
$eployment = new Eployment;
$eployment->user_id = $user->id;
$eployment->type_of_occupation = $request->type_of_occupation;
$eployment->client_ico = $request->client_ico;
$eployment->client_dic = $request->client_dic;
$eployment->employer_name = $request->employer_name;
$eployment->employer_ico = $request->employer_ico;
$eployment->employment_adress = $request->employment_adress;
$eployment->employment_city = $request->employment_city;
$eployment->month_of_arrival = $request->month_of_arrival;
$eployment->year_of_arrival = $request->year_of_arrival;
$eployment->net_monthly_income = $request->net_monthly_income;
$eployment->other_income = $request->other_income;
$eployment->payment_method = $request->payment_method;
$eployment->expenditure_payments = $request->expenditure_payments;
$eployment->loan_repayments = $request->loan_repayments;
$eployment->wage_deductions = $request->wage_deductions;
$eployment->other_expenditure = $request->other_expenditure;
$eployment->have_bank_account = $request->have_bank_account;
$eployment->iban_account = $request->iban_account;
$data->save();
$user->personal()->associate($user);
$personal->save();
$user->adress()->associate($user);
$adress->save();
$user->eployment()->associate($user);
$eployment->save();
$user->loans()->attach($data);
return redirect('/');
}
I don't know if I understood Laravel Relationships correctly but I try to ...
Excuse my English I'm Slovak and I helped with Google Translator

Well if you want to do everything with one request (building large form and manipulating it with JS) your store method is quite good.
You have to remove Loan $loan, User $user, Personal $personal, Adress $adress, Eployment $eployment, from your store method because your are not using model route binding for this: https://laravel.com/docs/5.8/routing#route-model-binding.
Also you don't need to associate ($user->personal()->associate($user);) or attach everything because you already adding user_id to every model you create.
Also don't forget form validation.

Related

Cant persist data to the database? (Laravel)

I'm new to Laravel. My data doesn't seem to get to the database. Here's my Controller code:
public function store(Request $request)
{
$rules = [
'first_name' => 'required|string|min:3|max:255',
'last_name' => 'required|string|min:3|max:255',
'gender' => 'required|string|min:3|max:255',
'qualifications' => 'required|string|min:3|max:255'
];
$validator = FacadesValidator::make($request->all(), $rules);
if ($validator->fails()) {
return redirect('/home')
->withInput()
->withErrors($validator);
} else{
$data = $request->input();
try {
$crud = new CRUD;
$crud->first_name = $request->get('first_name');
$crud->last_name = $request->get('last_name');
$crud->gender = $request->get('gender');
$crud->qualifications = $request->get('qualifications');
$crud->save();
return redirect('/home')->with('status',"Insert successfully");
} catch (\Throwable $th) {
return redirect('/home')->with('failed',"Insert Failed");
}
}
}
And my Model:
protected $table = '$user';
protected $fillable = [
'first_name', 'last_name', 'gender', 'qualifications'
];
public $timestamps = true;
And my route(Web.php):
Route::post('/store', [App\Http\Controllers\CRUDController::class, 'store'])->name('insert');
Here is my migration for the users table:
Schema::create('user', function(Blueprint $table) {
$table->bigIncrements('id');
$table->string('first_name');
$table->string('last_name');
$table->string('gender');
$table->string('qualifications');
$table->timestamps();
});
You should fix your table name in your model, since your table is called user:
protected $table = "user";
Also, you can use mass assignment to fill your models:
$crud = CRUD::create($request->validated());
It should fix your problem.

Completion system with Laravel + VueJS

I've been tring for hours to define my relations for a completion system but I failed.
I have a table Users and a table Episodes, and I would like to get in my views if the User has completed an episode.
I created a "completions" table with user_id and episode_id and a boolean field called "completed"
Is it a manytomany relation ? I'd like to have something like $episode->completed which gave me True if the logged in user finished the course, but I can't find my way... I just wanna know how to define my relations, not a whole work done.
Thank you very much !!!!
I believe you can tell Laravel what table to use and also query the pivot column.
//user model
public function episodes(){
return $this->belongsToMany( 'App\Episode', 'completions', 'user_id', 'episode_id' );
}
public function completedEpisodes(){
return $this->belongsToMany( 'App\Episode', 'completions', 'user_id', 'episode_id' )
->wherePivot('completed','=', true)->get();
}
//episode model
public function users(){
return $this->belongsToMany( 'App\User', 'completions', 'episode_id', 'user_id' );
}
The alternative would be to create your pivot as episode_user and laravel will auto detect it as the pivot, add a completed boolean to that table and it would function with just:
//user model
public function episodes(){
return $this->belongsToMany('App\Episode');
}
public function completedEpisodes(){
return $this->belongsToMany('App\Episode')
->wherePivot('completed','=', true)->get();
}
//episode model
public function users(){
return $this->belongsToMany('App\User');
}
Query if episode is complete:
//maybe, haven't tried this
//user
public function hasCompletedEpisode($id){
return $this->belongsToMany('App\Episode')->wherePivot('episode_id','=', $id)
->wherePivot('completed','=', true)->get();
}
//episode
public function hasCompletedEpisode($id){
$user_id = Auth::id();
return $this->belongsToMany('App\User')->wherePivot('user_id','=', $user_id)
->wherePivot('completed', true)->get();
}
If I was you, I would use a custom intermediate table. You can implement this as follows:
Migrations
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
Schema::create('episodes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
Schema::create('watches', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('epsiode_id');
$table->boolean('completed');
$table->timestamps();
});
Models
class User extends Model
{
protected $guarded = [];
public function watchedEpisodes()
{
return $this->hasMany(Episode::class)->using(Watch::class);
}
public function watches()
{
return $this->hasMany(Watch::class);
}
}
class Episode extends Model
{
protected $guarded = [];
public function users()
{
return $this->hasMany(User::class)->using(Watch::class);
}
}
class Watch extends \Illuminate\Database\Eloquent\Relations\Pivot
{
protected $table = 'watches';
protected $guarded = [];
public static function create(User $user, Episode $episode, bool $completed = false)
{
$watch = new self();
$watch->user_id = $user->id;
$watch->epsiode_id = $episode->id;
$watch->completed = $completed;
$watch->save();
return $watch;
}
public function user()
{
return $this->hasOne(User::class);
}
public function episode()
{
return $this->hasOne(User::class);
}
}
Example Use
$user = User::create(['name' => 'Big Watcher']);
$episode1 = Episode::create(['name' => 'Episode 1']);
$episode2 = Episode::create(['name' => 'Episode 2']);
$episode3 = Episode::create(['name' => 'Episode 3']);
$episode4 = Episode::create(['name' => 'Episode 4']);
Watch::create($user, $episode1);
Watch::create($user, $episode2);
Watch::create($user, $episode3);
return $user->watchedEpisodes;

Saving Relationships in Laravel on the go

I have a user model that is related to a enduser model with a hasOne relationship.
I want to save the inputs of a form field in both user and enduser model in one go.
here is my code;
$user = new User();
$user->first_name = $request->first_name;
$user->last_name = $request->last_name;
$user->phone_number = $request->phone_number;
$user->email = $request->email;
$user->password = $request->password;
$user->enduser->username = $request->username;
$user->save();
return user;
my relationships are
Class User extends Authenticable
{
public function enduser()
{
return $this->hasOne('App\Enduser');
}
}
my migration for users is thus
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('phone_number');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->enum('roles',['admin', 'enduser', 'organization', 'radio', 'employee'])->nullable();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
and for enduser is thus
public function up()
{
Schema::create('endusers', function (Blueprint $table) {
$table->increments('id');
$table->text('address')->nullable();
$table->string('lga')->nullable();
$table->string('city')->nullable();
$table->string('state')->nullable();
$table->string('username');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('endusers');
}
i get an error that says 'unknown column 'username' in where clause'. How can i save such relationship?
Your user model should look like this.
User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'first_name','last_name','phone', 'email', 'password',
];
public function enduser() {
return $this->hasOne('App\EndUser');
}
}
and your EndUser model should look like this
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class EndUser extends Model
{
use Notifiable;
protected $fillable = [
'username',
];
public function user() {
return $this->belongsTo('App\User');
}
}
Then you can write this code in your controller
//first becrypt your password
$request->merge('password',bcrypt($request->password));
$user = User::create(
$request->only(['first_name','last_name','phone','email','password'])
);
$user->enduser->create([$request->username]);
return $user;
Therefore, in order to accomplish what you're trying to do, you can do first create the user then associate the enduser with it by doing the following:
$user = new User();
$user->first_name = $request->first_name;
$user->last_name = $request->last_name;
$user->phone_number = $request->phone_number;
$user->email = $request->email;
$user->password = $request->password;
$user->save();
$endUser = new EndUser();
$endUser->username = $request->username;
$user->enduser()->save($endUser);
return $user;
However, prior to doing this, you must ensure your User and enduser models have their relationships set up correctly.
Maybe you can solve it using laravel observers:
php artisan make:observer UserObserver --model User
This command will create a UserObserver.php file inside app/Observers folder.
Add this to the created function:
public function created(User $user)
{
EndUser::create([
'username' => $user->username,
'user_id' => $user->id,
]);
}
You also need to edit the AppServiceProvider file, inside the app/Providers folder. Add this to the boot method:
public function boot()
{
User::observe(UserObserver::class);
}
Now, every time that a User get registered, it will create a EndUser with the user data.
Hope it helps.

Laravel update hasMany relationship

I just read more documentation and issues for this problem to resolve that , but nothing work for me, i have 2 table wit this models:
class Month extends Model
{
protected $guarded = ['id'];
public function lessons()
{
return $this->hasMany(Lesson::class);
}
}
class Lesson extends Model
{
protected $guarded = ['id'];
public function months()
{
return $this->belongsTo(Month::class);
}
}
I can save some data with relation ship with this sample code and that work fine:
$month = Month::find(1);
$lesson = new Lesson();
$lesson->title = $request->title;
$lesson->content = $request->content;
$lesson->file_url = $uploadedFilePath;
$lesson->filename = $filename;
$lesson->time = $request->time;
$month = $month->lessons()->save($lesson);
now I try to update some lesson fields with this code:
$lesson = Lesson::find($id);
$lesson->update([
'title' => $request->title,
'content' => $request->content,
'file_url' => $uploadedFilePath,
'filename' => $filename,
'time' => $request->time
]);
$month = Month::find($request->months['0']);
$lesson = Lesson::find($lesson->id);
$lesson->months()->associate($month)->save();
in that I try to change month_id column on Lesson table in database with for example $month->id value, how can I do that?
UPDATED:
class Lesson extends Model
{
protected $guarded = ['id'];
public function month()
{
return $this->belongsTo(Month::class);
}
}
Controller:
$lesson->update([
'title' => $request->title,
'content' => $request->content,
'file_url' => $uploadedFilePath,
'filename' => $filename,
'time' => $request->time
]);
$month = Month::find($request->months['0']);
$lesson = Lesson::find($lesson->id);
$lesson->month()->associate($month)->save();
$lesson->update(['month_id' => $month->id]);
Migrations:
Schema::create('months', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('description');
$table->string('price');
$table->timestamps();
});
Schema::create('lessons', function (Blueprint $table) {
$table->increments('id');
$table->integer('month_id')->unsigned();
$table->string('title');
$table->longText('content');
$table->string('file_url');
$table->string('filename');
$table->string('time', 50);
$table->foreign('month_id')->references('id')->on('months')->onDelete('cascade');
$table->timestamps();
});
Firstly, the name of method months() should be renamed to month because it's returning single month not multiple months.
Secondly, If your Lesson has a field called month_id then its far simpler than you are thinking about. We can just change these two lines:
$lesson = Lesson::find($lesson->id);
$lesson->months()->associate($month)->save();
To the following line:
$lesson->update(['month_id' => $month->id);
It should update the month_id of your $lesson to the $month->id

Laravel: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails

im having this error on my Laravel Application:
Cannot add or update a child row: a foreign key constraint fails
Here are my 2 tables
Users
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username');
$table->string('password');
$table->string('firstname');
$table->string('lastname');
$table->string('middlename');
$table->string('address');
$table->string('birthday');
$table->string('contact');
$table->string('email');
$table->enum('isAdmin', array(0,1))->default(0);
$table->enum('isTeacher', array(0,1))->default(0);
$table->timestamps();
});
Attendance
Schema::create('attendance', function(Blueprint $table)
{
$table->increments('id');
$table->string('status');
$table->string('comment');
$table->integer('student_id')->unsigned();
$table->foreign('student_id')->references('id')->on('users');
$table->string('student_firstname');
$table->string('student_lastname');
$table->integer('teacher_id')->unsigned();
$table->foreign('teacher_id')->references('id')->on('users');
$table->string('teacher_firstname');
$table->string('teacher_lastname');
$table->timestamps();
Here's my models
User
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = array('password', 'remember_token');
public function attendance()
{
return $this->belongsTo('Attendance');
}
Attendance
class Attendance extends Eloquent
{
public function users()
{
return $this->hasMany('User');
}
}
**And finally here's my controller **
public function postCreateAttendance()
{
$validate = Validator::make(Input::all(), array(
'status' => 'required'
));
if ($validate->fails())
{
return Redirect::route('viewStudent')->withErrors($validate)->withInput();
}
else
{
//$student = User::whereRaw('isTeacher = "0" and isAdmin = "0"')->get();
//$teacher = User::whereRaw('isTeacher = "1" and isAdmin = "0"')->get();
$student = new User();
$teacher = new User();
$attendance = new Attendance();
$student->save();
$teacher->save();
$attendance->save();
$student->student_id = Input::get('3');
$attendance->status = Input::get('status');
$attendance->comment = Input::get('comment');
$attendance->student_firstname = Input::get('student_first');
$attendance->student_lastname = Input::get('student_last');
$attendance->teacher_firstname = Input::get('teacher_first');
$attendance->teacher_lastname = Input::get('teacher_last');
I tried this too as well and its giving me the same error.
public function sampleAttendance()
{
User::find(1)->attendance()->insert(array(
'status' => 'present',
'comment' => 'none',
'student_id' => '1',
'student_firstname' => 'student_first',
'student_lastname' => 'student_last',
'teacher_firstname' => 'teacher_first',
'teacher_lastname' => 'teacher_last'
));
Please help? :(
In your schema, you have placed integrity constraints, aka foreign keys in your attendance table.
Therefore you cannot save an attendance record without adding its student_id and teacher_id first.
This will not work:
$student = new User();
$teacher = new User();
$attendance = new Attendance();
$student->save();
$teacher->save();
$attendance->save();
This will work:
$student = new User();
$teacher = new User();
//Saves student model
$student->save();
$teacher->save();
//Saves Attendance model in relationship with student and teacher model
$attendance = new Attendance([
'student_id' => $student->id,
'teacher_id' => $teacher->id
]);
$attendance->save();
Why can't i just associate the attendance model with the student and teacher models and then save them to the DB?
See Laravel Framework Issue: https://github.com/laravel/framework/issues/6437
It's been proposed but not yet implemented.

Resources