【问题标题】:How to implement many to many nova resource without building custom tool如何在不构建自定义工具的情况下实现多对多 nova 资源
【发布时间】:2020-02-11 21:33:09
【问题描述】:

我目前正在构建一个时间表生成系统,我有这些模型,下面是 SubjectTeacher 作为两个主要模型及其 nova 资源,我创建了一个透视模型 SubjectAllocation(有一个 nova 资源),带有一个带有 teacher_idsubject_id 字段的透视表 subject_allocations。我希望能够使用 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'),




    ];
}

教师资源中的Fields方法

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

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

【问题讨论】:

    标签: laravel eloquent laravel-nova


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      • 2022-10-19
      • 1970-01-01
      • 2019-04-21
      • 1970-01-01
      相关资源
      最近更新 更多