我試圖修改我的寄存器(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
來做到這一點?
你知道關於雄辯嗎?創建一個模型,與用戶建立關係並觀看魔術 –
我該怎麼做?你可以給我一些代碼嗎? – Robin
這裏是文檔:https://laravel.com/docs/5.2/eloquent創建雄辯模型users_image並首先了解它的基本 –