如何在 Laravel 中创建模型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28709143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a model in laravel?
提问by user2755140
I'm having some difficulties properly understanding laravel models. Let's take the default model provided by the framework as an example.
我在正确理解 Laravel 模型时遇到了一些困难。我们以框架提供的默认模型为例。
This is the whole content of the Model:
这是模型的全部内容:
User.php
用户名.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?
如果我正在创建一个模型来让我们说表格products
而不是表格,我是否需要那个users
?use
如果表是其他表而不是用户表并且不需要身份验证,我需要哪些?我是否也没有正确理解身份验证?
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 ?
我知道我必须扩展 eloquent 类才能与数据库交互,但关于工具的同样问题:我是否需要它们用于无用户表?我需要不同的吗?
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.
感谢你们为我提供的所有帮助,如果我没有说清楚或者你有任何疑问,你想从我这里知道什么,不要犹豫,问我。我真的很期待在继续我现在工作的项目之前对 laravel 中的模型有一个全面的理解,并且由于缺乏关于我正在使用的技术的基本概念而不是由于编程工作中常见的技术难点。学习困难只是意味着你在学习时没有得到正确的方法。我被那条狗咬过好几次了。
回答by Frank Lucas
You can use artisan to create a model like this:
您可以使用 artisan 来创建这样的模型:
php artisan make:model YourNewModel
回答by taurus
You do not need this, if you are creating products model :
如果您正在创建产品模型,则不需要这个:
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
You should use something like this:
你应该使用这样的东西:
class Product extends Eloquent {
protected $table = 'products';
public $timestamps = false;
protected $softDelete = false;
}
Check this link about models: http://laravel.com/docs/5.0/eloquent
检查有关模型的链接:http: //laravel.com/docs/5.0/eloquent
I would recommend doing some of step by step laravel tutorials. It will answer most of your questions . Laracast https://laracasts.com/have some really amazing video tutorials. Hope this was helpful for you.
我会建议做一些循序渐进的 Laravel 教程。它将回答您的大部分问题。Laracast https://laracasts.com/有一些非常棒的视频教程。希望这对你有帮助。
回答by dustbuster
Furthermore, if you wanted to make a model, controller, and migration. you can do:
此外,如果您想制作模型、控制器和迁移。你可以做:
php artisan make:model YourNewModel -mcr
php artisan will create basic CRUD controller and the model, was well as the migration. All the stock dependancies will be created as well.
php artisan 将创建基本的 CRUD 控制器和模型,以及迁移。所有的股票依赖也将被创建。