I've been all over the web and struggling with this for around 2 hours.
I have a USER model, a RUN model and a TIME model.
In the real world the user is in a race and gets their time entered in the database along with a USER_id and a RUN_id.
A user should only be able to have one row in the TIMES table for each RUN_id - if that makes sense!
Is this something I need to manage at the controller level? Or is there a relationship I can setup to ensure that a duplicate entry of this style can not enter the database?
Database structure at present:
USERS:
name
RUNS:
name
TIMES:
time
user_id
run_id
The Models:
USER:
public function times()
{
return $this->hasMany(Time::class);
}
RUN:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Run extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function times()
{
return $this->hasMany(Time::class);
}
}
TIME:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Time extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function run()
{
return $this->belongsTo(Run::class);
}
}