Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tomasnorre's avatar

Laravel Models

Hi,

I'm quite new to Laravel, but not to PHP. I have started to look into Laravel due to its popularity, and wanted to see/learn what it's all about.

A thing that stroke me is the domain models and the creation part.

I'm used to from PHP that I have a class lets say Person which have some properties like firstName and lastName.

<? 

class Person 
{

	public string $firstName;
	public string $lastName;

    public function __constructor(string $firstName, string $lastName)
    {
		$this->firstName = $firstName;
		$this->lastName = $lastName;
	
	}
}

but in Laravel, it's done with attributes, when working with the Eloquent Model.


$person = new Person(['firstName => 'John', 'lastName' => 'Doe']);

The only way to see which attributes a class has is to look into the migration for the given table. The Person class itself doesn't have properties the same way as in vanilla PHP.

Honestly I find this a little strange, and would like to understand the reasoning and advantages of this.

So if you could point me to some videos here, where this is the addressed or explain some reason behind it, I would be really happy.

0 likes
12 replies
jlrdw's avatar

I don't follow, I never use a constructor to create a record or update, I just use regular methods, same in php, java, asp.net, etc

    public function petUpdate(Request $request) {
      // Auth here however you need

        $validator = \Illuminate\Support\Facades\Validator::make($request->all(), [
                    'species' => 'required',
        ]);

        if ($validator->fails()) {
            return response()->json($validator->errors(), 422);
        }

        $petid = $request->input('petid');
        $species = $request->input('species');
        $postdata = [
            'species' => $species
        ];
        DB::table('dc_pets')
                ->where('petid', $petid)
                ->update($postdata);
        return Response::json(['success' => 'all okay']);
        
    }

For heavy search queries I use scopes in model.

tomasnorre's avatar

Lets say that I have the Person object stored in DB regardless how it got there.

When I'll use it then, I don't know from reading the class nor my auto-completion which properties the object has expect that I look into the Table migration or the Database directly.

jlrdw's avatar
jlrdw
Best Answer
Level 75

You could view https://laracasts.com/series/laravel-8-from-scratch

and

https://laracasts.com/series/laravel-6-from-scratch very in depth series here.

But when all is done, you have a database record stored, I don't even understand calling it an object.

Example: https://laravel.com/docs/8.x/eloquent#inserts

When you see things like:

        $flight = new Flight;

        $flight->name = $request->name;

        $flight->save();
    }

That's an active record thing, in the background you have a regular insert.

tomasnorre's avatar

Thanks.

The "laravel 8 from scratch" I am watching as we speak.

I agree that I have a database record store, but it's of the Object type Person or User, Pet or what ever class. And that's why I call it an object.

I want to work with that as an object.

Perhaps I need to adjust my understanding of this to match how laravel is "thinking".

jlrdw's avatar

@tomasnorre yes object is for us in coding, so object is correct in that sense. But it's just data stored.

I try not to get lost in all the fancy terminology. Like OOP and MVC. OOP is for us humans to separate code. But runtime all code is procedural, broken down to line by line for the processor.

Don't forget, eloquent (active record) does have to convert to normal sql and pdo at runtime.

I once used procedural php, but once I used an MVC framework I never looked back.

But I look at things different, I have contributed to another framework, thus what someone sees as a "shortcut", you already know in the background (vendor folder) there is regular normal php making that happen.

But you will find laravel easy I'm sure.

tomasnorre's avatar

Thanks. I find it quite easy, but not that "logic", but as said before it's probably my way of thinking that I need to adjust a little.

usmanmalik's avatar

@tomasnorre , Laravel uses object-relational mapper ( ORM ). That's why you don't have to define properties in the models.

However, you can refer to database columns with different names using accessor methods.

1 like
martinbean's avatar

@tomasnorre It’s nothing specific to Laravel. It’s just a design pattern, specifically ORM (object relational mapper).

Eloquent “models” aren’t entity classes as per your example. What you have is just a simple value object. It’s a class that represents something. Eloquent model classes extend the base Eloquent ORM class and have a ton of functionality for interacting with rows in a database representing that class, that your POPO example does not.

1 like
tomasnorre's avatar

I'm aware of the ORM part, I just like the "Symfony"-way of having Objects mapped with Database over Doctrine Annotations.

namespace App\Entity;

use App\Repository\ExerciseRepository;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass=ExerciseRepository::class)
 */
class Exercise
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private ?int $id = null;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private ?string $title;

Here I know the properties on the object, like I would on a value object, but it's mapped over annotation.

Does something like this exists for Laravel as well?

martinbean's avatar

@tomasnorre Just use Doctrine if you want to use Doctrine.

Eloquent is Laravel’s built-in ORM, but you don’t have to use it. If you go into Eloquent hoping it’s like Doctrine, then you’re going to be disappointed. Doctrine does things its way; Eloquent does things its way.

1 like
jlrdw's avatar

Yes doctrine orm is a mapper, eloquent is more active record. I did try doctrine once, but I found it unnecessary with that exact mapping, to me I just want to save the database record after the insert.

Of course still do any sanitizing of data and validation to make sure you are secure. But if you are watching @Jeffreyway courses you will learn validation.

Another thing to really concentrate on learning well is authentication and authorization, so important especially these days.

tomasnorre's avatar

Thank you for all your replies. It helped me better understand how Laravel works.

Will dig more into it and learn more.

Take care and stay safe.

Please or to participate in this conversation.