如何在 Laravel 中创建模型?

use*_*140 11 php laravel

我在正确理解 Laravel 模型时遇到一些困难。我们以框架提供的默认模型为例。

这是模型的全部内容:

用户.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');

}
Run Code Online (Sandbox Code Playgroud)

我理解之间发生的一切{}。但有些事情我完全迷失了:

比如,如何设置:

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
Run Code Online (Sandbox Code Playgroud)

如果我要创建一个模型来表示表products而不是表,我是否需要它usersuse如果该表不是用户表并且不需要身份验证,我需要哪些?我也没有正确理解身份验证吗?

另外,还有另外一个:

class User extends Eloquent implements UserInterface, RemindableInterface
Run Code Online (Sandbox Code Playgroud)

我知道我必须扩展 eloquent 类才能与数据库交互,但关于实现的同样的问题:我是否需要它们用于无用户表?我需要不同的吗?

感谢你们为我提供的所有帮助,如果我说得不够清楚,或者您有任何疑问,或者您想从我这里了解其他信息,请随时询问我。我真的很期待在进一步从事我现在正在工作的项目之前对 Laravel 中的模型有一个全面的理解,但由于缺乏关于我正在使用的技术的基本概念而不是由于编程工作中常见的技术困难。学习困难仅仅意味着你在学习时没有找到正确的方法。我被那只狗咬过好几次了。

Fra*_*cas 18

您可以使用 artisan 创建如下模型:

php artisan make:model YourNewModel
Run Code Online (Sandbox Code Playgroud)


小智 1

如果您正在创建产品模型,则不需要这个:

use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
Run Code Online (Sandbox Code Playgroud)

你应该使用这样的东西:

class Product extends Eloquent {

     protected $table = 'products';

     public $timestamps = false;

     protected $softDelete = false;

}
Run Code Online (Sandbox Code Playgroud)

检查有关模型的链接: http: //laravel.com/docs/5.0/eloquent

我建议您学习一些分步 Laravel 教程。它将回答您的大部分问题。Laracast https://laracasts.com/有一些非常精彩的视频教程。希望这对您有帮助。