Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is local key in laravel eloquent

http://laravel.com/docs/4.2/eloquent#relationships

what does local key mean in this thing? does it mean primary key of the table? or what? for example in this code

return $this->hasOne('Phone', 'foreign_key');

return $this->hasOne('Phone', 'foreign_key', 'local_key');
like image 933
apache Avatar asked Nov 07 '14 14:11

apache


People also ask

What is eloquent in Laravel?

Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with your database. When using Eloquent, each database table has a corresponding "Model" that is used to interact with that table.

How to use dynamic query scope in Laravel 7/6?

You can easily use dynamic query scope in laravel 7/6 application. we are working model query like get todays records, get active records, get banned users etc. If you create laravel model eloquent scope then you don't have to write same logic with where condition again and again.

What is model in Laravel?

Laravel is an MVC based PHP framework. In MVC architecture, ‘ M ’ stands for ‘ Model ’. A Model is basically a way for querying data to and from the table in the database. Laravel provides a simple way to do that using Eloquent ORM (Object-Relational Mapping). Every table has a Model to interact with the table.

How to populate a table with a few entries in Laravel?

You can now populate the table with a few entries and run: Here you can keep deleting, adding, editing entries while you work, and then reset them with a simple command. CRUD operations under the Eloquent object-relational mapper (ORM) make it easier for Laravel developers to work with multiple databases.


2 Answers

local_key is the primary key of your table. You only need to specify it if your primary key is not called id AND you do not have the $primaryKey property set in your model.

like image 150
zen Avatar answered Oct 04 '22 01:10

zen


I believe everything is written in the doc:

ake note that Eloquent assumes the foreign key of the relationship based on the model name. In this case, Phone model is assumed to use a user_id foreign key. If you wish to override this convention, you may pass a second argument to the hasOne method. Furthermore, you may pass a third argument to the method to specify which local column that should be used for the association:

Which basically means that 'local_key' is the name of the table column in your db which is responsible to match the related entity (phone) with your current entity (user).

If you have a look at the db, I'm sure you'll find a table user with a phone_id column, try to change it to something else (like "phone" only) and your eloquent request will crash. Then change your call to return $this->hasOne('Phone', 'user_id', 'phone'); and this might work again.

like image 42
Thomas Dutrion Avatar answered Oct 03 '22 23:10

Thomas Dutrion