Need help with the tutorial #1336
Replies: 2 comments
-
I'm glad to hear you're finding Backpack Pro and DevTools helpful! Let's address your question about managing relationships between devices and people. For your many-to-many relationship between devices and people with a pivot table (user_devices), you'll need to set up a few things to show the latest assigned person on your devices list. Setting up the relationshipFirst, make sure your models are properly set up with the relationship:
public function users()
{
return $this->belongsToMany(\App\Models\User::class, 'user_devices')
->withPivot('created_at')
->withTimestamps();
}
public function latestUser()
{
return $this->belongsToMany(\App\Models\User::class, 'user_devices')
->withPivot('created_at')
->withTimestamps()
->orderByPivot('created_at', 'desc')
->limit(1);
}
CRUD::column([
'name' => 'latestUser',
'type' => 'relationship',
'label' => 'Latest User',
'entity' => 'latestUser',
'attribute' => 'name', // or whatever attribute you want to display
]); Managing the relationshipFor editing the relationship, you can use the CRUD::field([
'name' => 'users',
'type' => 'relationship',
'label' => 'Users',
'entity' => 'users',
'attribute' => 'name',
'pivot' => true,
]); If you need to save additional data to the pivot table, you can use subfields as described in the CRUD Fields documentation: CRUD::field([
'name' => 'users',
'type' => 'relationship',
'label' => 'Users',
'entity' => 'users',
'attribute' => 'name',
'pivot' => true,
'subfields' => [
[
'name' => 'created_at',
'type' => 'datetime',
'wrapper' => ['class' => 'form-group col-md-6'],
],
// Add other pivot fields if needed
],
]); Additional ResourcesFor more detailed guidance on relationships in Backpack:
If you're still having trouble, you might want to check out the StackOverflow tag "backpack-for-laravel" where you can post specific questions about your implementation. ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️ Relevant sources: Github Issue (open): Remove friction from PAID user conversion (and experience) |
Beta Was this translation helpful? Give feedback.
-
Hey @JohnHamagami The AI Bot is right. Basically, you are looking for more info on using pivot table. I don't have exact idea what exactly you are trying to build. But to help you out, here are some related documentation links:
Let me know, if you have any other query - Please mark answered and close if you find things solved. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Just purchased Backpack Pro and DevTools and I am super amazed at how it was able to do something that took me two weeks in about 5 min. But I am struggling understanding a few things... Specially because the documentation is different than what I thought was a default install that would allow me to follow along with the video tutorial. my biggest hurdle is understanding the relationship part. I want to manage many devices for many people so I have a 3rd table called user_devices and the user id and the device_id .. on my devices controller, I would like to call the latest assigned person to the device and show that on my list.. but I am not sure how to do that. Anyway can point me to instructions on how to do that?
Beta Was this translation helpful? Give feedback.
All reactions