Ad
How To Get Inserted Id Of Pivot When I Use Attach() In Laravel 5.6 (Eloquent)?
I have this tables: users, programs and users_programs (pivot)
I use this sentence to relate users with programs (Many to Many)
$user->programs()->attach($program);
I want to get id to pivot table users_programs, it's posible? How I return id when save a row?
Thanks
Ad
Answer
Pivot tables don't have an increment id field in the realm of Eloquent
.
If you want to get a specific program for a user in the pivot table, you can use something like the following approach as an example:
$user->programs()->attach($program->id);
$user = $user->fresh();
$userProgram = $user->programs->keyBy('program_id')->get($program->id);
Ad
source: stackoverflow.com
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
Ad