繁体   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