Laravel user_id forign key无法正常工作

时间:2016-04-21 17:21:20

标签: foreign-keys migration acl laravel-5.2

我是Laravel的新手,我有多种注册类型,我试图在aravel 5.2文档之后添加一个外键,它一直给我错误任何帮助将不胜感激。我需要将每种类型用户的注册连接到不同的注册类型。下面我将发布我拥有的三种注册类型之一。如果有一种方法只是在验证了电子邮件之后才将用户信息添加到用户表中。换句话说,每次注册(3x)我需要它们来填写注册,只有用户表信息转到用户表,其余的将转到另一个表,但我想要两个连接。

这是我尝试按照laravel 5.2文档开始工作的迁移文件。

 <?php

 use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
   public function up()
 {
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->boolean('active')->default(false);
        $table->rememberToken();
        $table->timestamps();

            $table->foreign('user_id')->references('id')->on('artists');

        $table->foreign('user_id')
            ->references('id')->on('artists')
            //->onUpdate('cascade')
            ->onDelete('cascade');
    });
     }

   /**
      * Reverse the migrations.
    *
    * @return void
      */
   public function down()
   {

    Schema::drop('users');
   //$table->dropForeign('artists_user_id_foreign');
   }
   }

我也尝试在艺术家表上做同样的事情,但它不能继续得到外来约束问题。

我需要为每种类型的用户帐户执行此过程。

对于艺术家

,控制器设置如下
  <?php 

    namespace App\Http\Controllers\Artist;

     use App\User;
    use App\Artist;
     use App\Mailers\AppArtMailer;
      use Illuminate\Http\Request;
      use App\Http\Controllers\Controller;


    class RegistrationController extends Controller
    {
    //protected $redirectTo = 'art';
    /** Create a new registration instance.
     */
     public function __construct()
     {
        //$this->middleware('artist');
     }
     /** show the Register page/
      * 
      * @return \Response
      */
    public function register()
    {
        return view('art.register');
    }
    /**
     * Perform the registration.
     *
     * @param  Request   $request
     * @param  AppMailer $mailer
     * @return \Redirect
     */

    public function postRegister(Request $request, AppArtMailer $mailer)
    {
        //Validate
            $this->validate($request, [
                'name' => 'required',
                'email' => 'required|email|unique:artists',
                'password' => 'required'
            ]);
        //create artist
        $artist = Artist::create($request->all());
        $user = User::create($request->all());

        //email them

        $mailer->sendEmailConfirmationTo($artist);
        //$mailer->sendEmailConfirmationTo($user);
        //flash
        flash('Please confirm your email address.');

        // redirect

        return redirect()->back();

    }
    /**
     * Confirm a user's email address.
     *
     * @param  string $token
     * @return mixed
     */

    public function confirmEmail($token)
    {
        Artist::whereToken($token)->firstOrFail()->confirmEmail();

        flash('You are now confirmed. Please login');

        return redirect('artist');



      }


       }

我也遵循laracast ACL角色和权限,但不确定如何将其集成到每个用户注册中,以便每种类型自动具有设置角色。

以下是我不同的模特,因为我正在学习和玩耍,所以每个模型都有所不同。


艺术家模型这实际上会改变,因为如果他们是观众,他们只能是艺术家。

namespace App; 


use Illuminate\Database\Eloquent\Model; 
use Illuminate\Auth\Authenticatable; 
use Illuminate\Auth\Passwords\CanResetPassword; 
use App\Http\Middleware\RedirectIfAuthenticatedArtist; 
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; 
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; 

class Artist extends Model implements AuthenticatableContract, CanResetPasswordContract 
{ 
protected $table = 'artists'; 


use Authenticatable, CanResetPassword; 

/** 
     * The database table used by the model. 
     * 
     * @var string 
     */ 


    /** 
     * 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', 
  ]; 
 /** 
   * Boot the model. 
     * 
     * @return void 
      */ 



            public static function boot() 
       { 
    parent::boot(); 

    static::creating(function($artist) { 
        $artist->token = str_random(30); 
    }); 
    } 
   /** 
          * Set the password attribute. 
          * 
          * @param string $password 
         */ 

public function setPasswordAttribute($password) 
{ 
    $this->attributes['password'] = bcrypt($password); 
} 
 /** 
        * Confirm the user. 
       * 
         * @return void 
        */ 

public function confirmEmail() 
{ 
    $this->verified = true; 
    $this->token = null; 

    $this->save(); 
} 
    }

这是我的用户模型

<?php 

namespace App; 

use DB; 
use Illuminate\Http\Response; 
use App\Http\Controllers; 
use Illuminate\Database\Eloquent\Model; 
use Illuminate\Support\Facades\Storage; 
use Illuminate\Support\Facades\File; 
use Cmgmyr\Messenger\Traits\Messagable; 
use Illuminate\Foundation\Auth\User as Authenticatable; 

class User extends Authenticatable 
{ 
use HasRoles, Messagable; 

     //* The attributes that are mass assignable. 
     //* 
    // * @var array 
    // */ 
    protected $fillable = [ 
        'name', 'email', 'password', 
    ]; 
    public function setPasswordAttribute($password) 
    { 
        $this->attributes['password'] = bcrypt($password); 
    } 
    /** 
     * The attributes that should be hidden for arrays. 
     * 
     * @var array 
     */ 
    protected $hidden = [ 
        'password', 'remember_token', 
    ]; 

// $user->roles 

} 

赞助商模特

    <?php 

    namespace App; 


    use Illuminate\Database\Eloquent\Model; 
    use Illuminate\Auth\Authenticatable; 
    use Illuminate\Auth\Passwords\CanResetPassword; 
    //use App\Http\Middleware\RedirectIfAuthenticatedSponsor; 
    use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; 
    use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; 

    class Sponsor extends Model implements AuthenticatableContract,             CanResetPasswordContract 
    { 
    protected $table = 'sponsors'; 


    use Authenticatable, CanResetPassword; 

    /** 
       * The database table used by the model. 
      * 
      * @var string 
      */ 


      /** 
        * 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', 
        ]; 
     /** 
         * Boot the model. 
         * 
         * @return void 
         */ 



    public static function boot() 
    { 
    parent::boot(); 

    static::creating(function($sponsor) { 
        $sponsor->token = str_random(30); 
        }); 
    } 
        /** 
       * Set the password attribute. 
         * 
     * @param string $password 
     */ 

    public function setPasswordAttribute($password) 
    { 
        $this->attributes['password'] = bcrypt($password); 
    } 
     /** 
     * Confirm the user. 
     * 
     * @return void 
     */ 

public function confirmEmail() 
{ 
    $this->verified = true; 
    $this->token = null; 

    $this->save(); 
} 
}

观众模型

<?php 

namespace App; 

use Illuminate\Auth\Authenticatable; 
use Cmgmyr\Messenger\Traits\Messagable; 
use Illuminate\Database\Eloquent\Model; 
use Illuminate\Auth\Passwords\CanResetPassword; 
use App\Http\Middleware\RedirectIfAuthenticatedViewer; 
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; 
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; 

class Viewer extends Model implements AuthenticatableContract, CanResetPasswordContract 
{ 
    protected $redirectTo = 'viewer'; 
    use Authenticatable, CanResetPassword, Messagable; 

    /** 
    * The database table used by the model. 
    * 
    * @var string 
   */ 
   protected $table = 'viewers'; 
//protected $table = 'experience'; 
   /** 
     * 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', 
    ]; 

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

    public function awardExperience($points) 
    { 
    return $this->experience->award($points); 
    } 
     /** 
     * Boot the model. 
     * 
     * @return void 
     */ 
    public static function boot() 
    { 
    parent::boot(); 

    static::creating(func

0 个答案:

没有答案