mercredi 11 mai 2016

How to get a specific column from the joined table in HasMany relationship in Laravel Eloquent

My Model looks like this

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class appraisaltask extends Model
{
    //
     protected $table = 'empappraisaltask';

    /*
     * An invoice can has many payments 
     *
     */
     public function ratings(){

        return $this->hasMany('App\appraisalrating','empappraisaltask_id')->select(array('comment', 'rating'));
    }
}

and I am doing a query in my function like this

 public function getUserbasictask(){

        $taskwithcomments = appraisaltask::select(array('id','taskname','description','status'))->with(  array('ratings' => function($query)
          {
             // the condition that will be apply on the with relation
              $query->where('emp_id','=',Auth::user()->empid);

          }))->where('type','=','basic')->get();

            return json_encode($taskwithcomments);
    }

But I am getting Empty Rating object . ANy suggestion how to do that

If I remove the select() from the rating function in the model I get all the details

Any help would be appreciated



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire