2016-01-03 46 views
0

我試圖修改我的寄存器(Laravel 5.1內置寄存器),所以我可以插入一個記錄到另一個表。Laravel 5.1 Eloquent插入到另一個表上的寄存器

我的寄存器:

<?php 

namespace Cussion\Http\Controllers\Auth; 

use Cussion\User; 
use Request; 
use Validator; 
use Redirect; 
use Cussion\Http\Controllers\Controller; 
use Illuminate\Foundation\Auth\ThrottlesLogins; 
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers; 

class AuthController extends Controller 
{ 
    /* 
    |-------------------------------------------------------------------------- 
    | Registration & Login Controller 
    |-------------------------------------------------------------------------- 
    | 
    | This controller handles the registration of new users, as well as the 
    | authentication of existing users. By default, this controller uses 
    | a simple trait to add these behaviors. Why don't you explore it? 
    | 
    */ 

    use AuthenticatesAndRegistersUsers, ThrottlesLogins; 

    private $redirectTo = '/dashboard'; 
    private $maxLoginAttempts = 3; 

    /** 
    * Create a new authentication controller instance. 
    * 
    * @return void 
    */ 
    public function __construct() 
    { 
     $this->middleware('guest', ['except' => 'getLogout']); 
    } 

    /** 
    * Get a validator for an incoming registration request. 
    * 
    * @param array $data 
    * @return \Illuminate\Contracts\Validation\Validator 
    */ 
    protected function validator(array $data) 
    { 

     $messages = [ 
      'firstname.required' => '<script>toastr["error"]("Het invullen van het veld is vereist!.", "Registreren mislukt")</script>', 
      'firstname.max' => '<script>toastr["error"]("U mag maar maximaal :max karakters ingeven.", "Registreren mislukt")</script>', 
      'lastname.required' => '<script>toastr["error"]("Het invullen van het veld is vereist!.", "Registreren mislukt")</script>', 
      'lastname.max' => '<script>toastr["error"]("U mag maar maximaal :max karakters ingeven.", "Registreren mislukt")</script>', 
      'email.required' => '<script>toastr["error"]("Het invullen van het veld is vereist!", "Registreren mislukt")</script>', 
      'email.email' => '<script>toastr["error"]("U moet een bestaand e-mail adres opgeven!", "Registreren mislukt")</script>', 
      'email.max' => '<script>toastr["error"]("U mag maar maximaal :max karakters ingeven.", "Registreren mislukt")</script>', 
      'email.unique' => '<script>toastr["error"]("Het opgegeven e-mail adres bestaat al.", "Registreren mislukt")</script>', 
      'password.require' => '<script>toastr["error"]("Het invullen van het veld is vereist!", "Registreren mislukt")</script>', 
      'password.min' => '<script>toastr["error"]("U moet minimaal :min karakters gebruiken voor uw wachtwoord.", "Registreren mislukt")</script>', 
      'password2.require' => '<script>toastr["error"]("Het invullen van het veld is vereist!", "Registreren mislukt")</script>', 
      'password2.same' => '<script>toastr["error"]("De twee wachtwoorden komen niet overeen.", "Registreren mislukt")</script>', 
     ]; 

     $rules = [ 
      'firstname' => 'required|max:255', 
      'lastname' => 'required|max:255', 
      'email' => 'required|email|max:255|unique:users', 
      'password' => 'required|min:4', 
      'password2' => 'required|same:password', 
      ]; 

     return Validator::make($data, $rules, $messages); 
    } 

    /** 
    * Create a new user instance after a valid registration. 
    * 
    * @param array $data 
    * @return User 
    */ 
    protected function create(array $data) 
    { 

     if (User::where('slug', '=', strtolower($data['firstname']).'.'.strtolower($data['lastname']))->exists()) { 

      return User::create([ 
       'firstname' => strtolower(htmlspecialchars($data['firstname'])), 
       'lastname' => strtolower(htmlspecialchars($data['lastname'])), 
       'slug' => strtolower(htmlspecialchars($data['firstname'])).'.'.strtolower(htmlspecialchars($data['lastname'])).'.'.time(), 
       'email' => strtolower($data['email']), 
       'password' => bcrypt($data['password']), 
       'ip_reg' => Request::ip(), 
      ]); 

     }else{ 

      $user = new User; 
      $user->firstname = strtolower($data['firstname']); 
      $user->lastname = strtolower($data['lastname']); 
      $user->slug = strtolower($data['firstname']).'.'.strtolower($data['lastname']); 
      $user->email = strtolower($data['email']); 
      $user->password = bcrypt($data['password']); 
      $user->ip_reg = Request::ip(); 
      $user->userImages()->create(['image' => 'empty-avatar.png']); 
      $user->save(); 

      Session::flash('success', '<script>toastr["success"]("Uw profiel is nu up to date.", "Profiel succesvol bijgewerkt")</script>'); 
      return Redirect::to('dashboard'); 
     } 
    } 

} 

我的模型:

<?php 

namespace Cussion; 

use Illuminate\Database\Eloquent\Model; 

class UserImage extends Model 
{ 
    protected $table = 'users_images'; 

    protected $fillable = ['user_id','image']; 
} 

的user.php的

<?php 

namespace Cussion; 

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 Bican\Roles\Traits\HasRoleAndPermission; 
use Bican\Roles\Contracts\HasRoleAndPermission as HasRoleAndPermissionContract; 

class User extends Model implements AuthenticatableContract, CanResetPasswordContract, HasRoleAndPermissionContract 
{ 
    use Authenticatable, CanResetPassword, HasRoleAndPermission; 

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

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

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


    public static function userImages() 
    { 
     return $this->hasMany('Cussion\UserImage'); 
     //Laravel automatically snake case your model 
     //so it will assume the foreign key is named user_id 
    } 

} 

在我的控制器我有2種獨立的方式來存儲。我不知道該用什麼......請幫助我。所以我在這裏檢查一個用戶slug是否存在,如果是的話,它需要添加time();我實際上想要添加1如果它是第一個和2如果第二個等等。就像你在臉書上一樣,也許如果有人能解決這個問題,那就太好了。

我想在users_images中插入一個新記錄,其中user_id是剛剛註冊的用戶。

這是什麼,表中有值:

id, user_id, image, created_at, updated_at

我acctually只想有USER_ID和填充時間戳,因爲其餘的將去自動的。

如何使用Laravel 5.1來做到這一點?

+0

你知道關於雄辯嗎?創建一個模型,與用戶建立關係並觀看魔術 –

+0

我該怎麼做?你可以給我一些代碼嗎? – Robin

+0

這裏是文檔:https://laravel.com/docs/5.2/eloquent創建雄辯模型users_image並首先了解它的基本 –

回答

1

創建users_images模型(順便說一句,這是一個支點或只是一個一對多的關係?)

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


class UserImage extends model{ 
     protected $table='users_images'; 
     protected $fillable = ['user_id','image']; 

} 

在用戶模型添加

public function userImages() 
{ 
    return $this->hasMany('App\UserImage'); 
    //Laravel automatically snake case your model 
    //so it will assume the foreign key is named user_id 
} 

現在你可以使用的關係創建像這樣的新模型:

$user->userImages()->create(['image'=>whateves]); 
+0

它玩它只需要創建一個新的行與註冊的user_id。我不知道如何在auth系統的商店功能中實現。 – Robin

+0

也許我誤解了你,而不是返回'返回User :: create($ data)'將其保存到一個變量'$ user'中,然後執行上面的代碼,這將創建你的行,如果你不想發送圖像,只需使用create,laravel就會知道它們所屬的時間戳和user_id。 – Cptmaxon

+0

我做了一切你發佈在這裏,但現在我的問題是...我如何需要存儲用戶,我需要返回什麼? – Robin

相關問題