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

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.

相关问题 从表中获取值并在laravel中的同一控制器中更新第二个表 - fetch value from table and update second table in same controller in laravel 无法在laravel雄辩的联接查询中从第二个表中获取数据 - Not able to fetch data from the second table on join query in laravel eloquent 如何将数据从第一个表插入第二个表,就像这个例子 - how to insert the data from the first table to the second table to be like this example 如何在Laravel 5.2中验证数据插入表 - how to validate data insert to table in Laravel 5.2 如何修复将数据插入第二张表 - How to fix the inserting data into the second table 在一对多关系中从第二个表中获取记录会在 Laravel 5.2 中产生错误 - Fetching records from second table in one to many relationship produce error in laravel 5.2 如何将另一个表数据插入另一个表laravel 5.2? - How to insert another table data into another table laravel 5.2? 将数据插入 Laravel 5.2 中的数据透视表 - Inserting data in to a pivot table in Laravel 5.2 如何从一个表中获取数据并插入到php中的另一个表中 - how to fetch data from one table and insert into another table in php 使用选择选项Laravel 5.2插入新记录时,将数据添加到数据透视表 - Adding data to a pivot table while inserting a new record using select option Laravel 5.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM