2017-07-18 10 views
0

私はthis repoを私のプロジェクトの出発点として使用しています。Laravel 5.4関連データを取得して表示する

私はuserテーブルとclubsテーブルを持っています。 Userは1つのクラブを持つことができ、にはclubが割り当てられます。

したがって、hasOneの関係をclubsテーブルに定義しました。

public function user() 
{ 
    return $this->hasOne(User::class, 'id', 'user_id'); 
} 

clubsテーブルにはuser_id列があります。

私のindexページで正しいデータが得られず、UseruserNameが表示されました。

私はClubsRepositoryを使用してデータを表示しています。

以下はそのコードです。以下のリポジトリで

public function __invoke(ManageClubRequest $request) 
{ 
    return Datatables::of($this->clubs->getForDataTable()) 
     ->escapeColumns(['clubName', 'sort']) 
     ->addColumn('user', function ($clubs) { 
      return $clubs->user->pluck('first_name'); 
     }) 
     ->addColumn('actions', function ($clubs) { 
      return $clubs->action_buttons; 
     }) 
     ->withTrashed() 
     ->make(true); 
} 

public function getForDataTable() 
{ 
    return $this->query() 
     ->with('user') 
     ->select([ 
      'clubs.id', 
      'clubs.clubName', 
      'clubs.description', 
      'clubs.user_id', 
      'clubs.created_at', 
      'clubs.updated_at', 
     ]); 
} 

私の 'getFromDataTable()' メソッドである私のindex.bladeコードが

<table id="clubs-table" class="table"> 
    <tr> 
     <th>clubName</th> 
     <th>description</th> 
     <th>userName</th> 
     <th>created</th> 
     <th>last_updated</th>   
    </tr> 
</table> 

<script> 
    $(function() { 
     $('#clubs-table').DataTable({ 
      dom: 'lfrtip', 
      processing: false, 
      serverSide: true, 
      autoWidth: false, 
      ajax: { 
       url: '{{ route("admin.access.clubs.get") }}', 
       type: 'post', 
       data: {status: 1, trashed: false}, 
       error: function (xhr, err) { 
        if (err === 'parsererror') 
         console.log(err); 
       } 
      }, 
      columns: [ 
       {data: 'clubName', name: 'clubs.clubName'}, 
       {data: 'description', name: 'clubs.description'}, 
       {data: 'user', name: 'user.first_name', sortable:false}, 
       {data: 'created_at', name: 'clubs.created_at'}, 
       {data: 'updated_at', name: 'clubs.updated_at'} 
      ], 
     }); 
    }); 
</script> 

を下回っている私はここで間違って何をしているのですか?クラブテーブルは、それがユーザーをbelongs to意味USER_ID列を、持っている場合、ない -

+0

@AaronFaheyはそれを試みましたが、それは動作しませんでした。 –

答えて

0

することで、関係の間違った種類を使用しているように見えます

User::with(array('clubs' => function($q) { 
    }))->get(); 
0

ユーザーズ自分のクラブとのデータを取得するには、この文字列で検索してくださいそれはhas oneユーザーです。

複数のクラブを持つことができ、各ユーザーと仮定すると、何が欲しいのは

// in the User model 
public function clubs() 
{ 
    return $this->hasMany(Club::class); 
} 

// in the Clubs model 
public function user() 
{ 
    return $this->belongsTo(User::class); 
} 

はまた、私はDataTableのに慣れていないんだが、注目すべき一つのことは、あなたが実際に実行されないということですgetForDataTable()メソッドのクエリ->get()などがありません。 $this->query()があなたがやろうとしていることを行う最も効率的な方法のようには見えないという問題もありますが、確かに言うのは難しいです。

そして、この行で:

return Datatables::of($this->clubs->getForDataTable()) 

は、複数のクラブの集まりのクラブはありますか?これは、オブジェクトのコレクションに対してモデルメソッドを呼び出すことができないためです。

+0

私はこれを試して、今度はすべてのユーザーを 'first_name'という配列で表示しています。 –

+0

もう少し更新しました。うまくいかない場合は、 '$ this-> clubs 'とは何か、さらにあなたは何を呼び出そうとしていますか? –

関連する問題