Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search not working on relations field with same table. #2054

Closed
sagadsalem opened this issue Apr 11, 2019 · 8 comments
Closed

Search not working on relations field with same table. #2054

sagadsalem opened this issue Apr 11, 2019 · 8 comments

Comments

@sagadsalem
Copy link

I have datatable that work perfectly when is search on relations field that I had edit it but today when I make some search test on datatable its never search on this field

Code snippet of problem

$('#movements-datatable').DataTable({
            processing: true,
            serverSide: true,
            "pagingType": "first_last_numbers",
            "order": [[0, "desc"]],
            ajax: '{{route("movements.datatable")}}',
            columns: [
                {"data": "id", "name": "id", orderable: true, searchable: true},
                {"data": "item_id", "name": "item_id", orderable: true, searchable: true},
                {"data": "unit", "name": "unit", orderable: true, searchable: false},
                {"data": "quantity", "name": "quantity", orderable: true, searchable: false},
                {"data": "price", "name": "price", orderable: true, searchable: false},
                {"data": "in_location", "name": "in_location", orderable: true, searchable: true},
                {"data": "out_location", "name": "out_location", orderable: true, searchable: true},
                {"data": "assigned", "name": "assigned", orderable: true, searchable: true},
                {"data": "moved_by", "name": "moved_by", orderable: true, searchable: true},
                {"data": "created_at", "name": "created_at", orderable: true, searchable: false},
                {"data": "action", "name": "action", orderable: false, searchable: false},
            ],
        });


 $movements = Movement::latest();
        return Datatables::of($movements)
            ->addColumn('action', function ($row) {
                $html = '<div class="btn-group">';
                $html .= "<button type='button' class='btn btn-sm btn-danger' onclick='DeleteMovement({$row->id})'><i class='mdi mdi-delete'></i></button>";
                $html .= "</div>";
                return $html;
            })
            ->editColumn('item_id', function ($row) {
                return $row->item->name;
            })
            ->editColumn('in_location', function ($row) {
                return $row->locationIn->name;
            })
            ->editColumn('out_location', function ($row) {
                return $row->locationOut->name;
            })
            ->editColumn('assigned', function ($row) {
                return $row->assignedTo->name;
            })
            ->editColumn('moved_by', function ($row) {
                return $row->movedBy->name;
            })
            ->editColumn('price', function ($row) {
                return '$' . $row->price;
            })
            ->editColumn('created_at', function ($row) {
                return $row->created_at->toDateTimeString();
            })
            ->removeColumn('updated_at')
            ->make();

  • Operating System
  • PHP 7.3.2
  • Laravel 5.8.11
  • Laravel-Datatables latest version
@yajra
Copy link
Owner

yajra commented Apr 15, 2019

See demo https://datatables.yajrabox.com/eloquent/relationships for ref. You need to define which relationship to load.

$posts = Post::with('user')->select('posts.*');

@yajra yajra added the question label Apr 15, 2019
@sagadsalem
Copy link
Author

sagadsalem commented Apr 15, 2019

But when i add individual column search and I have more than one column have relation with the same table
Here I will have conflict and no result return

@yajra
Copy link
Owner

yajra commented Apr 15, 2019

I see, sorting / searching on same table is not yet supported by the package. We are working on a solution at #2051. Maybe you can try it too and provide some feedbacks? Thanks!

@yajra yajra changed the title Search not working on relations field Search not working on relations field with same table. Apr 15, 2019
@sagadsalem
Copy link
Author

How can I Test it? any example

@yajra
Copy link
Owner

yajra commented Apr 15, 2019

Same as the example on demo. But with codes modified using the version on PR. Thanks!

@yajra
Copy link
Owner

yajra commented Apr 24, 2019

Fixed via #2051.

@yajra yajra closed this as completed Apr 24, 2019
@sagadsalem
Copy link
Author

The Problem Still there
this my columns array in datatable:

         columns: [
                {
                    "class": "details-control",
                    "orderable": false,
                    "data": null,
                    "defaultContent": ""
                },
                {"data": "id", "name": "movements.id", orderable: true, searchable: true},
                {"data": "item.name", "name": "item.name", orderable: true, searchable: true},
                {"data": "unit", "name": "movements.unit", orderable: true, searchable: true},
                {"data": "quantity", "name": "movements.quantity", orderable: true, searchable: true},
                {"data": "price", "name": "movements.price", orderable: true, searchable: true},
                {"data": "total_price", "name": "total_price", orderable: true, searchable: true},
                {"data": "type", "name": "movements.type", orderable: true, searchable: true},
                {"data": "status", "name": "movements.status", orderable: true, searchable: true},
                {"data": "inside.name", "name": "inside.name", orderable: true, searchable: true},
                {"data": "outside.name", "name": "outside.name", orderable: true, searchable: true},
                {
                    "data": "assigned.name",
                    "name": "assigned.name",
                    orderable: true,
                    searchable: true,
                    defaultContent: "no assigned"
                },
                {"data": "moved.name", "name": "moved.name", orderable: true, searchable: true},
                {"data": "created_at", "name": "movements.created_at", orderable: true, searchable: true},
                {"data": "action", "name": "action", orderable: false, searchable: false},
            ],

this the movements modal relation too:

public function item()
    {
        return $this->hasOne(Item::class, 'id', 'item_id');
    }

    public function inside()
    {
        return $this->hasOne(Location::class, 'id', 'in_location');
    }

    public function outside()
    {
        return $this->hasOne(Location::class, 'id', 'out_location');
    }

    public function assigned()
    {
        return $this->hasOne(Location::class, 'id', 'assigned');
    }

and this the datatable query :

 $movements = Movement::with('item', 'inside', 'outside', 'assigned', 'moved')
            ->select('movements.*');

@raphaeldealmeida
Copy link

I have this problema with two relationship on same table

@github-actions github-actions bot locked and limited conversation to collaborators Oct 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants