如何在laravel中创建模型? [英] How do I create a model in laravel?

查看:61
本文介绍了如何在laravel中创建模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是模型的全部内容:

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:

例如,如何设置:

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

如果我要创建一个模型来表示表 products 而不是表 users ,我是否需要?如果该表是其他表而不是用户表,并且不需要身份验证,那么我需要使用哪些表?我是否也无法正确理解身份验证?

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?

另外,还有一个:

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 ?

感谢您能为我提供的所有帮助,如果我自己不够清楚,或者您有任何疑问,想从我这里了解到其他信息,请随时向我询问.我真的很希望在laravel中对模型有一个全面的了解,然后再继续进行我现在正在研究的项目,并由于缺乏有关我所使用技术的基本概念而陷入困境,而不是因为编程工作中常见的技术困难.刻苦学习只是意味着您在学习时没有获得正确的方法.我被那只狗咬了好几次.

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.

推荐答案

您可以使用工匠创建这样的模型:

You can use artisan to create a model like this:

php artisan make:model YourNewModel

这篇关于如何在laravel中创建模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆