简体   繁体   English

想要将一个表的最后插入的ID插入laravel 5中的另一个表

[英]Want to insert one table's last inserted id into another table in laravel 5

Want to insert the latest inserted one table's id into another table. 想要将最新插入的一个表的ID插入另一个表。

Suppose there are two tables transactions and user_transactions . 假设有两个表transactionsuser_transactions Now I want to store data in transaction table and also want to store the last inserted transactions table id into another table user_transactions . 现在,我想将数据存储在transaction表中,并且还想将最后插入的transactionsid存储到另一个表user_transactions Do all part in one function. 发挥全部作用。

Here is my store function code where I want to do the above job. 这是我要执行上述工作的store功能代码。

public function store(Request $request)
{
    $data = new transaction;
    $data->title_id = $request->title_id;
    $data->transaction_type_id = '2';
    $data->amount = $request->amount;
    $data->save();

    $data_of_user_transaction = new user_transaction;
    $data_of_user_transaction->transaction_id = $data->id;
    $data_of_user_transaction->user_id = session('user_info.id');
    $data_of_user_transaction->save();
}

After did the code data is inserted in transactions table but not inserted in user_transactions table. 之后,将代码数据插入transactions表中,但不插入user_transactions表中。

I have got my answer . 我有我的答案。 I didn't use user_transaction model at the top of my code. 我没有在代码顶部使用user_transaction模型。

Just add the below line at the top of my code. 只需在我的代码顶部添加以下行。

use App\user_transaction;

Given store function code is totally okay. 给定store function code完全可以。

Do like this i have Auction Table and Rule table I want Auction to save both tables in same time but I want to store Auction_id in Rule Table Column so i did this and get an appropriate answer... 这样做我有Auction Table和Rule table,我希望Auction同时保存两个表,但是我想将Auction_id存储在Rule Table Column中,所以我这样做并获得了适当的答案...

public function store(Request $request,$id)
{
    $auction = new Auction();
    $auction->products_id= $id;
    $auction->auc_price=$request->input('price');
    $auction->r_price=$request->input('rprice');
    $auction->from=$request->input('date');
    $auction->to=$request->input('todate');
    $auction->auc_status= 1;
    $auction->save();
    $rules= new Rule();

    $rules->auction_id = $auction->id;
    $rules->from=$request->input('from');
    $rules->to=$request->input('to');
    $rules->gap=$request->input('gap');

    $rules->save();

  return back();


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在另一个表中插入最后插入的ID - How to Insert last inserted id in another table 使用Codeigniter将最后插入的记录ID插入另一个表的数组中 - insert the last inserted record id into an array in another table using Codeigniter PHP检索插入的最后一条记录的ID,并将其插入另一个表 - php retrieving id of the last record inserted and insert it in another table Laravel将ID从一个表插入另一个表 - Laravel Insert ID from one table to another 如何获取一个表上的最后一个插入ID,以在另一个表上的另一个插入中使用它? - How to get the last inserted id on a table to use it on another insert on another table? 从一个表中获取最后一个插入ID,然后放入另一个表中 - Grab the last insert id from one table to put into another table 带有 postgresql db 的 Laravel 无法将值插入表并使用雄辩的方式获取最后插入的 id - Laravel with postgresql db cant insert value to table and get last inserted id with eloquent 如何将一张表的所有id插入另一张表 - How to insert all id's from one table to another table 获取Doctrine中最后插入的ID,并将该ID插入其子表 - Get Last Inserted Id in Doctrine and Insert that Id to its child table 同一个查询中另一个表的表的最后插入 ID? - Last inserted ID of a table for another table in the same query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM