[英]how to fetch value from one table and insert into second table while inserting data to the second table as backend function. using laravel 5.2
I have two table, one is account
and other is user
. 我有两个表,一个是account
,另一个是user
。
In account
table accountID
is primary key. 在account
表中, accountID
是主键。
uerID is a column in the 'user' table. uerID是“用户”表中的一列。 When i insert userID
value to user
table ,the accountID
value from the account
table should insert as hidden into the `user' table (column name is accountID itself).Because when we insert userID , we are not giving any option to enter the accountID again. 当我向user
表中插入userID
值时, account
表中的accountID
值应以隐藏的形式插入到“用户”表中(列名称本身就是accountID)。由于我们插入userID时,我们不提供任何输入accountID的选项再次。 It will automatically taken from the 'account' table. 它将自动从“帐户”表中获取。 In account table contain single data. 在帐户表中包含单个数据。
public function userInsert(Request $request)
{
$postUser = Input::all();
$account = Account::select('accountID')->get();
/* $data=DB::table('account')
->select("accountID"); */
//insert data into mysql table
$data = array('accountID'=>$account['accountID'],
'userID'=> $postUser['userID']
);
// echo print_r($data);
$ck = 0;
$ck = DB::table('user')->Insert($data);
//echo "Record Added Successfully!";
$name = DB::table('user')->simplePaginate(10);
return view('user.userAdmin')->with('name', $name);
}
above am giving the insertion function for creating new userID 以上是给插入功能创建新的用户ID
How to write sql query in laravel 5.2 to do this ? 如何在laravel 5.2中编写sql查询以执行此操作?
Can anyone please help me responses are appreciable..! 任何人都可以帮助我,答复是明显的..!
From what I understand, you want to add the accountID
to the user
table. 据我了解,您想将accountID
添加到user
表中。 You should be able to insert the accountID
by simple just adding it to the array... 您应该能够通过简单地将accountID
添加到数组中来插入该accountID
。
$postUser = Input::all();
$account = Account::select('accountID')->get();
// Add the value of the accountID to the post array
$data = array("accountID" => $account['accountID'],
"userID"=> $postUser['userID']
);
// echo print_r($data);
$ck = 0;
$ck = DB::table('user')->Insert($data);
//echo "Record Added Successfully!";
$name = DB::table('user')->simplePaginate(10);
return view('user.userAdmin')->with('name', $name);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.