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,

1117

πŸ“˜

Notice

First you will need to create a CRUD, and then you can add the relationship. Otherwise, you will see the message below.

600

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:

1196

Now it supports the relations belongsTo, belongsToMany, hasOne and hasMany.

You can also create a child datatable list for the relationship:

928

As a result, you will see:

943

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 used categoryId 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();
}

What’s Next