In-depth understanding of Laravel eloquent (iii)-Model Relationship (association)
In this article, I will work with you to learn the most complex and difficult part of eloquent-the relationship between models. The official English document is
1. Introduction
Laravel's own eloquentorm provides a beautiful, simple ActiveRecord implementation that interacts with the database, each of which corresponds to a "model" of interacting with the table, allowing you to query the table for data,
Data from the premise database
Users table ID name email password remember_token created_at updated_at1 123456 123@ qq.com $2Y$10$EFEO1GCCK6JDWGXJQJNBK.FVZJGU7I68UKMPQNWBX9VPNVUVGTHM6
A user may have multiple articles, an article is written by a user, this is the relationship. The same article may contain more than one tag, and one tag may be associated with multiple articles.
In the project, we already have the user.php, namely
There are now 2 of tables
Goods and products
$products = Goods::where('merchant_id','=','5')->get(); foreach($products as $p){ print_r($p->product_id); $_name = Product::where('id','=',$p->product_id)->get();
Assume that we have two tables: user and posts. Each User can have multiple Posts, and each Post can only belong to one User. The relationships defined in User Model and Post Model are as follows:// User. phpPublic function post (){Return $ this->
Create a eloquent model, which automatically associates the article table,
PHP Artisan Make:model Article
PHP Artisan Make:model Article model created successfully.
This file will be generated in the
A user may have multiple articles, an article written by a user, which is a relationship. The same article may contain multiple tags, while a tag may have multiple articles associated with it.
In the project, we already have the user.php, which is
Let's build the first model.
Copy Code code as follows:
PHP Artisan Make:model Article
#输出
Model created successfully.
Created migration:2015_03_28_062517_create_articles_table
View the generated Files app/article.php
first, the demand:
The most common in data search is to call the same method query, and the query field may be one or several of the fields together to combine the query, such as: The search for the list, basically are a few fields randomly
good instructions: "JavaScript Again" – This manual can be found on the Mozilla Developer Network, language justice. This book is rich in content, there are a lot of code examples and paragraphs introduced. "Eloquent JavaScript" – The book is written by Marijn Haverbeke and can be read online for free, if you want to get a hard copy to buy on the Amazon website.
JavaScript"-This instruction can be found on the Mozilla Developer Network, and the language is fair. This book is rich in content and contains many code examples and Text sections. Eloquent JavaScript-This book is written by Marijn Haverbeke and can be read online for free. If you want to get a hard copy, you can purchase it on the Amazon website. It is more de
Developer Network, and the language is fair. This book is rich in content and contains many code examples and Text sections. Eloquent JavaScript-This book is written by Marijn Haverbeke and can be read online for free. If you want to get a hard copy, you can purchase it on the Amazon website. It is more detailed than MDN because it not only covers the JavaScript
that run in JavaScript.
This is not without justification. The following is an example of defining a reduce function in JavaScript, from Eloquent JavaScript.
function forEach(array, action) {for (var i = 0; i
Note: The recursive version of reduce is not used here. JavaScript
is a perfect continuation of the Java experience, given the freedom that every computer runs.Functional programming: A one-class citizen is a function, not a recursiveFunctions in JavaScript are the first class of citizens, and they are values that can be stored in variables, passed to other functions, and executed at the appropriate time.This opens the door to the functional programming world, which is the perfect way to structure
Personal Summary:1.webassembly Introduction: Webassembly is an efficient, low-level bytecode for developing network applications. Allows languages other than JavaScript (such as C,c++,rust and other) to be used in Web applications to write applications and then compile (early) WebAssembly.This is the sixth chapter of how JavaScript works.Now, we'll dissect how WebAssembly works, and most importantly, its pe
The setTimeout and setInterval of JavaScript are two methods that can easily fool others' feelings, because we often think that the call will be executed in the established way. I think many people share the same feeling,
JavaScript timer that is easy to fool others' feelings
The setTimeout and setInterval of JavaScript are two methods that can easily fool othe
, but Abas Ah! It is highly recommended to read.High-performance javascript: Another masterpiece of Nicholas C. Zakas, author of the Advanced programming of JavaScript."Eloquent JavaScript": This book is only 200 pages, very short, but changed my writing habits, this book through a few very classic examples (Aunt Emily
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.