11 months ago

Laravel how to put Restaurant id to menu table restaurant_id

Posted 11 months ago by Twittler

Hi guys, i'm having problem taking id from restaurants table and putting it to menu table Restaurant_id.

Restaurant model:

class Restaurant extends Model {

protected $table = 'restaurant';

protected $fillable = ['name, address, delivery_method, payment_method'];

public function user() {

    return $this ->belongsTo('App\User', 'foreign_key');

public function menu() {

    return $this -> hasMany(Menu::class);


Menu model:

class Menu extends Model { protected $table = 'menu';

protected $fillable = ['restaurant_id', 'name', 'ingredients', 'price'];

    public function menu() {

    return $this -> belongsTo(Restaurant::class);


Menu controller :

public function PostMenu(Request $request) {

    $name = $request['name'];

    $menu = new Menu();
    $menu -> name = $name;
    $menu -> restaurant_id = auth()->menu()->id;

    $menu -> save();

    return redirect() -> back();


And if i'm using $menu -> restaurant_id = auth()->user()->id; it just takes id from user table.

And if i'm just removing $menu -> restaurant_id = auth()->menu()->id; this line laravel says that restaurant_id is missing default value

Please sign in or create an account to participate in this conversation.