代码之家  ›  专栏  ›  技术社区  ›  Lahiru Lanka Rathnayaka

将多个表id传递给视图laravel

  •  0
  • Lahiru Lanka Rathnayaka  · 技术社区  · 6 年前

    所以这是我的问题。我正在尝试为两个表创建两个条目customer和booking。其中,customer id是预订表中的外键,是customer表中的主键。 我想做的是将新生成的客户id和预订id传递给同一控制器中的另一个函数(编辑函数),这样我就可以用生成的客户id更新预订表列。有没有简单的方法来完成这项工作。

            $booking =new Booking;
            $customer=new Customer;
            $booking->piklocation=$request->input('piklocation');
            $booking->date=$request->input('date');
            $customer=new Customer;
            $booking->save();
            $customer->save();
            return redirect()->route('booking.edit', $booking->id,$customer->id);
        }
    
    
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id,$id2)
    {
        //Validation of the edit code
        $booking = Booking::find($id);
        $customer=Customer::find($id2);
        return view('pages.vehicleselect')->with('booking',$booking)->with('customer',$customer);
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   atf.sgf    6 年前

    你应该用雄辩的关系 您可以根据表关系使用此文档 [mant to many relation DOC]

    并使用此关系获取客户的预订:

    Customer::with('booking')->find($customer_id);
    

    *预订是客户模型中的关系名称