簡體   English   中英

如何從一個表中獲取值並插入到第二個表中,同時如何將數據作為后端函數插入第二個表中。 使用laravel 5.2

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM