0
    $coin = DB::table('coins')->updateOrInsert(['name' => "ethereum", 'symbol' => "dsafd",'image' => "adfadsfasdfasdfasdfd"],
    ['circulated_supply'=>"123123",'max_supply'=>"123123"]);

DB::table('coin_prices')->Insert(['coin_id'=> here,'price_change'=>]
);

i wanna get coin_id(pk) in table 'coins' but i dont know how to do. i already searched in stackoverflow a lot but i couldn't find code that i want.

thanks
  • 1
  • 1
  • can you give more details? so we can helps you better – Harsh Patel Aug 24 '21 at 12:43
  • Does this answer your question? [Get the Last Inserted Id Using Laravel Eloquent](https://stackoverflow.com/questions/21084833/get-the-last-inserted-id-using-laravel-eloquent) – ADyson Aug 24 '21 at 13:26
  • If you can only use the QueryBuilder instead of using Eloquent, you can use `DB::getPdo()->lastInsertId();` but only if it was an insert. In case of an update you'll have to rely on querying the table with the same conditions to fetch its id. This because updateOrInsert returns a boolean value, so it's useless to determine the id. I suggest using the query with the same conditions, since it works for any situation. Is anything stopping you from making a Coin model and using Eloquent's updateOrCreate? The latter returns the model, including the id regardless of whether you updated or inserted. – wblommaert Aug 24 '21 at 14:22

0 Answers0