我正在构建一个时间表生成系统,下面有这些模型,它们分别是Subject和Teacher作为具有新星资源的两个主要模型,我创建了一个枢轴模型SubjectAllocation (具有nova资源),其数据透视表subject_allocations的字段为[[teacher_id和subject_id。我希望能够使用SubjectAllocation nova资源来选择一位老师,并为该老师分配多个科目,但是目前,我并不缺少它。尝试拉入此软件包dillingham / nova-attach-many以附加到SubjectAllocation模型,并且此软件包选择教师记录sloveniangooner / searchable-select,但无法在数据透视图中存储数据表格。
主题分配资源<?php
namespace App\Nova;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\BelongsToMany;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Http\Requests\NovaRequest;
use NovaAttachMany\AttachMany;
use Sloveniangooner\SearchableSelect\SearchableSelect;
class SubjectAllocation extends Resource
{
/**
* The model the resource corresponds to.
*
* @var string
*/
public static $model = 'App\SubjectAllocation';
/**
* The single value that should be used to represent the resource when being displayed.
*
* @var string
*/
public static $title = 'id';
/**
* The columns that should be searched.
*
* @var array
*/
public static $search = [
'id',
];
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
ID::make()->sortable(),
SearchableSelect::make('Teacher', 'teacher_id')->resource("teachers"),
AttachMany::make('Subjects')
->showCounts()
->help('<b>Tip: </b> Select subjects to be allocated to the teacher'),
];
}
/**
* 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 [];
}
}
主题资源中的字段方法
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Subject Name', 'name')
->withMeta(['extraAttributes' => ['placeholder' => 'Subject Name']])
->sortable()
->creationRules('required', 'max:255', 'unique:subjects,name')
->updateRules('required', 'max:255'),
Text::make('Subject Code', 'code')
->withMeta(['extraAttributes' => ['placeholder' => 'Subject Code']])
->sortable()
->creationRules('required', 'max:255', 'unique:subjects,code')
->updateRules('required', 'max:255')
,
Textarea::make('Description')
->nullable(),
BelongsToMany::make('Teachers'),
];
}
教师资源中的字段方法
public function fields(Request $request) { return [ ID::make()->sortable(), BelongsTo::make('User') ->searchable(), Text::make('First Name', 'first_name') ->withMeta(['extraAttributes' => ['placeholder' => 'First Name']]) ->sortable() ->rules('required', 'max:50'), Text::make('Middle Name', 'middle_name') ->withMeta(['extraAttributes' => ['placeholder' => 'Middle Name']]) ->sortable() ->nullable() ->rules('max:50'), Text::make('Last Name', 'last_name') ->withMeta(['extraAttributes' => ['placeholder' => 'Last Name']]) ->sortable() ->rules('required', 'max:50'), Text::make('Teacher Code', 'teacher_code') ->withMeta(['exraAttributes' => [ 'placeholder' => 'Teacher Code']]) ->sortable() ->creationRules('required', 'max:50', 'unique:teachers,teacher_code') ->updateRules('required', 'max:50'), BelongsToMany::make('Subjects'), ]; }
关于如何使它工作或提供更好解决方案的任何建议,将不胜感激
// app\Nova\SubjectAllocation.php
public function fields(Request $request)
{
return [
ID::make()->sortable(),
// SearchableSelect::make('Teacher', 'teacher_id')->resource("teachers"),
SearchableSelect::make('Teacher', 'teacher_id')->resource(\App\Nova\Teacher::class)
->displayUsingLabels(),
AttachMany::make('Subjects','subject_id')
->showCounts()
->help('<b>Tip: </b> Select subjects to be allocated to the teacher')
->fillUsing(function($request, $model, $attribute, $requestAttribute) {
$a = json_decode($request->subject_id, true);
$teacher = \App\Teacher::find($request->teacher_id);
if(count($a)==0){
// Error processing because no subject is choosen
}else if(count($a)==1){
$model['subject_id'] = $a[0];
}else{
$model['subject_id'] = $a[0];
array_shift ($a); // Remove $a[0] in $a
$teacher->subjects()->sync(
$a
);
}
})
];
}