I'm stuck on a problem in which I'm trying to save my data from my form into two different tables. I'm relatively new to laravel and I'm just modifying the project I created as an output from a tutorial. I've used dd() before in controllers however I quite can't wrap my head around using it in this situation where I'm using fortfy's CreateNewUser. Can someone guide me?
User.php
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Hash;
use Laravel\Sanctum\HasApiTokens;
use App\Models\Role;
use App\Models\Information_User;
class User extends Authenticatable implements MustVerifyEmail
{
use HasApiTokens, HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
'middlename',
'lastname',
'email',
'password',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
// public function setPasswordAttribute($password)
//{
// $this->attributes['password'] = Hash::make($password);
//}
public function roles()
{
return $this->belongsToMany('App\Models\Role');
}
//check if user has roles
public function hasAnyRole(string $role)
{
return null !== $this->roles()->where('name',$role)->first();
}
public function hasAnyRoles(array $role)
{
return null !== $this->roles()->whereIn('name',$role)->first();
}
public function Information_User()
{
return $this->hasOne('App\Models\Information_User');
}
}
Information_User.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Information_User extends Model
{
use HasFactory;
protected $fillable = [
'lastschoolyearattended'
'lastschoolattended',
'addressofthelastschoolattended',
'lastgradelevelcompleted',
'laststrandattended',
'schooltype',
'gradeleveltoenrolin',
'strandtoenrolin',
'returningstudent',
'housenumber',
'baranggay',
'municipality',
'province',
'phonenumber',
'birthday',
'age',
'sex',
'mothertongue',
'religion',
'generalaverage',
'lrnnumber',
'psastatus',
'psanumber',
'fatherfirstname',
'fathermiddlename',
'fatherlastname',
'motherfirstname',
'mothermiddlename',
'motherlastname',
'guardianfirstname',
'guardianmiddlename',
'guardianlastname',
'guardianrelationship',
'guardianphonenumber',
'guardianemail'
];
public function users()
{
return $this->belongsTo('App\Models\User');
}
}
CreateNewUser.php
<?php
namespace App\Actions\Fortify;
use App\Models\User;
use App\Models\Role;
use App\Models\Information_User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\CreatesNewUsers;
class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;
/**
* Validate and create a newly registered user.
*
* @param array $input
* @return \App\Models\User
*/
public function create(array $input)
{
Validator::make($input, [
'email' => [
'required',
'string',
'email',
'max:255',
Rule::unique(User::class)],
'password' => $this->passwordRules(),
'name' => ['required', 'string', 'max:255'],
'middlename' => ['required', 'string', 'max:255'],
'lastname' => ['required', 'string', 'max:255'],
'lastschoolyearattended' => ['required', 'string', 'max:255'],
'lastschoolattended'=> ['required', 'string', 'max:255'],
'addresslastschool'=> ['required', 'string', 'max:255'],
'lastgradelevelcompleted'=> ['required'],
'laststrandattended'=>['required'],
'schooltype'=> ['required'],
'gradeleveltoenrolin'=> ['required'],
'strandtoenrolin'=> ['required', 'string', 'max:255'],
'returningstudent'=> ['required'],
'housenumber'=> ['required', 'string', 'max:255'],
'baranggay'=> ['required', 'string', 'max:255'],
'municipality'=> ['required', 'string', 'max:255'],
'province'=> ['required', 'string', 'max:255'],
'phonenumber'=> ['required', 'string', 'max:11'],
'birthday'=> ['required'],
'age'=>['required'],
'sex'=> ['required'],
'mothertongue'=> ['required', 'string', 'max:255'],
'religion'=> ['required', 'string', 'max:255'],
'generalaverage'=> ['required', 'string', 'max:2'],
'lrnnumber'=> ['required', 'string', 'max:12'],
'psastatus'=> ['required'],
'psanumber'=>['integer', 'max:10'],
'fatherfirstName' => ['required', 'string', 'max:255'],
'fathermiddleName'=> ['required', 'string', 'max:255'],
'fatherlastName'=> ['required', 'string', 'max:255'],
'motherfirstname'=> ['required', 'string', 'max:255'],
'mothermiddlename'=> ['required', 'string', 'max:255'],
'motherlastname'=> ['required', 'string', 'max:255'],
'guardianfirstname'=> ['required', 'string', 'max:255'],
'guardianmiddleName'=> ['required', 'string', 'max:255'],
'guardianlastName'=> ['required', 'string', 'max:255'],
'guardianrelationship'=> ['required', 'string', 'max:255'],
'guardianphonenumber'=> ['required', 'string', 'max:11'],
'guardianemail'=> ['required', 'email','string', 'max:255'],
])->validate();
$user = User::create([
'email' => $input['email'],
'password' => Hash::make($input['password']),
'name' => $input['name'],
'middlename' => $input['middlename'],
'lastname' => $input['lastname'],
]);
$user->Information_User()->create([
'lastschoolyearattended'=> $input['lastschoyearattended'],
'lastschoolattended'=> $input['lastschoolattended'],
'addressofthelastschoolattended'=> $input['addressofthelastschoolattended'],
'lastgradelevelcompleted'=> $input['lastgradelevelcompleted'],
'laststrandattended'=> $input['laststrandattended'],
'schooltype'=> $input['schooltype'],
'gradeleveltoenrolin'=> $input['gradeleveltoenrolin'],
'strandtoenrolin'=> $input['strandtoenrolin'],
'returningstudent'=> $input['returningstudent'],
'housenumber'=> $input['housenumber'],
'baranggay'=> $input['baranggay'],
'municipality'=> $input['municipality'],
'province'=> $input['province'],
'phonenumber'=> $input['phonenumber'],
'birthday'=> $input['birthday'],
'age'=> $input['age'],
'sex'=> $input['sex'],
'mothertongue'=> $input['mothertounge'],
'religion'=> $input['religion'],
'generalaverage'=> $input['generalaverage'],
'lrnnumber'=> $input['lrnnumber'],
'psastatus'=> $input['psastatus'],
'psanumber'=> $input['psanumber'],
'fatherfirstname'=> $input['fatherfirstname'],
'fathermiddlename'=> $input['fathermiddlename'],
'fatherlastname'=> $input['fatherlastname'],
'motherfirstname'=> $input['motherfirstname'],
'mothermiddlename'=> $input['mothermiddlename'],
'motherlastname'=> $input['motherlastname'],
'guardianfirstname'=> $input['guardianfirstname'],
'guardianmiddlegame'=> $input['guardianmiddlename'],
'guardianlastname'=> $input['guardianlastname'],
'guardianrelationship'=> $input['guardianrelationship'],
'guardianphonenumber'=> $input['guardianphonenumber'],
'guardianemail'=> $input['guardianemail']
]);
$user->roles()->attach(4);
return $user;
}
}