laravel插入具有来自另一个表的键和空列的新行 [英] laravel insert a new row that has a key from another table and a null columns
问题描述
这是我的餐厅桌子:
餐厅表具有adminID
字段.这是管理表:
the restaurant table has a adminID
field . and this is the admin table:
我有一个既具有餐厅又具有管理员值的表单,
I have a form that has the values of both the restaurant and the admin,
这是管理员模型:
class Admin extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
public function restaurant(){
return $this->belongsTo('Restaurant', 'ID');
}
这是餐厅模型:
class Restaurant extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
public function admin(){
return $this->hasOne('Admin', 'adminID');
}
这是用于存储新餐厅的RestaurantsController
This is the RestaurantsController for storing a new restaurant
$input = Input::all();
$validation = Validator::make($input, Admin::$rules);
if($validation->passes()){
$validation = Validator::make($input, Restaurant::$rules);
if ($validation->passes())
{
$admin = Admin::create(Input::only('username', 'password', 'mobileNumber', 'firstName', 'lastName'));
如您所见,我有$admin
变量
我需要插入一个新餐厅的statmnet
what is the statmnet that I need to insert a new restaurant
- 请注意,我可以从$ admin-> id中获取adminID
- 请注意,餐厅的空列如徽标和addressManualID
推荐答案
首先,您不需要实现UserInterface
和RemindableInterface
,也不需要使用use UserTrait, RemindableTrait
创建所有模型但如果您具有用于用户身份验证的User
模型(登录/注销),则仅使用User
模型.如果使用Admin
模型替代User
模型,则您需要实现这些接口,并且还需要使用这些特征.另外,您还需要显式声明protected $table
属性,以分配用于Admin
模型的表名(如果未使用User
模型),并且还需要更改app/config/auth.php
文件中的某些设置,例如如下所示:
At first, you don't need to implement UserInterface
and RemindableInterface
and and also don't use use UserTrait, RemindableTrait
to create all of your models but only the User
model if you have a User
model for user authentication (log in/out). If the Admin
model is being used as a replacement of the User
model then you need to implement those interfaces and also need to use those traits as well. Also you need to explicitly declare the protected $table
property to assign the table name which is being used for Admin
model (If you are not using User
model) and also you need to change some settings in app/config/auth.php
file as given below:
'model' => 'Admin',
'table' => 'admins', // Could be anything else, same as protected $table property
然后在创建Admin
时尝试执行以下操作以保存相关模型:
Then try following to save the the related model when creating Admin
:
//...
if ($validation->passes()) {
$admin = Admin::create( Input::only('username', ...) );
$restaurent = new Restaurant(Input::only(...));
$admin->restaurant()->save($restaurent);
}
这篇关于laravel插入具有来自另一个表的键和空列的新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!