How To Only Return Collections Where no other relationship use App\User;
use App\Task;
use App\Join;
public function tasks(Request $request)
{
$user = auth::user();
$query = Task::query();
$query->with('User')->with('task_type');
$q = $query->paginate($tasksPerPage);
foreach($q as $task) {
$status = Join::where('user_id',$user->id)->where('task_id',$task->id)->first();
if($status['user_id'] == $user->id){
$task->current_status = false;
}
else{
$task->current_status = true;
}
}
return $q;
How return only task where not found record in other relation Join model ?
or return only records with current_status true and pagination
Should be no need for a join
if task belongs to user, its a simple where statement
$tasks = Task::where('user_id',Auth::id())
->where('current_status',true)
->with('task_type')
->paginate($tasksPerPage);
@SNAPEY - i need return where task is not belongs to user and where no record in other model Join, how?
Task does not belong to this user (!= on where condition) or does not belong to any user (where user_id is null)?
@SNAPEY - yes where null in Join model and where task not contains auth user->id
where auth user_id != task user_id and where null in join model with auth user_id
use App\User;
use App\Task;
use App\Join;
public function tasks(Request $request)
{
$user = auth::user();
//get all tasks which does not belong to the user
$query = Task::query()->where('user_id', '!=', $user->id)
->with('User')
->with('task_type')
->paginate($taskPerPage);
//get all Join which does not belong to user
$taskIds = Task::query()->pluck('id');
$joins = Join::whereIn('task_id', $taskIds)->where('user_id', '!=', $user->id)->paginate($taskPerPage);
}
@MUSHOOD - thx, but i need return tasks not join, only need check if it null there in join...
I still dont know what you want to achieve
$tasks = Task::where('user_id', '!=', Auth::id())
->where('current_status',true)
->with('task_type')
->paginate($tasksPerPage);
returns all tasks owned by someone else, or owned by nobody
@SNAPEY - i need return tasks where owned by someone else, and user not join...
i can't do where ('current_status') because this attribute is empty when i get tasks
i make
foreach($q as $task) {
$status = Join::where('user_id',$user->id)->where('task_id',$task->id)->first();
if($status['user_id'] == $user->id){
$task->current_status = false;
}
else{
$task->current_status = true;
}
then attribute is true or false...
So current_status is an attribute of your model and not a column on the database?
Is there an accessor on your model for current_status?
@PALAK27 - i need query and where in relationship with other model join
i have model task and join
when user join it record (task_id,user_id)
need return free to join tasks
@SNAPEY - app
this only attribute of model.
Task.php
protected $attributes = ['current_status' => 0];
I thought that it could just check attributes.
because i need return query with relation auth->user->id
also i do something like this in model
Task.php
public function joined()
{
return $this->hasManyThrough('App\Join', 'App\Task', 'id', 'task_id', 'id', 'id');
}
Join.php
public function user_join()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
can i return query task where joined() has not auth user ?
return $query->with(['joined' => function ($query) {
return $query
->with(['user_join']);
}]);
like that i get joined users, but i can't understand how use this relationship with where and not equal auth->user->id
current_status is always zero? I don't then understand why it needs to be in your query.
I explained how to get tasks that can be assigned to users. What is it that you don't understand?
Why do you insist that you need a join?
@SNAPEY - current_status always zero yes, i using this for my view template...
i don't understand how to make query right
how only return tasks of unauthorized user and where there are no records in another table join
maybe like that but it not working...
$query->WhereHas('joined','===','null')->paginate(30);
Solution is
Task.php
public function join()
{
return $this->hasOne('App\Join','task_id');
}
TaskController.php
$user = auth::user();
$query->where('user_id', '!=', Auth::id())
->WhereDoesntHave('join', function (Builder $query) use($user){
$query->where('user_id', 'like', $user->id);
});
What? using like to check IDs
What are you thinking?
You have a relationship called join ?
Please sign in or create an account to participate in this conversation.