R
R
Roman2019-02-07 19:08:56
Database
Roman, 2019-02-07 19:08:56

How justified is the use of foreign key in Laravel?

Hello.
We are talking about this construction (in migration):

$table->foreign('user_id')->references('id')->on('users');

that is, in theory, it will work without it, if you add to the model
public function user()
    {
        return $this->belongsTo('App\User');
    }

Yes?
How much is a foreign key really needed?
I have just started learning Laravel - I'm looking for the simplest solutions without unnecessary entities.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2019-02-07
@procode

Will. But sudb will not know that this is a foreign key, so the goodies like "when we delete a section, elements are deleted" will not work. And this is often necessary

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question