Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
Isn't user_id a foreign key? –  JeffO Jan 5 at 22:21

1 Answer 1

Using the exact same words from @JeffO

"Isn't user_id a foreign key?"

I think the problem is not necessarilly setting a foreign key or not. The problem is how much do you need to depend on the database features.

On Laravel you will set your foreign keys while adding your migrations:

http://laravel.com/docs/4.2/schema#foreign-keys

But if you for any reason/error have user having direct access to the database or using php features without the framework or whatever other issue the might not only rely on the framework, it is a best practice to add your database foreign keys.

Finally on the other hand, I already worked on projects where they decided not to have foreign keys and rely only on the frameworks validations for maintenance reasons.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.