2021年3月20日星期六

Laravel Eloquent - auto-numbering on has many relationship

I'm very much a beginner when it comes to database relationships hence what I suspect is a basic question! I have two database tables as follows:

Projects

  • id
  • company_id
  • name
  • etc...

rfis

  • id
  • project_id (foreign key is id on the Projects table above)
  • Number (this is the column I need help with - more below)
  • question

The relationships at the Model level for these tables are as follows:

Project

public function rfi()  {      return $this->hasMany('App\Rfi');   }  

RFI

public function project()  {      return $this->belongsTo('App\Project');  }  

What I'm trying to achieve In the RFI table I need a system generated number or essentially a count of RFI's. Where I'm finding the difficulty is that I need the RFI number/count to start again for each project. To clarify, please see the RFI table below which I have manually created with the the 'number' how I would like it displayed (notice it resets for each new project and the count starts from there).

enter image description here

Any assistance would be much appreciated! Todd

https://stackoverflow.com/questions/66728267/laravel-eloquent-auto-numbering-on-has-many-relationship March 21, 2021 at 10:18AM

没有评论:

发表评论