gpt4 book ai didi

php laravel 如何插入外键

转载 作者:行者123 更新时间:2023-11-29 12:54:28 24 4
gpt4 key购买 nike

我有两个表:

管理员:

ID | username | password | mobileNumber | firstName | lastName

餐厅:

website | adminID | name

我有一个包含此数据的 html 表单

restaurantName
website
username
password
mobileNumber
firstName
lastName

当我提交该表单时,我想将数据插入到管理表中,然后将具有 adminID 的数据插入到餐厅表中:

我尝试过这个:

$input = Input::all();
$admin = Admin::create(Input::only('username', 'password', 'mobileNumber', 'firstName', 'lastName'));

$data = [
'name' ,
Input::get('restaurantName'),
'website' => Input::get('website')
];

$restaurant = new Restaurant($data);
$admin->restaurant()->save($restaurant);

但是我遇到了这个异常:

BadMethodCallException
Call to undefined method Illuminate\Database\Query\Builder::save()

你能帮忙吗?

管理模型

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');
}

最佳答案

尝试:

$restaurant->admin()->associate($admin);
$restaurant->save();

关于php laravel 如何插入外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24284164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com