Laravel-datatables: ID列の値が間違っています

作成日 2016年12月09日  ·  3コメント  ·  ソース: yajra/laravel-datatables

問題または機能のリクエストの概要

idがJavaScript側から要求されていない場合、 id列の値が間違っています。

この問題は、dataTables設定columnsid列について言及していない場合にのみ発生します。

$('#table').DataTable({
    columns: [
            { data: 'id', name: 'id' }, // ----> Problem appear when i don't mention this column in javascript
            { data: 'user.first_name', name: 'user.first_name' },
            ...
    ]
})

PHP側では、私はこのようなものを持っています

$students = Student::with(['user', ...]);

return Datatables::of($students)
->addColumn('actions', function ($row)
{
    $row->id // ----> Will contain wrong value, it will contain users.id instead of students.id

    return $actions;
})
->make(true);

idの値は、 students.idではなくusers.idます

システムの詳細

  • オペレーティング・システム
  • PHPバージョン:5.6
  • Laravelバージョン5.3.26
  • Laravel-Datatables 6.22.5
documentation question

最も参考になるコメント

これはlaravelフレームワークのバグだと思います。これを修正するための回避策は、ここに記載されているようにクエリにselect('students.*')を追加することです

$students = Student::with(['user', ...])->select('students.*');

return Datatables::of($students)
->addColumn('actions', function ($row)
{
    $row->id // ----> Will contain wrong value, it will contain users.id instead of students.id

    return $actions;
})
->make(true);

全てのコメント3件

これはlaravelフレームワークのバグだと思います。これを修正するための回避策は、ここに記載されているようにクエリにselect('students.*')を追加することです

$students = Student::with(['user', ...])->select('students.*');

return Datatables::of($students)
->addColumn('actions', function ($row)
{
    $row->id // ----> Will contain wrong value, it will contain users.id instead of students.id

    return $actions;
})
->make(true);

おかげで、今は正常に動作しています

@yajra 、あなたの答えは私を助けてくれました、ありがとう。 あなたの例では反対であるはずなので、コメントを削除する方が良いでしょう。 正しい値が含まれます。
ありがとう

このページは役に立ちましたか?
0 / 5 - 0 評価