doq8211 2019-07-03 23:52
浏览 63
已采纳

relatableQuery()用于Laravel Nova中同一模型上的两个资源字段

I have a relationship between work 'days' and projects of different types. So my 'days' record has a reference to my 'projects' table twice because I have two different types of projects called 'Series' and 'Event'.

In my 'days' resource I've created two fields as such:

BelongsTo::make('Series','series',Project::class)->sortable()->nullable(),
BelongsTo::make('Event','event',Project::class)->sortable()->nullable(),

What I'm trying to do is filter the projects by their types so I've created this:

public static function relatableProjects(NovaRequest $request, $query){
    return $query->where('type', 'Series');
}

I've tried making relatableSeries and relatableEvents but they don't work. How can I make this connect to the fields correctly without having to create two separate tables for 'series' and 'events'.

The relatableQuery above winds up filtering both resource fields.

  • 写回答

1条回答 默认 最新

  • douwei2825 2019-07-06 00:31
    关注

    Because relatableQuery() is referencing a relatableModel() (so relatableProjects() references the Project model) I was able to create another model solely for the purpose of helping with this.

    I created an Event model which references the same projects table and then was able to create a relatableEvents() method to use the where() filter query.

    Note: I did have to also create an Event resource which references the Event model since this is how Nova works but was able to hide it from being accessed which you can find more information about here

    See revised BelongsTo fields and new model below:

    Day resource

        /**
         * Build a "relatable" query for the given resource.
         *
         * This query determines which instances of the model may be attached to other resources.
         *
         * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
         * @param  \Illuminate\Database\Eloquent\Builder  $query
         * @return \Illuminate\Database\Eloquent\Builder
         */
        public static function relatableProjects(NovaRequest $request, $query){
            return $query->where('type', 'Series');
        }
    
        /**
         * Build a "relatable" query for the given resource.
         *
         * This query determines which instances of the model may be attached to other resources.
         *
         * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
         * @param  \Illuminate\Database\Eloquent\Builder  $query
         * @return \Illuminate\Database\Eloquent\Builder
         */
        public static function relatableEvents(NovaRequest $request, $query){
            return $query->where('type', 'Event');
        }
    
        /**
         * Get the fields displayed by the resource.
         *
         * @param  \Illuminate\Http\Request  $request
         * @return array
         */
        public function fields(Request $request)
        {
            return [
                ID::make()->hideFromIndex()->hideFromDetail()->hideWhenUpdating(),
                BelongsTo::make('User','user',User::class)->sortable(),
                BelongsTo::make('Budget','budget',Budget::class)->sortable()->nullable(),
                BelongsTo::make('Series','series',Project::class)->sortable()->nullable(),
                BelongsTo::make('Event','event',Event::class)->sortable()->nullable(),
                DateTime::make('Last Updated','updated_at')->hideFromIndex()->readOnly(),
                new Panel('Schedule',$this->schedule()),
                new Panel('Time Entry',$this->timeEntries()),
    
            ];
        }
    

    Event model

    <?php
    
    namespace App;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Event extends Model
    {
        /**
         * The table associated with the model.
         *
         * @var string
         */
        protected $table = 'projects';
    
        protected $casts = [
            'starts_on' => 'date',
            'ends_on' => 'date',
        ];
    
        public function event(){
            return $this->belongsTo('App\Event');
        }
    
        public function project(){
            return $this->belongsTo('App\Project');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作