Kyslik / column-sortable

Package for handling column sorting in Laravel 5/6/7/8
MIT License
644 stars 105 forks source link

Sorting relations model #97

Closed bebopstudio closed 6 years ago

bebopstudio commented 6 years ago

Hello @Kyslik ,

I'm using your column sorting package for a few days and it's great, but today I have encountered a problem. I will explain my situation:

I've 3 models (users_detail, users and company) and they are related as follows:

users_detail--------users-------company

In users_detail i've a view with a table where shows some users_detail fields , some users fields and some company fields. So far everything works fine.

Can you help me?

Thanks

Regards

Kyslik commented 6 years ago

Hey @bebopstudio I am glad it works somewhat for you :), I will try to test this scenario out and I will get back to you.

Meanwhile can you share users model? As well as definition of all relations? Also do you have $sortable property in company?

bebopstudio commented 6 years ago

Hi @Kyslik,

Thank you for offering to test my scenario. Now I share users model:

  1. can you share users model?
namespace App;

use Illuminate\Notifications\Notifiable;
use Kyslik\ColumnSortable\Sortable;
class User extends Authenticatable
{
    use Notifiable, Sortable, EntrustUserTrait;

    protected $table = 'usuari';
    protected $primaryKey = 'id';
    protected $dateFormat = 'U';

    public $sortable = ['nom', 'perfil_pgap', 'cognom_primer', 'identificador', 'email'];

    public function company(){
        return $this->belongsTo('App\Company','cccid', 'id');
    }

    public function users_detail()
    {
        return $this->hasMany('App\Users_detail', 'id', 'userid');
    }
  1. definition of all relations? In company:
public function users()
{
    return $this->hasMany('App\User', 'cccid', 'id');
}

In users:

public function company()
{
    return $this->belongsTo('App\company','cccid', 'id');
}

public function users_detail()
{
    return $this->hasMany('App\Users_detail', 'id', 'userid');
}

In users_detail:

public function user()
{
    return $this->belongsTo('App\User','userid', 'id');
}

3. do you have $sortable property in company?
Yes.

```php
public $sortable = ['rao_social', 'nom', 'nif', 'ccc', 'cnae', 'ciutat', 'comunitat_autonoma', 'asepeyo_area', 'asepeyo_sector', 'asepeyo_centre'];
Kyslik commented 6 years ago

This package can sort only two relations belongsTo and hasOne. I prepared example branch for you you can see diff here https://github.com/Kyslik/column-sortable-example/commit/c0958e1d6c291db6f2980c6ef720c3078796ea9e or branch itself https://github.com/Kyslik/column-sortable-example/tree/example-multiple-relations-at-once

Take a close look at models, and UserController.php.

I hope all above will guide you to success.

Kyslik commented 6 years ago

Closing for inactivity, feel free to comment further.