laravel:雄辩的例子将数据插入数据库 [英] laravel: eloquent example insert data to database

查看:136
本文介绍了laravel:雄辩的例子将数据插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉问这个问题,但是我来自codeIgniter,并且非常难以理解雄辩的模型插入。这是一种使用模型的新方式。



我已经阅读了本文,现在了解一些基础知识。



我有以下示例。我有一个产品有很多属性,但相关的是品牌和产品名称。 (见下面的示例表)



products:id(PK),name,description,brand_id
品牌:id(PK),name



现在出现这个问题。我知道我可以创建一个新的品牌,我知道如何创建一个新的产品。但是,我没有任何连接两个人的气味。这是我现在的一些代码。我想创建一个新产品并自动填写品牌表。这是控制器的一部分。



简而言之。我想要brand.id里面的products.brand_id

  $ product = new Products; 
$ product-> name =product1;
$ product-> description =description1;

$ brand = new Brands;
$ brand-> name =brand1
$ product-> brand() - > associate($ brand);

$ brand-> save();
$ product-> save();

使其更加清晰。我有两个型号。产品和品牌型号。但是,我不清楚在模型中应该是什么。这是我当前的产品型号。我认为品牌型号只应该有一个受保护的$ table =品牌;线上模型类。

  class Products Enloquent {

protected $ table ='products' ;

public function brand()
{
return $ this-> hasOne('brands');
}
}

有人可以向我解释我做错了什么我找不到一个很好的教程如何使用在有雄辩的模型与关系插入数据。很多教程都是关于显示数据的。

解决方案

好的,我重读了你的问题,我想你有几个事情错了,所以不要在主帖上留下任何进一步的评论,我想我可以去一个答案,所以这里。



首先,你的关系是错误的类型和错误的方式。据我了解(并且在我自己的工作中实施这些事情)产品属于一个品牌 - 一个品牌可能有多个产品,但一个产品只能有一个品牌。所以首先你的数据库模式 - 你提到你有一个产品表与正常列,然后是外键 brand_id 。到目前为止这么好:这与我解释关系的方式是一致的。



然而,然后,继续向我们展示您的模型。您的产品型号为 hasOne 品牌 - 但实际上它属于品牌。你也没有定义关系的倒数 - 你需要双方使Laravel运行良好。除此之外,你的命名有点不合适 - 它可能会起作用,但是如果我们遵循Laravel约定,我们得到以下内容:



产品型号: Product.php

  class产品扩展Eloquent 
{
public function brand()
{
return $ this-> belongsTo('Brand');
}
}

现在品牌 model: Brand.php

  Eloquent 
{
public function products()
{
return $ this-> hasMany('Product');
}
}

您会注意到各种约定:


  1. 表名称是复数(产品品牌

  2. 外键使用单数( brand_id

  3. 模型名称是单数,而对于表产品品牌品牌

  4. $ table 属性不必指定,只要遵循Laravel约定(即表名称是模型类名的复数snake_case版本

  5. 您应该定义两者之间的关系方法(每个模型中的一个, belongsTo 的逆是 hasMany ,还有对 hasOne / belongsTo belongsToMany / belongsToMany

  6. 检索关系的方法的名称是明智的 - 如果您期望一个结果,使其成为单数(品牌产品),如果您希望多个结果使其复数(产品品牌

  7. 使用您的关系定义中的模型类名( $ this-> hasMany('Brand') not $ this-> hasMany('brands') 或任何其他变体

如果您遵守这些规则,您的模型可以非常简洁但非常强大



现在,至于如何实际定义真实数据,我感觉到您发布的代码可能会正常工作(这真的取决于Laravel在幕后如何聪明),但是正如我在第一条评论中所建议的那样,我会确保在调用 associate() $品牌 c>,这样Laravel就不会迷路了,该怎么办。因此我会去:

  //创建新品牌并保存
$ brand = new品牌;
$ brand-> name =品牌1;
$ brand-> save();

//创建新产品并保存
$ product = new Product;
$ product-> name =Product 1;
$ product-> description =描述1;
$ product-> brand() - > associate($ brand);
$ product-> save();

这样,你知道你在数据库中拥有已经定义的ID的品牌,使用它在一个关系。



您也可以以相反的方式定义关系,这样做可能不那么令人伤脑筋:

  //创建新产品并保存
$ product = new Product;
$ product-> name =Product 1;
$ product-> description =描述1;
$ product-> save();

//创建新品牌并保存
$ brand = new品牌;
$ brand-> name =品牌1;
$ brand-> save();

//现在将产品添加到品牌的产品列表中
$ brand-> products() - > save($ product);

您不需要调用 save()在最后一行之后的任一模型中,因为它将自动采用 $品牌 id 值,将它放入 $ product brand_id 字段中,然后保存 $ product



有关更多信息,请参阅有关如何执行此关系的文档插入: http://laravel.com/docs/eloquent#one-to-many



无论如何,希望这个帖子清除了很多你的代码出了什么问题。如上所述,这些是公约,你可以反对它们,但是要做到这一点,你必须开始添加额外的代码,对于其他开发人员来说,这可能是不可读的。我说如果你选择一个给定的框架,你可以遵循它的惯例。


I am sorry to ask this but I come from codeIgniter and have a really hard time understanding eloquent model insertion. This is a new way of working with models for me.

i have read this article and understand some basics now.

I have the following example. I have a product which has many attributes but the relevant one is brand and product name. (see the following example tables)

products: id(PK),name,description,brand_id brands: id(PK),name

Now here comes the problem. I know i can create a new brand and I know how I can create a new product. However I have no qlue how to connect the two together. Here is some code I have right now. I want to create a new product and automatically fill the brand table. This is a part of the controller right now.

In short. I want the brands.id inside the products.brand_id

    $product = new Products;
    $product->name = "product1";
    $product->description = "description1";

    $brand = new Brands;
    $brand->name = "brand1"
    $product->brand()->associate($brand);

    $brand->save(); 
    $product->save(); 

To make it more clear. I have 2 models. products and brands models. However it is not clear to me what should be in the model. This is my Current products model. I think the brands model only should have a protected $table = "brands"; line inside the model class.

 class Products extends Eloquent  {

    protected $table = 'products';

    public function brand()
    {
       return $this->hasOne('brands');
    }
  }

Can somebody explain to me what i do wrong. I cannot find a good tutorial how to work with inserting data inside eloquent models with relationships. The most tutorials are about displaying data.

解决方案

Okay, I've re-read your question and I think you have a few things wrong, so rather than leaving any further comments on the main post I figured I could have a go at an answer, so here goes.

First off, your relationship is the wrong type and the wrong way around. As I understand it (and as I implement these things in my own work) a product belongs to a brand - a brand may have multiple products, but a product can only have one brand. So first your DB schema - you mention you have a products table with the normal columns and then the foreign key brand_id. So far so good: this is consistent with the way I interpret the relationship.

However, you then go on to show us your model. You have the Product model as hasOne Brand - but actually it belongs to a brand. You also don't define the inverse of the relationship - you need both sides to make Laravel work well. In addition to that your naming is a bit out of whack - it'll possibly work, but if we follow Laravel conventions we get the following:

In the products model: Product.php

class Product extends Eloquent
{
    public function brand()
    {
        return $this->belongsTo('Brand');
    }
}

Now the brands model: Brand.php

class Brand extends Eloquent
{
    public function products()
    {
        return $this->hasMany('Product');
    }
}

Okay so far so good. You'll notice various conventions:

  1. Table names are plural (products, brands)
  2. Foreign keys use the singular (brand_id)
  3. Model names are singular and StudlyCase (so Product for a table products, Brand for table brands)
  4. The $table property doesn't have to be specified as long as you follow Laravel conventions (i.e. table name is the plural snake_case version of the model classname
  5. You should define the relationship both ways (one in each model, belongsTo's inverse is hasMany, there's also the pairs hasOne/belongsTo and belongsToMany/belongsToMany)
  6. The names of the methods to retrieve the relationship are sensible - if you expect one result, make it singular (brand in Product), if you expect multiple results make it plural (products in Brand)
  7. Use the model classnames in your relationship definitions ($this->hasMany('Brand') not $this->hasMany('brands') or any other variation

If you stick to these rules, your models can be really concise but very powerful.

Now, as for how you actually define real data, I have a feeling the code you posted may work fine (it really depends on how clever Laravel is behind the scenes), but as I suggested in my first comment, I'd ensure that I saved the $brand before calling associate(), just so that Laravel doesn't get lost working out what to do. As such I'd go for:

// create new brand and save it
$brand = new Brand;
$brand->name = "Brand 1";
$brand->save();

// create new product and save it
$product = new Product;
$product->name = "Product 1";
$product->description = "Description 1";
$product->brand()->associate($brand);
$product->save();

This way, you know you have the brand in the database with its IDs already defined before you go and use it in a relationship.

You can also define the relationship in the opposite manner, and it may be less brain-hurting to do so:

// create new product and save it
$product = new Product;
$product->name = "Product 1";
$product->description = "Description 1";
$product->save();

// create new brand and save it
$brand = new Brand;
$brand->name = "Brand 1";
$brand->save();

// now add the product to the brand's list of products it owns
$brand->products()->save($product);

You don't need to call save() on either model after that last line, as it wil automatically take the $brand's id value, place it into the $product's brand_id field, and then save the $product.

For more information see the docs on how to do this relationship inserting: http://laravel.com/docs/eloquent#one-to-many

Anyway, hopefully this post clears up a good amount of what was wrong with your code. As I said above, these are conventions, and you can go against them, but to do so you have to start putting extra code in, and it can be quite unreadable for other developers. I say if you pick a given framework, you may as well follow its conventions.

这篇关于laravel:雄辩的例子将数据插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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