我有2张桌子
#something - id, name, url
#something_users - id, id_something, email, password
我的模特
class Something extends Eloquent
{
protected $table = 'something';
protected $fillable = ['name', 'email', 'password'];
public $errors;
public function User()
{
return $this->belongsTo('User', 'id', 'id_something');
}
}
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
protected $table = 'something_users';
protected $hidden = array('password', 'remember_token');
public function Something()
{
return $this->belongsTo('Something');
}
}
控制器
$input = Input::all();
// also some validation
$this->db->fill($input);
$this->db->password = Hash::make(Input::get('password'));
$this->db->push();
$this->db->save();
SQL
insert into `something` (`name`, `email`, `password`) values...
我需要在第一个表(某物)和电子邮件中输入名称,将密码插入第二个(something_users)
怎么做?我对此有所了解。
答案 0 :(得分:10)
你的关系有点搞砸了,你可能想要改变它们。请注意hasMany()
与belongsTo()
的对比。如果something
只能有一个user
,您可能希望将功能从hasOne()
更改为hasMany()
,将功能名称更改为user()
这种方式更有意义。
class Something extends Eloquent {
protected $table = 'something';
public function users()
{
return $this->hasMany('User', 'id_something');
}
}
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
protected $table = 'something_users';
protected $hidden = array('password', 'remember_token');
public function something()
{
return $this->belongsTo('Something', 'id_something');
}
}
然后要保存something
和user
并将它们关联起来,这很容易。
$something = new Something;
$something->name = Input::get('name');
$something->url = Input::get('url');
$something->save();
$user = new User;
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));
$something->users()->save($user);
我没有看到您的构造函数,因此我不知道$this->db
引用了哪个模型,但您可能希望替换somethings
或users
,具体取决于你有什么。为了保持依赖注入,我建议将依赖项命名为实际内容。
class SomeController extends BaseController {
public function __construct(User $user, Something $something)
{
$this->user = $user;
$this->something = $something;
}
public function someFunction()
{
$this->something->name = Input::get('name');
$this->something->url = Input::get('url');
$this->something->save();
$this->user->email = Input::get('email');
$this->user->password = Hash::make(Input::get('password'));
$this->something->users()->save($this->user);
}
}
答案 1 :(得分:0)
Laravel 6。
我想使用一个名为RegisterController的控制器向用户表和客户表添加数据。
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\Customer;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = RouteServiceProvider::HOME;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
//this part is my code
protected function create(array $data)
{
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'role' => '2',
]);
$customer = Customer::create([
'user_id' => $user['id'],
'firstname' => $data['name'],
'lastname' => $data['name'],
'email' => $data['email'],
'address' => '',
'phone' => '',
'gender' => '',
]);
return $user;
}
}
此答案不是最佳答案,因为此答案只是快捷方式代码,因此我的代码没有错误。将来可能还会出现另一个错误。 但我希望我的回答能解决您的问题
答案 2 :(得分:-1)
我有类似的问题。 我有2个表并保存这些数据
用户表:
id, name, username, password, role
成员表:
member_id, member_ic, member_contact, addressType_id, address, postcode, state, user_id
我可以将这些数据分别添加到数据库中,但不知道如何将它们添加到一起,如示例所示......
除此之外,数据仅在角色= 3时保存 我正在使用https://github.com/bestmomo/laravel5-example
中的示例框架任何人都可以指导我,谢谢你。