Support the ongoing development of Laravel.io →
Eloquent Forms Validation
Last updated 1 year ago.
0

What does your User model look like?

Last updated 8 years ago.
0

thomastkim said:

What does your user's model look like?

Edited the main post to add the User Model. I've tried it with and without the fields for this form.

0

Hm...I can't see anything that would cause that error. That is very weird.

  1. It looks like you are updating an existing profile. Just to be sure though, does the profile already exist or are you updating one?

  2. Just to be safe, can you dd the $user and $input to see what you get?

0

thomastkim said:

  1. It looks like you are updating an existing profile. Just to be sure though, does the profile already exist or are you updating one?

I am updating an existing profile. It already exists AND I'm updating it. When the user registers, it creates an (almost) blank profile for them, registering their user_id for reference back to the user. The User and Profile are two separate tables.

thomastkim said:

  1. Just to be safe, can you dd the $user and $input to see what you get?

This is the dd of the $user:

User {#238 ▼
  #table: "users"
  #fillable: array:13 [▼
    0 => "name"
    1 => "email"
    2 => "password"
    3 => "business_name"
    4 => "address"
    5 => "city"
    6 => "state"
    7 => "area_code"
    8 => "work_number"
    9 => "cell_number"
    10 => "twitter_username"
    11 => "facebook_url"
    12 => "bio"
  ]
  #hidden: array:2 [▼
    0 => "password"
    1 => "remember_token"
  ]
  #dates: array:2 [▶]
  #connection: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:15 [▶]
  #original: array:15 [▶]
  #relations: []
  #visible: []
  #appends: []
  #guarded: array:1 [▶]
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: false
}

And this is the dd of the $input:

array:10 [▼
  "business_name" => "TheLightMedia, LLC"
  "address" => "<This Was My Address>"
  "city" => "<This Was My City>"
  "state" => "<This Was My State>"
  "area_code" => "<This Was My Area Code>"
  "work_number" => "<This Was My Number>"
  "cell_number" => "<This Was My Number>"
  "twitter_username" => "TheLightMedia"
  "facebook_url" => "http://facebook.com/TheLightMediaLLC";
  "bio" => "Shining a Light on your business."
]

THIS, however, is a dd of the Profile Model:

Profile {#239 ▼
  #connection: null
  #table: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:15 [▶]
  #original: array:15 [▶]
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #fillable: []
  #guarded: array:1 [▶]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: false
}

It is not showing any fields as fillable. Why is it not? Did I not use $fillable properly in my model?

Last updated 8 years ago.
0

Can you dd($user->profile)?

0

thomastkim said:

Can you dd($user->profile)?

I ran:

dd($user->profile);

And this is the result:

Profile {#241 ▼
  #connection: null
  #table: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:15 [▶]
  #original: array:15 [▶]
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #fillable: []
  #guarded: array:1 [▶]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: false
}
Last updated 8 years ago.
0

According to that, you haven't set fillable fields for the model. Are you sure you namespaced everything correctly?

Also, I just noticed this, but your Profile model have certain things that you don't need.

use App\User;
use Authenticatable, Authorizable;

Another odd thing is that both the Profile model and the User model have the same fields. Is there a reason for that?

0

I namespaced the User and Profile Models to the App.

namespace App;

I added the extra fields to the User model just in case it needed it, though it normally shouldn't.

As far as the App\User and Authenticatable, Authorizable goes, those were last ditch efforts before bothering the kind people (like you) here. I have removed them.

0

I have no idea what just happened. I was messing around with it, and it just started working again. I have only touched the elements we looked at above, and here they are again. User Model(the 'use' and namespace part were not changed, just including all this time):

namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
use Laravel\Cashier\Contracts\Billable as BillableInterface;
use Laravel\Cashier\Billable as BillableTrait;
use App\Profile;

class User extends Model implements AuthenticatableContract,
                                    AuthorizableContract,
                                    CanResetPasswordContract,
                                    BillableInterface
{
    use Authenticatable, Authorizable, CanResetPassword, BillableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password'];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    protected $dates = ['trial_ends_at', 'subscription_ends_at'];

    public function profile()
    {
        return $this->hasOne('App\Profile');
    }
}

Profile Model:

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\User;

class Profile extends Model
{

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'profiles';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['business_name', 'address', 'city', 'state', 'area_code', 'work_number', 'cell_number', 'twitter_username', 'facebook_url', 'bio'];


    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

Update() function:

public function update($username)
    {
        $user = User::whereName($username)->firstOrFail();
        $input = Input::only('business_name', 'address', 'city', 'state', 'area_code', 'work_number', 'cell_number', 'twitter_username', 'facebook_url', 'bio');
        $user->profile->fill($input)->save();
        return redirect('/profile/' . $user->name)->with('flash_message', 'Profile Saved!');
    }

Can you spot the difference? I cant...

0

Awesome! I don't see a difference. Lol. I thought it was weird how earlier, the output for the Profile instance said null for table and empty for fillable. That didn't make any sense, but at least, it's working now!

0

You know what's curious thomastkim? It just started doing it again! No idea why this time either.

The only difference I see is that this time the 'guarded' attribute (when i dd($user->profile)) is:

  #guarded: array:1 [▼
    0 => "*"
  ]

It's saying its set to all. So I did dd($user->profile->isGuarded('business_name')); and it returns true. EVERYTHING IS RANDOMLY SET TO GUARDED. Laravel apparently doesn't care that I've set attributes available in $fillable.

Last updated 8 years ago.
0

+1 same issue

#guarded: array:1 [▼ 0 => "*" ]

0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.