Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a model in laravel?

Tags:

php

laravel

I'm having some difficulties properly understanding laravel models. Let's take the default model provided by the framework as an example.

This is the whole content of the Model:

User.php

<?php

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

}

I understand everything that happens between {}. But there are things I'm totally lost:

Like, how to set this:

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

Do I need that if I'm creating a model to let's say the table products instead of the table users? And which ones do I need to use if the table is other rather than a user table and doesn't require authentication? Am I also not understanding properly the authentication?

Plus, there is another one:

class User extends Eloquent implements UserInterface, RemindableInterface

I know I have to extend eloquent class in order to interact with the database, but same question regarding the implements: Do I need them for a no-users table? Do I need a different ones ?

Thank you for all the help you guys can provide me and if I didn't make myself clear enough or you just have any doubt, something else you want to know from me, don't hesitate to ask me. I'm really looking forward to have a well rounded comprehension of models in laravel before going further in the project I'm working now and getting stuck due to a lack of basic concepts regarding the technology that I'm using rather than due to the common technical difficulty that working in programming has. Learning the hard way just means that you didn't get the right approach while learning. I've been bitten by that dog several times.

like image 332
user2755140 Avatar asked Mar 16 '23 22:03

user2755140


1 Answers

You can use artisan to create a model like this:

php artisan make:model YourNewModel
like image 143
Frank Lucas Avatar answered Mar 19 '23 10:03

Frank Lucas