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');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大