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
@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
Payer it User and Guest models
Users table
Guests table
Target it Subscription and Order models
Subscriptions table
Orders table
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
Most helpful comment
is this morphTo feature plan to deploy or in the roadmap?