0

I need help inserting data to multiple tables after the user register Suppose I have the code below, what is the best way for creating a new user and storing data into multiple tables.

RegisterUser Trait

<?php

namespace App\Traits;

use App\User;
use App\Profile;
use App\Account;
use Keygen;

trait RegisterUser
{
    public function registerUser($fields)
    {   


        $user = User::create([

            'username'      => $fields->username,
            'accno'        =>  $this->generateAccountNumber(),
            'email'      => $fields->email,
            'password'  => $fields->password = bcrypt(request('password')),
            'roles'  => $fields->roles,
            'activation_code' =>  $this->generateToken()
        ]);

            Profile::create([
                'accno' => $user->accno,
                'username' => $user->username,
                'acc_type'      => $fields->acc_type,
                'firstname'      => $fields->firstname,
                'lastname'      => $fields->lastname,
                'nationality'     => $fields->nationality,
                'occupation'     => $fields->occupation,
                'address'     => $fields->address,
                'city'     => $fields->city,
                'state'     => $fields->state,
                'zipcode'     => $fields->zipcode,
                'phoneno'     => $fields->phoneno,
                'dob'     => $fields->dob,
                'gender'     => $fields->gender,
                'martial_status'     => $fields->martial_status,
                'user_image'     => $fields->user_image,
            ]);

            Account::create([
                'accno' => $user->accno,
                'username' => $user->username,
            ]);





            return $user;

    }

with this registrationController;

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use App\Http\Requests\RegistrationRequest;
use App\Traits\RegisterUser;

class RegistrationController extends Controller
{
    use RegisterUser;

    public function show()
    {
        return view('auth/register');
    }

    public function register(RegistrationRequest $requestFields)
    {
      //calling the registerUser method inside RegisterUser trait.
      $user = $this->registerUser($requestFields);

      return redirect('/login');

     }
}

but now each time I try to register the user, I will get a page not found(404) error and the data are only inserted into the user table.

Toby Nwude
  • 391
  • 7
  • 24
  • What do your models look like? I think you should configure the `fillable` and `guarded` attribute with your fields from the request. Here is the documentation: https://laravel.com/docs/6.x/eloquent#mass-assignment – UfguFugullu Apr 08 '20 at 06:51
  • please i need help using this to solve my problem, i have included the guarded attribute in my User,Profile and Account model and my fillable is correct – Toby Nwude Apr 08 '20 at 07:07
  • What do your models look like? Do you get errors in the log file of laravel? – UfguFugullu Apr 08 '20 at 07:08
  • how can I check for errors? – Toby Nwude Apr 08 '20 at 16:22
  • The default directory of the logging file is ROOT -> storage -> logs – UfguFugullu Apr 09 '20 at 06:15
  • I properly delete the voyager package using this [https://stackoverflow.com/questions/58124293/how-to-safely-uninstall-voyager-in-laravel][1] – Toby Nwude Apr 14 '20 at 13:24

1 Answers1

0

Do not change the registry controller The best way is to use an observer with a command

php artisan make: observer RegisterObserver -m Users

An observer is created and does his work in the created method

Then in the AppServiceProvider file provider Write the boot method of this command

User:observe(RegisterObserver::class)

To redirect to the desired page, change this

protected $redirectTo={path}
Hasan
  • 49
  • 4
  • 1
    Observer is not best place for this type of task. Event/Listener would be way better. You would like to use Observer when you make some change over (in this case of observer) User's object. Passing full blown `$fields` object to observer and making lot of non-related actions in observer is not good way. In UserObserver you would like to make changes to user object and nothing else trying to keep code clean. Think this way what is observer: it observes what is happening with user object - not what is happening with non-related objects. – Tpojka Apr 08 '20 at 07:55