Laravel Eloquent Relationships Tutorial From Scratch
ORM stands for Object-relational mapping. Laravel introduce ORM from Laravel 5 framework version. We used and see the foreign key for database relationship. but in this relation laravel created relationships between model.
As we know database table is almost related to another database table. but when you are working on retrieve data, create data or etc task. you have to use a join or something on every SQL query. So it takes time and also we have to write lots of database query. But in laravel Eloquent Model Relationship we can easily make in relation by using their types.
Why we should use laravel model relationship, So I want to give one example for this. If you have a "users" table and also you have "user_addresses" table. both tables are connected with each other using a foreign key. There are several records in users table and also more records in your address table. If you didn't use laravel eloquent relationship then you can get all address using "join" like as below:
Basic Model Query:
$userAddresses = User::select("users.*", "user_addresses.*")
->join("user_addresses", "user_addresses.id_user", "=", "users.id")
As you can above laravel query, you have to write long query, right now it is not big, but when you have more tables connected with users table then it can be more complected, so if we use laravel Relationship then you can do it just simple and you don't require to write every time join and anything, laravel will manage it. So you can write this way:
Model Query Using Relationship:
$userAddresses = User::find(1);
As you can see how it is simple, so i can say, we have to use laravel relationship.
In this tutorial, i will explain every types with how to insert data with relation model and how to get records from database table using model. i will also give example with table migration, model, retrieve and create new records. You have to just read and you will understand how it is working on all types of relationships in laravel. You can easily use order by, filter, where condition, delete, create, update, group by, get all, get count, save etc laravel methods.
Now as bellow i listed all laravel model relationships types and you can click on it and read in details of each types.
*** Click On it and Read in Details of RelationShip types:
I hope you found your best tutorials.