fetch associated name of the ID from different table using laravel eloquent relation for chart js <p>This is the code i am using to extract data in my controller which i am using it in my chartjs:</p> <pre><code>$startdate = request('startdate') ?: \Carbon\Carbon::now()-&gt;startOfYear(); $enddate = request('enddate') ?: \Carbon\Carbon::now(); return complaintRegistrationModel::with('complaintmoderelation') -&gt;select(DB::raw(DB::raw('count(*) as complaints')), DB::raw('modeID')) -&gt;whereBetween('updated_at', array($startdate, $enddate)) -&gt;groupBy('modeID') -&gt;pluck('modeID', 'complaints'); </code></pre> <p>As you can see, I am using foreign key <code>modeID</code> from table <code>tblcomplaintregistration</code> ...what i want to do is i want to get the <code>modeName</code> associated with the <code>modeID</code> which is stored in another table call <code>tblmode</code>. So my chart should give <code>modeName</code> in my x-axis instead of <code>modeID</code>. Please help me out. Thank you</p> <p><strong>UPDATE</strong></p> <p>ok i was able to solve it by making some changes in the query like this. Hope some might find it useful.</p> <pre><code>return \App\complaintRegistrationModel::select(\Illuminate\Support\Facades\DB::raw(DB::raw('count(*) as complaints')), \Illuminate\Support\Facades\DB::raw('(select modeName from cr_pltblcomplaintmode where complaintmodeID = modeID) as modeName')) -&gt;whereBetween('updated_at', array($startdate, $enddate)) -&gt;groupBy('modeName') -&gt;pluck('complaints', 'modeName'); </code></pre>