user2208349
user2208349

Reputation: 7709

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

Upvotes: 0

Views: 3097

Answers (1)

clod986
clod986

Reputation: 2647

Try with:

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

Upvotes: 1

Related Questions