I have those schemas:
Schema::create('tickets', function (Blueprint $table) {
$table->increments('id');
$table->integer('ticket_reason_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->timestamps();
$table->foreign('ticket_reason_id')->references('id')->on('ticket_reasons')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
Schema::create('ticket_reasons', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
And those defined relationships
class Ticket extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
public function reason()
{
return $this->belongsTo(TicketReason::class, 'ticket_reason_id');
}
}
The "reason" method works but I had to manually add the ID. Why do I have to add the id if I'm following the convention? (or at least I think so). It follows the same convention than the user_id column and it doesnt need the id setter.