duancong6937 2016-08-12 21:58
浏览 34
已采纳

Laravel多对多关系5表

I have a model called Members, another called Awards and another called Events.

Each member can have multiple awards more than once and each award can be assigned to multiple members, these can be awarded on the members profile or as a result of an event.

My member model relationship:

public function Awards()
    {
        return $this->belongsToMany('App\Award', 'award_members', 'member_id', 'award_id')
                    ->withTimestamps()
                    ->orderBy('award_members.created_at','desc');
    }

My relations table:

Schema::create('award_members', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('club_id');
            $table->integer('award_id');
            $table->integer('member_id');
            $table->integer('event_id')->nullable();
            $table->timestamps();
        });

My view output:

@foreach($member->Awards as $a)
  {{ $a->title }} | {{ $a->pivot->created_at }}
@endforeach

This outputs:

  • Award 1 | 12/08/2016
  • Award 2 | 12/08/2016
  • Award 1 | 11/08/2016

I'd also like to output if stored in the relations table the event name if it was added to the members profile via an event. the above only shows details of awards when added directly to the member.

In my relations table an entry from an event would look similar to:

  • id = 4
  • user_id = 1 (related to user table)
  • club_id = 1 (related to club table)
  • award_id = 3 (related to awards table)
  • member_id = 1 (related to members table)
  • event_id = 1 (related to events table but can be NULL)

How can I output when entered the event name?

  • 写回答

1条回答 默认 最新

  • douweng9427 2016-08-16 14:27
    关注

    I guess when you have so complex a pivot, maybe it warrants being a model itself. In fact if you think about it, your Award is a type of award, which can be obtained many times in different circumstances. An award being granted to someone, at some moment, through some event, ... that's a different concept.

    Say you are developing a system for a library. You would typically have a model for a book (title, author, etc.) and another one for "BookCopy" or "BookExemplar" which represents each physical copy of it. When a user takes a book, they really are taking the copy.

    So I would say

    // Member.php
    public function Awards()
    {
        return $this->hasMany(AwardGrant::class);
    }
    

    // AwardGrant.php
    public function Award()
    {
        return $this->belongsTo(Award::class);
    }
    
    public function Event()
    {
        return $this->belongsTo(Event::class);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了