Support the ongoing development of Laravel.io →
Authentication Database Configuration
Last updated 1 year ago.
0

is 'club_id' a fillable field on the user model?

0

also, see your AuthController below

return User::create([
           'name' => $data['name'],
           'email' => $data['email'],
           'club_id' => $data[’club_id'],

why are the single quotes at the beginning and end of 'club_id', for the value, different? Not sure if this forum is presenting them differently, but maybe take a look at that.

0

rberlin01 said:

also, see your AuthController below

return User::create([
          'name' => $data['name'],
          'email' => $data['email'],
          'club_id' => $data[’club_id'],

why are the single quotes at the beginning and end of 'club_id', for the value, different? Not sure if this forum is presenting them differently, but maybe take a look at that.

Thanks for your reply! I added it wrong in the forum, the single quotes are correct. club_id is a fillable field.

Could it be because having $data['club_id'] expects getting that from a form or something with that id and can't be modified using eloquent?

0

You could just create the user in your controller

$newuser = User::create([
   'name' => $request->input('name'),
   'email' => $request->input('email'),
   'club_id' => Auth::user()->club_id
]);
0

Also, have you done an echo on Auth::user()->club_id to ensure that the logged in user actually has a value set here?

0

rberlin01 said:

Also, have you done an echo on Auth::user()->club_id to ensure that the logged in user actually has a value set here?

Thanks again for your help. I did an echo and it has a valid value set.

I tried 'club_id' => Auth::user()->club_id and it worked great! But I wouldn't like it to be like that since a different role can choose the club_id from a select form. Do you have any other ideas what I could do? Thanks a lot once again!

0

Look for the input, if not there use the user's.


if(isset($request->input('club')){
    $ClubID = $request->input('club');
}else{
    $ClubID = Auth::user()->club_id;
}

$newuser = User::create([
   'name' => $request->input('name'),
   'email' => $request->input('email'),
   'club_id' => $ClubID
]);

Last updated 8 years ago.
0

rberlin01 said:

Look for the input, if not there use the user's.


if(isset($request->input('club')){
   $ClubID = $request->input('club');
}else{
   $ClubID = Auth::user()->club_id;
}

$newuser = User::create([
  'name' => $request->input('name'),
  'email' => $request->input('email'),
  'club_id' => $ClubID
]);

Thanks again for trying to help mate! That's a good idea but I'm getting syntax errors.

First:
FatalErrorException in AuthController.php line 68: Cannot use isset() on the result of a function call (you can use "null !== func()" instead)

Then I changed it to:

if(null !==($request->input('club_id')))
{
            $ClubID = $request->input('club_id');
        }

        else
        {
            $ClubID = Auth::user()->club_id;
        }

Now I'm getting:
ErrorException in AuthController.php line 68: Undefined variable: request

Last updated 8 years ago.
0

What's the route and method declaration look like?

0

rberlin01 said:

What's the route and method declaration look like?

It's the standard one from authcontroller, right now it is:

    protected function validator(array $data)
    {

        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

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

route:

Route::get('user/register', ['middleware' => 'auth', 'uses' => 'Auth\AuthController@getRegister']); 
0

I managed to solve it by adding use Input; at the top of AuthController and this code:


        if(Input::has('club_id'))
        {
            $club_id = $data['club_id'];
        }

        else
        {
            $club_id = Auth::user()->club_id;
        }

Thanks for the idea rberlin01!

Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Bobeta bobeta Joined 5 Sep 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.