In this tutorial, today we discuss laravel one to one Eloquent relationship. Eloquent ORM means Object-relational Mapping and laravel provides a beautiful ActiveRecord structure. so we can easy to interact with the application database. let’s start about one to one eloquent relationship.
Laravel one to one relationship is based on the relation between two tables. so we need one table of the primary key and another table of reference key. For example, we have two tables: Students and Standards. both tables are connected as a one to one relationship with each other using the primary key and reference key.
Setting Database Configuration
After complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=Enter_Your_Database_Name(one_to_one_relation) DB_USERNAME=Enter_Your_Database_Username(root) DB_PASSWORD=Enter_Your_Database_Password(root)
Create Table using migration
Now, We need to create a migration. so we will below command using create the students and standards table migration.
php artisan make:migration create_students_table --create=students php artisan make:migration create_standars_table --create=standards
After complete migration. we need below changes in the database/migrations/create_students_table and database/migrations/create_standars_table file.
create_students_table.php
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateStudentsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('students', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('first_name'); $table->string('last_name'); $table->text('address'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('students'); } } ?>
create_standars_table.php
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateStandardsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('standards', function (Blueprint $table) { $table->bigIncrements('id'); $table->integer('student_id')->unsigned(); $table->string('standard_name'); $table->timestamps(); $table->foreign('student_id')->references('id')->on('students')->onDelete('cascade'); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('standards'); } } ?>
Run the below command. after the changes above file.
php artisan migrate
Create Model
Here below command help through we will create the Student and Standard model. we will also use “hasOne()” for a student model and “belongsTo()” for a standard model.
php artisan make:model Student php artisan make:model Standard
Student.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Student extends Model { protected $fillable = [ 'first_name','last_name', 'address' ]; /** * Get the standard record associated with the student. */ public function standard() { return $this->hasOne('App\Standard'); } } ?>
Standard.php
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Standard extends Model { /** * Get the student that owns the standard. */ public function student() { return $this->belongsTo('App\Student'); } } ?>
Route and Controller
We have to need put below student resource route in routes/web.
<?php /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get('/', function () { return view('welcome'); }); Route::get('student','studentsController@index'); ?>
Here below command help to create the student controller.
php artisan make:controller StudentsController
StudentController.php
<?php namespace App\Http\Controllers; use App\Student; use App\Standard; use Illuminate\Http\Request; class StudentController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { // $student1 = Student::find(1)->standard; dd($student1); } } ?>
I am sure this piece of writing has touched all the internet
visitors, its really really good piece of writing on building up new website.
I’ve been browsing online more than three hours today, yet I never found any interesting article like yours.
It’s pretty worth enough for me. In my view, if all web owners and bloggers made good content as you
did, the net will be a lot more useful than ever before.
I’ve been surfing online more than 4 hours today, yet I never found any
interesting article like yours. It’s pretty worth enough for me.
In my opinion, if all webmasters and bloggers made good content as you did, the internet will be a lot more useful than ever before.
http://foxnews.org/
thanks for comment
Really informative and excellent structure of written content, now that’s user friendly (:.
Good day! This is my first visit to your blog! We
are a collection of volunteers and starting a new initiative in a community
in the same niche. Your blog provided us beneficial information to work on. You have done a wonderful
job!
Great wordpress blog here.. It’s hard to find quality writing like yours these days. I really appreciate people like you! take care
I don’t normally comment but I gotta tell thanks for the post on this perfect one : D.
This is the right blog for anyone who wants to find out about this topic. You realize so much its almost hard to argue with you (not that I actually would want…HaHa). You definitely put a new spin on a topic thats been written about for years. Great stuff, just great!
Wow, this article is nice, my sister is analyzing such things, so I am going to inform her.
Howdy! I could have sworn I’ve been to this blog before but after browsing through some of the post I
realized it’s new to me. Nonetheless, I’m definitely delighted I found it aand I’ll bee bookmarking and checking back frequently!
Wow cuz this is great work! Congrats and keep iit up!
Very nice post. I just stumbled upon your blpog and wanted to say that I’ve truly enjoyed surfing around your blog posts.
In any case I will bbe subscribing too your feed and
I hope you write again vedry soon!
It iss not my first time to go too see this website, i am visiting thiis web page dailly and take good information from here all
the time.
Hello everyone, it’s my first visit at this website, and piece of writing is genuinely fruitful designed for me, keep up posting
such articles oor reviews.
Ohh, its fastidious discussion about this article here at this web site,
I have read all that, so now me aloso commenting at this place.
I have found very interesting your article.It’s pretty worth enough for me.
In my view, if all website owners and bloggers made good content as yoou did, the web will be a lot mlre useful than ever before.
What’s up, after reading this awesome post i am as
well delighted to share my experience here with
friends.
I like iit whenever people come together and share thoughts.Great website,
continue the good work!
Really interesting information, I am sure this post
has touched all internet users, its really really pleasant piece of writing on building up
new website.
I do not even know how I ended upp here, but I thought this post was great.
I don’t know whho you are but certainly you aare going to a famous blogger if you are
not already 🙂 Cheers!
Great post! We will be linking to tuis great
post on oour website. Keep up the great writing.
You need to take part in a contest for one of tthe most
useful websitds online. I will recommend this site!
It’s not my first time to go to see this web page, i am visiting
this web site vefy often and take good facts from here.
Thank you for this very good posts. I was wanting tto
know whether you were planning of publishing
similar posts to this. Keep up writing superb content articles!
I enjoy reading through your website. Thanks!
It is not my first time to go to ssee this website, i
am visiting this web page dailly and take good information from here all the time.
Hello! I wish to say that thyis post is awesome, great written and come with approximately all impirtant infos.
I’d like to lok extra posts like this! 🙂
Great post! We will be linkimg to this great post onn our website.
Keep up the great writing.
Hello to every one, sinmce I am genuinely keen of reading
this website’s post to bbe updated regularly.
It carries nice data.
Wow, amazing blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your web site is excellent,as well as
the content.
Wow, this article is nice, my sister is analyzinbg such things, so
I am going to inform her.
You need tto take part in a contest for one of the most useful websites online.
I will recommend this site!
I am pleassed that I detected this web blog, just the right information that I was
searching for!
Nice post. I was checking continuously this blog andd I’m impressed!
Very useful informatio specially the last part 🙂 I care
for such infoo a lot. I was looking ffor this particular information for a long time.
Thank you and best of luck.
Great post! We will be linking to this great post on our website.
Keep up thhe great writing.
Hey! Thhis iss mmy first comment here so I just wanted to give
a quick shout out and say I truly enjoy reading through your
articles. Appreciate it!
It is not my irst time to go to see this website, i am visiting thi web page dailly and take
good information from ere all the time.
Very good post! We will be linking to this great content on our website.Keep up the great writing!
Wow cuuz thks is great work! Congrats and kesep
it up!
It is not my first time to goo to see this website, i am visiuting this web page dailly and takke good information from here aall the time.
Hi there too every body, it’s my first pay a quick visit of this website; this blog contains awesome and genuinely excellent data for visitors.
I like it whenbever people come together and share thoughts.Great website, continue the
good work!
I simply want to input that you have ? good website ?nd I enjoy the design and also artcles ?n it!
Excellent post. I was checking continuously this blog and
I am impressed! Extremely useful information. I care foor such information a lot.
I was looking for this certain information for a ery lonbg time.Thank you and good luck.
I enjoy reading through your website. Thanks!
Greetings! Very helpfrul advice in this particular article!
It’s the little changes thast make the most important changes.
Many thanks for sharing!
Way cool! Some very valid points! I appreciate you penning this post and
the rest of thee website is alpso really good.
Please keep uus up to date like this. Thanks for sharing…
I got this sjte fro my pal who shaed with me concerning this website
and now this time I am browsing this website and reading
very informative articles or reviews at this place.
Nice blog here! Also your web site a lot up very fast!
I desire my site loaded up as quickly as yours…
Would love to perpetually get updated outstanding web blog!
Wow cuz this iss great work! Congrats and keep it up!
Please keep us up to date like this. Thanks for sharing…
Fabulous, what a weeb site it is! This web site provides useful data to us, keep it up.
Fabulous, what a web site itt is! This web sitte provides useful data to
us, kerp it up.
Very good info. Lucky me I discovered your blog by accident.I have book-marked it for later!
I enjoy reading throuyh your website. Thanks!
Very nice post. I just stumbled upon your blog and wanted to say that I’ve truly enjoyed
surfing around your blog posts. In any case I will be
subscribing to your feed and I hoe you write again very soon!
Hello to every one, since I am genuinely keen of reading this website’s post to be updated regularly.
It caries nice data.
Hello! I wish to sayy that thi post iis awesome, great
written and come with approximatey all important infos. I’d
like tto look extra posts like this! 🙂
Hello there! Would you mind if I share your blog with
my twitter group? There’s a lot of people tgat I think would really appreciate your content.Please let me know.
Thanks!
Very nice post. I just stumbled upon your blog and wanted
to say thwt I’ve truly enjoyed surfing aroind your blog
posts. In any case I will be subscribing to your feed and I hope you
write again very soon!
Nice post. I was checking continuously this blog and I’m impressed!
Very useful information specially thhe last part 🙂 I care for such info
a lot. I waas looking for this particular inormation for a long time.
Thank you aand best of luck.
Thank you for this very good posts. I was wanting to know whether
you were planning of publishing similsr posts to this.
Keepp up writing superb content articles!
Heplo There. I found yiur blog using google. This is an extremely well written article.
I’ll make sure tto bookmark it and return to read more
of your useful information. Thanks for the post. I’ll certainly return.
I’m very happy to discover this page. I need to to thank you for ones time just for
this fantastic read! I definitely really liked every little bit of it and
I have you bookmarked to check out new things in your blog.
Pretty! This has been an extremely wonderful post.Thank you for
supplying this info.
Fabulous, what a weeb site it is! This web site provides
useful data to us, keep it up.
Very good post! We will be linking to this great content on our website.Keep up the great writing!
Hello to every one, since I am genuinely keen of reading this website’s post to be updated regularly.
It carries nice data.
Pretty! Thiis has been an extremely wonderful post.Thank you for supplyingg this info.
Would love to perpetually get updated outstanding web blog!
Hello There. I found youur blog using google. This is an extremely well written article.
I’ll make sure to bookmark it and return to read more of your useful information. Thanks for
the post. I’ll certainly return.
Great post! We will bee linking too this great post on ourr website.
Keep upp the great writing.
Pretty! This has been an extremely wonderful post. Thanks for providing
this info.
Pretty! This haas been an extremely wonderful post.
Thanks for providing this info.
Hello all,here evey person is sharing such know-how, so it’s pleasant too read this website, andd I used to goo to see this website daily.
You need tto take part iin a contest for one of
the most useful websites online. I will recommend
this site!
Nice post. I was checking continuously thos blokg and I’m
impressed! Very useful information specially the last part 🙂 I care foor sch info a lot.
I wass looking for this particular information for a long time.
Thank you and best off luck.
Very good post! We will be linking to this great content on our website.Keep up tthe great writing!
What’s up, after reading this awesome post i am as well delighted to share my experience here with friends.
Way cool! Some very valid points! I appreciate you penning this post annd the rest oof the website iss
also really good.
Wow cuz this is great work! Congrats andd keep it up!
Hello! Do you use Twitter? I’d like to ollow you if that would be okay.
I’m definitely enjoying your blog and look forward too new articles.
Hello everyone, it’s myy first visit att this website, and pjece
of writinng is genuinely fruitful designed for me, keep up posting such articlkes or reviews.
Pretty! Thiis has been an extrremely wonderful post.
Thanks for providing this info.
Hello there! Would you mind iff I share your
blog with my twitter group? There’s a lot of people that I think would really appreciate your content.Please let me know.
Thanks!
Very nice post. I just stumbled upon your blog and wanted to say that
I’ve truly enjoyed surfing around your blog posts. In any case I will be subscribing to your feed and
I hope you write again very soon!
Pretty! This has een an extremely wonderful post.
Thanks for providing this info.
Hello There. I found your blog using google. This is an exremely well written article.
I’ll make sure to bookmark it and return to rea more of you
seful information. Thanks for the post. I’ll certainly return.
Hello too every one, since I am genuinely
keen of reading this website’s post to be updated regularly.
It carries nice data.
You need to take part in a contest for one of the most useful websites online.
I will recommend this site!
I got this site from my pal whho shared with me concerning this website annd now
thius time I am browsing this website and reading very informative articles or reviews at this place.
Hello everyone, it’s my first visit at this website,
and piece of writinng iis genuinely fruitful dewigned for me, keep up posting
such articles or reviews.
I need to to thank you for this fantasric read!! I definitely enjoyed every
bit off it.I have got you book-marked to look at new things youu post…
I have found very intereesting your article.It’s pretty worth
enough for me.In my view, if all website owners and bloggerss made good content
ass you did, the webb will be a lot more useful than ever
before.
Hello, I enjoy reading all of your article. I likje too write a little comment to support you.
Nice post. I was checking continuously this blog and I’m impressed!
Very useful information specially the last part
🙂 I care ffor such info a lot. I was looking for this particular information for a long time.
Thank you and best of luck.
Wow, amazing blog layout! How long have you been blogging for?
you made blogging look easy. The overall look oof your web
site is excellent,as well as the content.
I enjoy reading through your website. Thanks!
Hello everyone, it’s my first visit at this website, and
piece of writing is geninely fruitful designed for me, keep up posting such articles or reviews.
Well composed articles like yours renewws my faith in today’s writers.You’ve written information I caan finally agree on and also use.Many thanks foor sharing.
Pretty! This has been an extremely wonderful post.
Thanks for providing this info.
Nice post. I was checking continuously this blog and I’m impressed!
Very useful information soecially the last part 🙂 I
care for such info a lot. I waas looking for this particular
information for a long time. Thank you and best of luck.
It’snot my first tikme to go to seee this web page, i
am visiting this web site very often and take
good facts from here.
Great delivery. Great arguments.Keep up the amazing spirit.
Hello, after reading this amazing article i aam ass well happy to share my familiarity here with
mates!
Reaqlly interesting information, I am sure this post has touched all internet
users, its redally really pleasant piecce of writing oon building up new website.
Howdy! I could have sworn I’ve been to this blog before but after browsing
through some of the post I realized it’s new to me.
Nonetheless, I’m definitely delighted I found it and I’llbe
bookmarking and checking back frequently!
Hi there to every body, it’s my first go to see of this
webpage; this webpage contains amazing and really excelklent data in support
of readers.
Very good info. Lucky me I discovered your bloig by accident.
I have book-marked it for later!
Hello all, here every person is sharibg such know-how, so it’s pleasant to read this website, and I used to go to see
this website daily.
I got this ssite from mmy pal who shared with me concerning tuis website
and now this timje I amm browsing this website
and reasding very informative articles or reviews at this place.
Would love too perpetually get updated outstanding web
blog!
Very good info. Lucky mme I discovered your blog by accident.
I have book-marked it ffor later!
Nice blog here! Also your web site a lot up very
fast! I desire my site loaded up as quickly as yours…
Hey! This is my first comment here so I just wanted to give a quick
shout out and say I truly enjoy reading through your articles.
Appreciate it!
Wow, amazing blog layout! How long have you been bloggung for?
you made blogging look easy. The overall look of your web sitge is excellent,as well aas the content.
I enjoy reading through your website. Thanks!
Your mode of describing all in this piece of writing is in fact pleasant, all be capabe
of simply bee aware of it, Thanbks a lot.
I need to to thank you for this fantastic read!!
I detinitely enjoyed every biit of it.I have got you book-marked to look at new things you post…
Pretty! This has been an extremely wonderful post.
Thanks ffor provfiding this info.