Laravel Eloquent Relationships

Create connections between data

What are relationships?

  • Link different Eloquent models
  • Ex: A user has posts
  • A user has a role
  • Comments belong to a user
  • Comments belong to a post

Types of Relationships

  • One to one: hasOne()
  • One to many: hasMany() / belongsTo()
  • Many to many: belongsToMany()
  • Has many through: hasManyThrough()
  • Polymorphic: morphTo() / morphMany()

One to One

A user has one address.

One to Many

A user has many posts.

Many to Many

A user has multiple roles.

Has Many Through

A user has posts which have comments.

A CSS Task

A JS Task

Auto-Loading Plugins

Database Migrations

users

  • name
  • email
  • avatar
  • username
  • password
  • role

posts

  • user_id
  • title
  • slug
  • image
  • content
  • status
  • published_at

Model Factories

Create sample data quickly.

Seeding Our Database

Fill our database with sample data.

Routing Our App

Top down overview.

 

  • Home Page
  • Single Page
  • Contact Page

Assets

CSS and JS for our application.

 

  • Using gulp and Laravel Elixir
  • Using Sass
  • Using jQuery
  • Using Bootstrap

Styling the Site

Styling Post Components

Blade Layouts

Simple and extendable views.

Contact Page

  • Showing a Form
  • Processing a form
  • Sending an email

Authentication

  • Built-in Laravel authentication
  • Creating login/register views

Profile Pages

  • View a user profile
  • Let a user edit their own profile

Admin Dashboard

  • Only admins can access
  • CRUD posts
  • New Dashboard namespace

Dashboard Routes

  • Home page
  • Posts resource controller

List All Posts

Create a Post

Edit a Post

Delete a Post

Authenticating the Dashboard

Only admins can access.

Dashboard Links

Only admins can see.

Laravel Eloquent Relationships

By Chris Sevilleja

Laravel Eloquent Relationships

  • 1,124