laravel fetch records from model if record exist in other model

ساخت وبلاگ

Vote count: 0

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('AppTask');
}
}

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('AppUser', 'id');
}
public function availableTask()
{ return $this->task()->where('user_id','!=', 0);
}
}

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

asked 39 secs ago

back soft...
ما را در سایت back soft دنبال می کنید

برچسب : نویسنده : استخدام کار backsoft بازدید : 338 تاريخ : جمعه 8 ارديبهشت 1396 ساعت: 20:28