Laravel 5.8 Tutorial From Scratch - e45 - Eloquent Relationships One To One (hasOne, BelongsTo)

preview_player
Показать описание
Eloquent offers a simple way to declare database relationships. In this episode, we are setting up a one to one relationship between a user and a phone.

Code for this Lesson

DigitalOcean Referral

Resources
Course Source Code

About This Course

Ready to get started on your path to Laravel Artisan? In this series, we are breaking down all of the basics of Laravel to get you comfortable using the world's most popular PHP framework. Let's get started!
Рекомендации по теме
Комментарии
Автор

Probably you will be covering one-to-many and many-to-many relationships very soon. Would you please focus on 1) multi-column primary keys (i.e. customer_id + order_id on a table say 'orders'), 2) how to increment/add the second column value (auto-increment order_id) when I provide the customer_id, that is the first column value while creating a record in this table, 3) child relationships from this orders table to say order_details table where primary key is a combination of customer_id + order_id + detail_id etc. Or how would you, an expert Laravel Developer, suggest to handle a situation like this? Should we totally opt out of multi-column parent-child relationships? So shameless of me to come out and demand things to you like this. My apologies. And as usual great explanation of 1-to-1 relationship here on this video. I guess this is the pain for being good at something. Blokes like us jump in asking for more. Thank you so very much.

shumit
Автор

Pls do one on searching with eloquent / Laravel :)

MementoNeli
Автор

Jumped here to add a comment!!! Let us create a discord server man so that well help each other along the way. thank you

RustineDave
Автор

Great work, i love you're tutorials. I have a question, why is de index on user_id suddenly removed later on in the cours?

Fox
Автор

May you make video about Service Container, Provider, Facade, pls?

vitalii
Автор

Hey nice video!
How to get the same functionallity with react on the front end?
In Tinker I can easily retrive the relationships, but with react it does not seem to work

DiscipleW
Автор

Can you help me with relating a pivot table with other tables by using this relationship(one to one)?

abela
Автор

I have the user_id on phone table cannot be null please help

nykroshatake
Автор

May I ask, let's say I want to add another attribute, what sequence do you think is the best? Create a model first, then do migration, then controller later on create a view page or what?

iqbalibrahim
Автор

Can't create table `laravel`.`subjects` (errno: 150 "Foreign key constraint is incorrectly formed")")




find error while specify the foriegn key for the datable ?
solution plz??

KingKhan-slkt
Автор

when i tried in tinker $user->phone;
i got this error:
Exception with message 'Property [phone] does not exist on this collection instance.'

bobosa
Автор

Whenever I try to migrate:fresh, I keep getting a "No connection could be made because the target machine actively refused it." alert. I don't know what went wrong. Please help.

tjburge
Автор

waiting for you response if anyone know my error

gururaj
Автор

I wanna to know why you setting one to one has foreign references, but another video didn't setting foreign references
i just wanna to know "setting foreign key 's meaning "
if your video has explained it, please told me ...
because i am Asian had poor English Listening .. thanks

xmpcrgx
Автор

Great, sir. Best tutorial ever. But please, very very please). When you will record videos on Many to Many, or other complicated relations. Could you make accent for edit/update methods. What is the best and clean approach - many to many has sync method. But OneToMany doesn't. Tried associate but didn't succeed.
What i mean. I have Protocol that has Tasks, and Tasks have Users. So if i delete some tasks in Protocol and update the whole Protocol i make the following:


// update method in Controller
// new array of tasks from the Form

$tasks = // array of existing tasks from the current model

$newTasks = Arr::pluck($request->task, 'id');
// need Laravel Helper function Arr((
$result=array_diff( $tasks, $newTasks); // find id's to delete


ProtocolTask::whereIn('id', $result)->delete();
foreach($request->task as $task) {
$newTask =, $task +



}


seems if i have (i will have much more relations) it'll be a disaster.


I am sorry for writing this here, but this Q seems very important for beeing studied Laravel way. And as you and your explanations are best here lot's of us would be grateful! Thanks in advance

dmdk