我有两个表:customers
和address
一个表单,用于将数据保存到此表中。为此,我做了如下关系:
地址模型
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');
}
}
任何建议将不胜感激!