samedi 30 janvier 2016

Laravel 5.2 -> fill up tables on registration

i have 2 accounts, business\company user and private user, they share some attributes as email,password,name. but there is a need to store more things about the businesses. so i have two tables.

main table: users company: company is connected through Foreign Key(user_ID). So far i was not able to write anything into company table :/ any advice ?

business model:

class Company extends Model
{
        protected $fillable = ['ICO'];
        public function user() {
        return $this->belongsTo('App\User');
}}

user model:

class User extends Authenticatable {
    protected $fillable = [
        'firstname','middlename','lastname', 'email', 'password','usertype',];
    protected $hidden = ['password', 'remember_token',];
    public function company(){
    return $this->hasMany('App\Company');
    }
}

migration-company

public function up() {
        Schema::create('companyuser', function (Blueprint $table) {   
        $table->integer('user_id')->unsigned()->nullable();
        $table->integer('ICO');
        $table->timestamps();
        $table->foreign('user_id')->references('id')->on('users')
              ->onDelete('cascade');    
        });
    }

AuthController.php

   $user = User::create([
                        'firstname' => $data['firstname'],
                        'lastname' => $data['lastname'],
                        'middlename' => $data['middlename'],
                        'usertype' => $data['usertype'],
                        'email' => $data['email'],
                        'password' => bcrypt($data['password']),
            ]);
    //no problem with this upper part but with this part on bottom.
     $company = Company::create([
               'ICO' => $data[ICO],
           ]);



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire