0

I am working on one to many relationship between tasks and Projects, i.e. a task can only belong to a single project, and I have used laravel's Accessor to get the selected project in my view in drop down:

my code is as follow:

public function getAssignUserAttribute($value)
{
   dd($value); // gives me null
    // if $value have id of user I want to get that user from db
}

and my view contains the drop down is:

{!! Form::select('assign_user', $assign_user, null, ['class' => 'form-control select2', 'id' => 'assign_user']) !!}

I have accessed all the users from database into TasksController to the view as:

$assign_user = User::pluck('title', 'id');
return view('tasks.edit', compact('task', 'assign_user'));

But I get all users selected, while I only want to have the selected user in my drop down.

Can someone guide me to the right path.

Thank you

Filip Koblański
  • 9,718
  • 4
  • 31
  • 36
Kashif Ullah
  • 672
  • 6
  • 19

1 Answers1

2

finally I solved the issue by myself I have edit the Accessor as follow:

public function getAssignUserAttribute()
{
    return [0 => $this->attributes['assign_user'] ];        
}

Since I required an array so I assigned the current user to the array index 0 and return, in that case the view selected the returned user in drop down :)

This may help someone :)

Filip Koblański
  • 9,718
  • 4
  • 31
  • 36
Kashif Ullah
  • 672
  • 6
  • 19