如何使用一个表单在多个表中保存数据

时间:2017-07-11 10:10:12

标签: laravel

我有两个表:customersaddress一个表单,用于将数据保存到此表中。为此,我做了如下关系:

  

地址模型

public function customer()
{
    return $this->belongsTo(Customer::class);
}
  

客户模式

public function address()
{
    return $this->hasMany(Address::class);
}

如何在表格中保存地址?我已经完成customer我可以保存,更新和删除。我阅读了Eloquent of laravel的DOC,但我并没有真正理解它。

  

这是我的控制器

class CustomerController extends Controller
{
    public function index()
    {
        //        $customer = Customer::all();
        $customer = Customer::with('address')->get();;

        return view('customer.index', compact('customer'));

    }

    public function create(Address $address)
    {
        $address = $address->all();
        return view('customer.create');
    }


    public function store(CustomerRequest $request , Customer $customer)
    {
        $customer = Customer::create($request->all());

        return redirect()->route('customer.index',compact('customer'));
    }


    public function show(Customer $customer)
    {
        return view('customer.show',compact('customer'));
    }


    public function edit(Customer $customer)
    {
        return view('customer.edit', compact('customer'));
    }


    public function update(CustomerRequest $request, $id)
    {
        $customer = Customer::find($id)->update($request->all());
        return redirect()->route('customer.index',compact('customer'));
    }

    public function destroy($id)
    {
        Customer::destroy($id);
        return redirect()->route('customer.index');
    }
}

任何建议将不胜感激!

0 个答案:

没有答案