divHelper11
divHelper11

Reputation: 2208

Saving to database after registration in laravel doesn't work

I am trying to save something called resources to database right after each user's registration in my application based on newest laravel.

As I know the best place to do that is register method in .../vendor/laravel/framework/src/Illuminate/Foundation/Auth/RegistersUsers.php.

So I added there __construct method and injected resourcesService, that contains createResources method saving it to database.

Unfortunately the app doesn't save anything, it doesn't even give me any errors so I don't understand what is going wrong.

Please let me know what you think.

RegistersUsers.php:

namespace Illuminate\Foundation\Auth;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Events\Registered;
use App\Http\Services\resourcesService;

trait RegistersUsers
{
    public $resourcesService;

    public function __construct(resourcesService $resourcesService)
    {
        $this->resourcesService = $resourcesService;
    }

    use RedirectsUsers;

    /**
     * Show the application registration form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showRegistrationForm()
    {
        return view('auth.register');
    }

    /**
     * Handle a registration request for the application.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));

        $this->guard()->login($user);

        $this->resourcesService->createResources();

        return $this->registered($request, $user)
                        ?: redirect($this->redirectPath());
    }

    /**
     * Get the guard to be used during registration.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard();
    }

    /**
     * The user has been registered.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function registered(Request $request, $user)
    {
        //
    }
}

resourcesService.php

namespace App\Http\Services;

use App\Resource;
use Auth;

class resourcesService
{

    public $resource;

    public function __construct(Resource $resource)
    {
        $this->resource = $resource;
    }


    public function getResources()
    {
        $resources = $this->resource::where("user_id", Auth::id())->first();
        return $resources;
    }


    public function createResources()
    {
        $resources = new $this->resource;
        $resources->user_id = Auth::id();
        $resources->wood = 0;
        $resources->stone = 0;
        $resources->food = 0;
        $resources->gold = 0;
        $resources->souls = 0;
        $resources->save();
    }
}

EDIT: RegisterController

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
}

Upvotes: 0

Views: 1498

Answers (1)

nakov
nakov

Reputation: 14288

In your RegisterController you can override the registered method which is called right after the user has been successfully registered:

protected function registered( Request $request, $user )
{
    // call your service here.
}

And Inject it in the constructor of the controller not of the trait.

Also from your service I noticed this:

$resources = new $this->resource;

// change it to this:

$resource = new Resource;

and remove the dependency injection as it does not make sense I believe.

Upvotes: 2

Related Questions