Top 5 features in Laravel you need to know


Eloquent relationships

Eloquent is Laravel’s native ActiveRecord implementation. It is built upon Laravel’s Fluent Query Builder. Due to the way in which Eloquent operates with Fluent, complex queries and relationships are easy to describe and understand.
ActiveRecord is a design pattern that describes an object-oriented way of interacting with your database.

For example: your database’s users table contains rows and each of these rows represents a single user of your site. Your User model is a class that extends the Eloquent Model class. When you query a record from your database, an instantiation of your User model class is created and populated with the information from the database.

One-to-one relationship

When a relationship connects two records in a way that doesn’t allow for more records to be related, it is a one-to-one relationship.

We’re defining the passport’s relationship differently. In the User class, we used the has_one() method. In the Passport class we used belongs_to().

First, let’s model this relationship in the User class

class User extends Eloquent
{
public function passport()
{
return $this->has_one(‘Passport’);
}
}

Now, let’s look at the Passport class:

class Passport extends Eloquent
{
public function users()
{
return $this->belongs_to(‘User’);
}
}

View the passport number of the user with the id of 1

$user = User::find(1);
if(is_null($user))
{
echo “No user found.”;
}
if($user->passport)
{
echo “The user’s passport number is “ . $user->passport->number;
}
else
{
echo “This user has no passport.”;
}

One-to-many relationships

One-to-many relationships are similar to one-to-one relationships. In this relationship type, one model has many of other relationships, which in turn belongs to the former.

One team
has many players. In this example, each player can only belong to one team
The only difference is that the team’s players() relationship uses as_many() rather than has_one(). The has_one() relationship returns a model object. The has_many() relationship returns an array of model objects.

First, let’s model this relationship in the Team class

class Team extends Eloquent
{
public function players()
{
return $this->has_many(‘Player’);
}
}

Then, model this relationship in the Player Class

class Player extends Eloquent
{
public function team()
{
return $this->belongs_to(‘Team’);
}
}

Display all of the players on a specific team:

$team = Team::find(2);
if(is_null($team))
{
echo “The team could not be found.”;
}
if(!$team->players)
{
echo “The team has no players.”;
}
foreach($team->players as $player)
{
echo “$player->name is on team $team->name. “;
}

Many-to-many relationships

The last relationship type that we’re going to cover is the many-to-many relationship. This relationship is different in that each record from each table could potentially be tied simultaneously to each record in another.

We have two models, each with the same type of relationship to each other. has_many_and_belongs_to is a long name. But, it’s a fairly simple concept. A course has many students. But, it also belongs to (belongs_to) student records and vice-versa.

Let’s define these models:

class Student extends Eloquent
{
public function courses()
{
return $this->has_many_and_belongs_to(‘Course’);
}
}
class Course extends Eloquent
{
public function students()
{
return $this->has_many_and_belongs_to(‘Student’);
}
}

Let’s look at how we’ll interact with these models in practice:

$student = Student::find(1);
if(is_null($student))
{
echo “The student can’t be found.”;
exit;
}
if(!$student->courses)
{
echo “The student $student->name is not enrolled in any courses.”;
exit;
}
foreach($student->courses as $course)
{
echo “The student $student->name is enrolled in the course
$course->name.”;
}

Conversely, let’s pull a course and see which students are a part
of it:

$course = Course::find(1);
if(is_null($course))
{
echo “The course can’t be found.”;
exit;
}
if(!$course->students)
{
echo “The course $course->name seems to have no students enrolled.”;
exit;
}
foreach($course->students as $student)
{
echo “The student $student->name is enrolled in the course
$course->name.”;
}

Enroll a new student into an existing course:

$course = Course::find(13);
if(is_null($course))
{
echo “The course can’t be found.”;
exit;
}
$new_student_information = array(
‘name’ => ‘Danielle’
);
$course->students()->insert($new_student_information);

Authentication

Filters

Validation

bundles

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.