![](/img/trans.png)
[英]fetch value from table and update second table in same controller in laravel
[英]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
我有兩個表,一個是account
,另一個是user
。
在account
表中, accountID
是主鍵。
uerID是“用戶”表中的一列。 當我向user
表中插入userID
值時, account
表中的accountID
值應以隱藏的形式插入到“用戶”表中(列名稱本身就是accountID)。由於我們插入userID時,我們不提供任何輸入accountID的選項再次。 它將自動從“帳戶”表中獲取。 在帳戶表中包含單個數據。
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);
}
以上是給插入功能創建新的用戶ID
如何在laravel 5.2中編寫sql查詢以執行此操作?
任何人都可以幫助我,答復是明顯的..!
據我了解,您想將accountID
添加到user
表中。 您應該能夠通過簡單地將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.