Ruka Xing
Ruka Xing

Reputation: 642

Laravel datatable relations error print

Problem is how to print(show) Topic's name in View. and i tried this topic->topic_name still got error.

Model of Vote

protected $fillable = [
    'topic_id' ,'question', 'answer', 'lastname', 'firstname', 'identity', 'user_id',
];
public function topic(){
    return $this->belongsTo('App\Topic');
}

Model of Topic

protected $fillable = [
    'topic_name',
];
public function votes(){
    return $this->hasMany('App\Vote');
}

APIController@getColumnSearchData

$customers = Vote::select(['id', 'topic_id', 'question', 'answer','lastname','firstname','identity', 'user_id', 'created_at']);
return Datatables::of($customers)->make(true);

View

processing: true,
    serverSide: true,
    ajax: '{{ route('api.column_search') }}',
    columns: [
      { data: 'id', name: 'id' },
      { data: 'topic_name', name: 'topic_name' },    there is error
      ... ...,
    ],

Upvotes: 1

Views: 143

Answers (1)

Jinal Somaiya
Jinal Somaiya

Reputation: 1971

try this:

// controller file:

$customers = Vote::with('topic')->get();
return Datatables::of($customers)->make(true);

//view:

processing: true,
serverSide: true,
ajax: '{{ route('api.column_search') }}',
columns: [
  { data: 'id', name: 'id' },
  { data: 'topic.topic_name', name: 'topic.topic_name' },    there is error
  ... ...,
],

Upvotes: 1

Related Questions