Relationships
Learn how to add relationships in your CRUD
Using the CRUD generator, you can create relationships between tables. At the bottom of the page you will see the "Create Relationships" button,
Notice
First you will need to create a CRUD, and then you can add the relationship. Otherwise, you will see the message below.
So, after the CRUD is already created, you can create new relationships. After clicking the button "Create a link". A new Modal window appears, which looks like this:
Now it supports the relations belongsTo
, belongsToMany
, hasOne
and hasMany
.
You can also create a child datatable list for the relationship:
As a result, you will see:
You can set a custom attribute for the allow custom attribute for belongsTo relationship:
{
"attribute": "account"
}
Alternative relationships
With the help of additional CRUD constructors, you can add relationships to the rows. There are 2 types of input that allow you to link to another table.
- Dropdown
- Multiple Select
Dropdown
A drop-down list can create a belongsTo
relationship from the current DataType to another table. Say, for example, that we had the line category_id
in the table
Quite simply, in the CRUD articles
, we could select Select Dropdown as the input type of the line category_id
and include the following: Optional Details:
{
"relationship": {
"key": "id",
"label": "title"
}
}
The key is the column that we want to use as the value in the select drop-down list, and label is the column that we want to display in the drop-down list.
Finally, we need to create a relationship in the Category
class. It will look like this:
public function categoryId(){
return $this->belongsTo(Category::class);
}
Notice
The method used for this relationship must match the camelCase string from the table
categories
. That's why we usedcategoryId
as the method name to bind the relationship.
By default Admin Panel will use the Category::all() method to populate the dropdown. However you can provide you own list by defining the categoryIdList()
on your Category
class:
public function categoryIdList(){
return Category::where('active', 1)->orderBy('position')->get();
}
Updated less than a minute ago