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 keybrand_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
Now the
brands
model:Brand.php
Okay so far so good. You'll notice various conventions:
products
,brands
)brand_id
)Product
for a tableproducts
,Brand
for tablebrands
)$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 classnamebelongsTo
's inverse ishasMany
, there's also the pairshasOne
/belongsTo
andbelongsToMany
/belongsToMany
)brand
inProduct
), if you expect multiple results make it plural (products
inBrand
)$this->hasMany('Brand')
not$this->hasMany('brands')
or any other variationIf 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 callingassociate()
, just so that Laravel doesn't get lost working out what to do. As such I'd go for: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:
You don't need to call
save()
on either model after that last line, as it wil automatically take the$brand
'sid
value, place it into the$product
'sbrand_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.