vendredi 28 avril 2017

laravel fetch records from model if record exist in other model

hi i have two models named user and task. An user has many task and aleast one task to alloted to one user. i have already made one to many relationship but when i fetch and show username and no of task each user, it shows those users who has 0 task. My user class

class User extends Model
{
/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'user';

/**
* The database primary key value.
*
* @var string
*/
protected $primaryKey = 'id';

/**
 * Attributes that should be mass-assignable.
 *
 * @var array
 */
protected $fillable = ['name', 'type', 'status', 'punch_time'];

public function tasks()
{
    return $this->hasMany('App\Task');
}
}

My task class

class Task extends Model
{
public $timestamps = false;
/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'task';

/**
* The database primary key value.
*
* @var string
*/
protected $primaryKey = 'tsk_id';

/**
 * Attributes that should be mass-assignable.
 *
 * @var array
 */
protected $fillable = ['user_id', 'name', 'description', 'punch_time',    'status', 'redemption_time'];


public function user()
{
    return $this->belongsTo('App\User', 'id');
}
public function availableTask()
{
    return $this->task()->where('user_id','!=', 0);
}
}

my controller function $users = User::with('availableTask')->paginate($perPage); Thanks in Advance



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2qegsiq
via IFTTT

Aucun commentaire:

Enregistrer un commentaire