Should I use foreign keys in the database if I use laravel

design-patternslaravelMySQLprogramming practicessql

I'm creating a website with Laravel for the first time. I checked relationships documentation today and it seems that Laravel just uses simple SQL queries.

class User extends Eloquent {

    public function phone()
    {
        return $this->hasOne('Phone');
    }

}

$phone = User::find(1)->phone;

And it's basically a select * from phones where user_id = 1 query. So the question is, should I use foreign keys anyway in my database to create relationships?

Best Answer

Foreign keys in your database enables data integrity, as you can't delete a parent row if there is a child row in another table.

While you can rely on the framework to handle data for you, the framework will not enable data integrity and you will eventually end up with orphan rows in your database.

So, my advice is: design the database properly in order to preserve data.