简体   繁体   中英

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 .

In account table accountID is primary key.

uerID is a column in the 'user' table. 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. 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

How to write sql query in laravel 5.2 to do this ?

Can anyone please help me responses are appreciable..!

From what I understand, you want to add the accountID to the user table. You should be able to insert the accountID by simple just adding it to the array...

$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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM