【问题标题】:Run where()->get() on Nova Action fields()在 Nova Action fields() 上运行 where()->get()
【发布时间】:2019-10-25 09:13:45
【问题描述】:

我正在尝试提供一个 Select 列表,其中仅包含通过数据透视表与模型相关的记录。

在为客户构建时间跟踪器/预算软件时,我正在使用两个模型,称为预算和项目,它们通过数据透视表连接在一起。 (所以budgetsprojectsbudget_project

我正在尝试在Select 字段上显示与选定的Budget(调用操作时来自Budget 资源)相关的所有项目。我不知道如何将model->id 传递给字段函数。然后我将运行一些代码来分析与给定Budget 关联的Projects,并创建一堆跨越日期范围和其他关系的记录。

请帮忙!

我正在寻找这样的东西......

class CreateSchedule extends Action
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /**
     * Perform the action on the given models.
     *
     * @param  \Laravel\Nova\Fields\ActionFields  $fields
     * @param  \Illuminate\Support\Collection  $models
     * @return mixed
     */
    public function handle(ActionFields $fields, Collection $models)
    {
        return Action::message('all good');
    }

    /**
     * Get the fields available on the action.
     *
     * @return array
     */
    public function fields()
    {
        $budgetProject = BudgetProject::where('budget_id',$this->id)->get();

        foreach($budgetProject as $bp){
            $projectArray[$bp->project_id] = $bp->project->name;
        }

        return [
            Select::make('Project','project_id')->options($projectArray),
        ];
    }
}

【问题讨论】:

    标签: php laravel crud laravel-nova


    【解决方案1】:

    对于任何与 Nova 抗争的人,我必须做一些非常相似的事情。 正如其他人建议的那样,您需要执行以下步骤。

    1. 在注册操作时,将 Action 类中所需的任何内容作为参数传递。 ( new MyAction($this->myParam) )
    2. 在您的操作类中创建一个构造函数并接受参数。
    protected $receivedParam;
    
    public function __construct($param)
    {
       $this->receivedParam = $param;
    }
    

    然后你可以在你的 fields() 中使用你的参数来做任何你需要做的事情。

    从镜头发起的操作的注意事项

    但是,在从 Lens 启动动作时,这将不起作用,因为您在执行 ( new MyAction($this->someParam) ) 时不会简单地获取参数。在 Lens 的上下文中,你需要先击败弹出的 Std Object,然后你需要从 $this 中挖掘资源及其属性。

    // pass entire $this here and filter down in the Action Class
    return [(new GiveMediaAccountAccessViaLens($this))]; // registering action from Lens
    
    // Action Class
        protected $receivedParam;
    
        public function __construct($receivedParam)
        {
            $this->receivedParam = $receivedParam; // $receivedParam is $this from MediaAccountFilterByClient Lens
        }
    
    
        public function fields()
        {
            if(!$this->receivedParam->resource instanceof Model)
            {
                // If you are trying to generate a dropdown/select then you should set your options to
                // empty array. $options = [];
                $options = [];
            }
            else
            {
                // Here, $this->receivedParam->resource contains the Model Object, by calling
                // getRawOriginal() on it, You will get the Original Attributes array which contains
                // key value pair, like "some Attribute => "value"
                // $this->receivedParam->resource->getRawOriginal()['someAttributeOnModel']
                
                // Finally in case you are making a dropdown, you can your data to the array that
                // will be passed to Select field.
                $options = Arr::add($options, "value", "label");
                
            }
    
            // finally return the fields with custom filled Dropdown
            return [
                // key will be used as key to get the field value in handle()
                Select::make('Label', 'key')->options($options),
            ];
        }
    

    对于我的用例,我最终使用了两个动​​作类(一个用于从镜头注册动作时使用,另一个用于从资源类注册动作,因为我正在生成自定义下拉列表。 Nova 做了一些奇怪的事情,我在从 Resource 执行操作时得到了一个无效的 foreach 参数。

    【讨论】:

      【解决方案2】:

      当我在拥有TemplateCampaign 模型的新闻通讯部分工作时,我遇到了类似的问题

      如果您想获取记录的数据,您可以通过这样做来添加您的模型 请注意,OneTabeRow 函数是一个内联操作,必须传递模型

      public function actions(Request $request)
      {
          return [
              (new CreateCampaign($this))
                  ->onlyOnTableRow()
          ];
      }
      

      现在您可以在 CreateCampaign 行动承包商中接收它们

      public function __construct($model)
      {
          $this->template = $model;
      }
      

      并且无需向数据库发出任何请求,您就可以像这样获取当前记录数据

      public function fields()
      {
          return [
              Text::make('Name', 'name')
                  ->default(function ($request) {
                      return $this->template->name;
                  })
                  ->rules('required'),
              Text::make('Subject', 'subject')
                  ->default(function ($request) {
                      return $this->template->subject;
                  })->rules('required'),
              Textarea::make('Content', 'content')
                  ->default(function ($request) {
                      return $this->template->content;
                  })->rules('required')->showOnIndex(),
              Hidden::make('Template Id', 'template_id')
                  ->default(function ($request) {
                      return $this->template->id;
                  })
          ];
      }
      

      这是我在第一条记录中单击内联操作Create Campaign 后想要的照片,我得到一个表单,其中包含我想在操作表单中显示的特定记录

      【讨论】:

        【解决方案3】:

        对我来说是这样的

        在 Resource 类中传递 id

         (new LogsDiffAction($this->id))
        

        在Action类中创建一个构造函数来接收这个参数

         protected $model;
        
        public function __construct($model)
        {
            $this->model = $model;
        }
        

        在你可以做的领域

        if ($this->model) {
                $entity = Activity::find($this->model);
                $model = json_decode($entity->properties, true);
                $partial = view('admin.nova.log-diff-partial', ['model' => $model])->toHtml();
        
                return [
                    Heading::make($partial)->asHtml(),
                ];
            }
        

        【讨论】:

          【解决方案4】:
          public function fields(Request $request)
          {
              $budgetProject = BudgetProject::where('budget_id',$request->budget_id)->get();
          
              dd($budgetProject);
          
              foreach($budgetProject as $bp){
                  $projectArray[$bp->project_id] = $bp->project->name;
              }
          
              return [
                  Select::make('Project','project_id')->options($projectArray),
              ];
          }
          

          【讨论】:

          • ErrorException"Declaration of App\Nova\Actions\CreateSchedule::fields(App\Nova\Actions\Request $request) should be compatible with Laravel\Nova\Actions\Action::fields()"
          猜你喜欢
          • 2020-05-03
          • 2019-11-15
          • 2021-01-10
          • 2022-11-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-13
          • 1970-01-01
          相关资源
          最近更新 更多