简体   繁体   中英

php laravel how to insert a foreign key

I have two tables which are:

Admin:

ID | username | password | mobileNumber | firstName | lastName

Restaurant:

website | adminID | name

I have an html form that has this data

restaurantName
website
username
password
mobileNumber
firstName
lastName

When I submit that form I want to insert the data to the admin table, then insert the data with the adminID to the restaurant table:

I tried this:

$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);

But I got this exception:

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

could you help please?

Admin model

class Admin  extends Eloquent implements UserInterface, RemindableInterface {

use UserTrait, RemindableTrait;

public function restaurant(){
    return $this->belongsTo('Restaurant', 'ID');
}

Restaurant model

class Restaurant extends Eloquent implements UserInterface, RemindableInterface {

use UserTrait, RemindableTrait;

public function admin(){
    return $this->hasOne('Admin', 'adminID');
}

Try with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM