[英]Add data into more than 1 table from Controller in Laravel
I have two table donor
and address
. 我有两个表
donor
和address
。
I want to insert all the basic details in donor
table and address into address
table with donor id. 我要在
donor
表和地址中插入所有基本信息,并输入施主ID的address
表。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Donor;
class DonorController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('donor.index');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$donor = new Donor;
$donor->salutation = $request->input('salutation');
$donor->gender = $request->input('gender');
$donor->first_name = $request->input('first_name');
$donor->last_name = $request->input('last_name');
$donor->phone = $request->input('phone_home');
$donor->mobile = $request->input('phone_mobile');
$donor->email = $request->input('email');
$donor->occupation = $request->input('occupation');
$donor->is_active = $request->input('status');
$donor->is_deleted = 0;
$donor->created_by = 1;
$donor->updated_by = 1;
$donor->save();
return redirect('/donor')->with('success', 'Hurray!! New donor Created.');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
How to enter details into address
table. 如何在
address
表中输入详细信息。
If a donor has an address, and the address is a unique item which has its own table, you should create a model for Address, and use the eloquent relationships to connect these two together. 如果捐助者有一个地址,并且该地址是一个具有自己表的唯一项,则应为地址创建一个模型,并使用雄辩的关系将这两个关系联系在一起。
You can read more about eloquent relationships here 您可以在这里阅读更多关于雄辩关系的信息
So in your Donor model, you would go something like this: 因此,在您的Donor模型中,您将执行以下操作:
public function address(){
return $this->hasOne('App\Address');
}
And in your Address class: 在您的Address类中:
public function donor(){
$this->belongsTo('App\Donor');
}
So, in your donor controller, you'll create a new instance of address, and connect it to the donor: 因此,在您的捐助方控制器中,您将创建一个新的地址实例,并将其连接到捐助方:
$address = new App\Address;
... Your stuff to populate the address
$donor->address()->save($address);
Note: this is not the entire code to achieve what you want, and I wrote it from memory. 注意:这并不是实现所需代码的全部代码,我是从内存中编写的。 You should do some research to figure it out.
您应该进行一些研究以找出答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.