如何在不构建自定义工具的情况下实现多对多新星资源

时间:2019-10-15 08:46:24

标签: laravel eloquent laravel-nova

我目前正在建立一个时间表生成系统,下面有这些模型,它们分别是 Subject Teacher 作为具有新星资源的两个主要模型,我创建了一个枢纽模型 SubjectAllocation (具有nova资源),具有枢纽表 subject_allocations ,其中具有字段 teacher_id subject_id 。我希望能够使用 SubjectAllocation 新星资源来选择一位老师,并为该老师分配多个科目,但是目前,我并不缺此。尝试引入此软件包 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'),
    ];
}

enter image description here

关于如何使它工作或提供更好的解决方案的任何建议,将不胜感激

1 个答案:

答案 0 :(得分:0)

没有构建自定义工具,请使用以下方法:

// 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
                        );
                    }
                })
        ];
    }