Closed phuclh closed 5 years ago
Hi @phuclh, could you provide all code, with models and Nova models, etc...
I am using Nova 2.0.6, here is my code:
My Resource:
class Task extends Resource {
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = TaskModel::class;
public static $with = ['users', 'campaign'];
public static $displayInNavigation = false;
/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'name';
/**
* Get the search result subtitle for the resource.
*
* @return string
*/
public function subtitle()
{
return 'Campaign: ' . $this->campaign->name;
}
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'name',
];
/**
* Only show tasks that was assigned for user.
*
* @param NovaRequest $request
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function indexQuery(NovaRequest $request, $query)
{
if (!auth()->user()->can('view any tasks'))
$query->assigned();
return $query;
}
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
$campaign = \Phuclh\NovaCampaign\Models\Campaign::find($request->viaResourceId);
$users = \App\User::all()->pluck('name', 'id')->toArray();
return [
ID::make()->sortable()->hideFromDetail(),
Select2::make('Members', 'members')
->sortable()
->options($users)
/**
* Documentation
* https://select2.org/configuration/options-api
*/
->configuration([
'placeholder' => __('Choose members who can attend to this task.'),
'allowClear' => true,
'minimumResultsForSearch' => 1,
'multiple' => true,
]),
];
}
/**
* Get the cards available for the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function cards(Request $request)
{
return [];
}
/**
* Get the filters available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function filters(Request $request)
{
return [];
}
/**
* Get the lenses available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function lenses(Request $request)
{
return [];
}
/**
* Get the actions available for the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function actions(Request $request)
{
return [];
}
}
My Model:
class Task extends Model {
protected $with = ['users', 'campaign'];
protected $casts = [
'start_at' => 'date',
'end_at' => 'date'
];
/**
* Get all users who assigned for a given task.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function users()
{
return $this->belongsToMany(User::class)->withPivot('campaign_id');
}
/**
* Get the campaign that this task belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function campaign()
{
return $this->belongsTo(Campaign::class);
}
/**
* Get task group that a given task belongs to.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function taskGroup()
{
return $this->belongsTo(TaskGroup::class);
}
/**
* Get all kanban boards belong to task.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function boards()
{
return $this->hasMany(KanbanBoard::class);
}
/**
* Get articles that were assigned for current writer.
*
* @param $query
* @return mixed
*/
public function scopeAssigned($query)
{
return $query->whereHas('users', function ($builder) {
return $builder->where('user_id', auth()->user()->id);
});
}
}
My Task Observer
class TaskObserver {
/**
* Handle the task "saving" event.
*
* @param Task $task
* @return void
*/
public function saving(Task $task)
{
dd(request()->toArray()); // Both of these statement return null for members
dd($task->toArray()); // Both of these statement return null for members
}
@koss-shtukert update error from console.
@koss-shtukert do you have any ideas?
@phuclh
Try to visit on Wiki MorphToMany relationship and using boot
method in your Model
, not -TaskObserver
In new Release, I'm fix .js issue :)
@phuclh Maybe, you just forgot getter method in your model:
public function getMembersAttribute()
{
return $this->items->pluck('id')->all();
}
@phuclh Any news? Have you solved the issue?
Here is my code:
The input allows selecting multiple users. But when I
dd
the request data in model observer, it returns null.Here is screeshot of the request array: