Laravel-datatables: Sorting and serching by morphTo relation model.

Created on 31 May 2016  路  9Comments  路  Source: yajra/laravel-datatables

Summary of problem or feature request

How i can write backend code on the Laravel 5 with using morpTo relations model for search and sorting by relation model.

I have the transactions table and Transaction model.

Transaction model have morphTo relations with models Subscribe and Order.

Transaction.php

public function targetable() {
    return $this->morphTo();
}

Subscription.php

public function transaction() {
    return $this->morphMany(Transaction::class, 'targetable');
}

I have in Order model "TITLE" column for show in table

Order.php

public function transaction() {
    return $this->morphOne(Transaction::class, 'targetable');
}

I have in Order model "ID" column for show in table

Controller.php

public function datatable() {
    $Transactions = Transaction::with('targetable', 'currency')
->leftjoin('currencies', 'transactions.currency_id', '=', 'currencies.id');
// What am I supposed to write here

    return Datatable::of($Transactions)
->make(true);
// How i can configure column for searching and ordering and show it in table (subscription.title and order.id)
}

I want to search and sorting by fields from morphTo relation model

  • Operating System Ubuntu 14.04
  • PHP Version 5.5.9
  • Laravel Version 5.1
  • Laravel-Datatables Version 6.11.3
not supported

Most helpful comment

is this morphTo feature plan to deploy or in the roadmap?

All 9 comments

@DJZT, I think searching/sorting with morph relationship is not yet supported by the package. But you can still display the values using addColumn method like:

->addColumn('subscription', function($trans) {
    return $trans->targetable->title;
})

@DJZT There is a way via Transformers.

http://datatables.yajrabox.com/eloquent/transformer

HOW TO:
http://fractal.thephpleague.com/transformers/

Then just override the query and order methods.

Can you provide a simple list of columns?

Thanks.

I have two morphTo relations in Transaction model. Target and payer
Transactions table

  • ID increment
  • TARGETABLE_ID integer(11)
  • TARGETABLE_TYPE varchar(1024)
  • PAYERABLE_ID integer(11)
  • PAYERABLE_TYPE varchar(1024)
  • CREATED_AT
  • UPDATED_AT

Payer it User and Guest models

Users table

  • ID increment
  • FIRST_NAME
  • LAST_NAME
  • ... other fields

Guests table

  • ID increment
  • FIRST_NAME
  • LAST_NAME
  • ... other fields

Target it Subscription and Order models

Subscriptions table

  • ID increment
  • ends_at
  • ... other fields

Orders table

  • ID increment
  • USER_ID integer(11)
  • ... other fields

I want get result table wit ordering and serching in Payer and Target columns

| #ID | Pyer | Target | Created At |
| --- | --- | --- | --- |
| 1 | User 1 | 29-05-14 14:01:00 (Subscription ends at) | 29-05-2013 14:01:00 |
| 2 | User 1 | 575 (Order ID) | 30-05-2013 14:01:00 |
| 3 | Guest 2 | 04-06-14 14:01:00 (Order ID) | 04-06-14 14:01:00 |

@ChaosPower As the transformer can help me? I not understood. You can write small example?

@DJZT sorry but I think morphTo relation is not yet supported by the package. If you can, please do not hesitate to submit a PR. Thanks!

is this morphTo feature plan to deploy or in the roadmap?

@yajra is this feature is now supported ??

For anyone else that finds this, this is how you can include morphMany() relationship fields.

My model's morphMany definition (the extra parameters are probably not part of yours, that is fine):

    public function extensions() {
        return $this->morphMany('App\Extension', 'extensionable', null, null, 'directoryid');
    }

Defining the column:

    /**
     * Get columns.
     *
     * @return array
     */
    protected function getColumns()
    {
        return [
            Column::make('username'),
            Column::make('surname'),
            Column::make('given_name'),
            Column::make('job_title'),
            Column::make('department.name')->title('Department'),
            Column::make('beep_extension'),
            Column::make('extensions.extension')->title('Extension(s)'),  //this is the morphMany column
        ];
    }

Edit the column:

    /**
     * Build DataTable class.
     *
     * @param mixed $query Results from query() method.
     * @return \Yajra\DataTables\DataTableAbstract
     */
    public function dataTable($query)
    {
        return datatables()
            ->eloquent($query)
            ->editColumn('extensions.extension', function(Person $person) {
                return $person->extensions->pluck('extension')->join(', ');
            });
    }

Make sure the first parameter in the editColumn matches the value defined in the Column::make() call.

I also added an example on how to filter these columns on this issue:

https://github.com/yajra/laravel-datatables/issues/2291#issuecomment-580469287

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ezekel picture ezekel  路  17Comments

FaZeRs picture FaZeRs  路  18Comments

ruchisheth picture ruchisheth  路  16Comments

Arkhas picture Arkhas  路  15Comments

ZAZmaster picture ZAZmaster  路  15Comments