Home  >  Article  >  Backend Development  >  Learn Laravel Easily - Basics

Learn Laravel Easily - Basics

黄舟
黄舟Original
2017-08-31 14:11:171393browse

The Laravel framework is one of the most popular PHP development frameworks in the world. In recent years, Laravel has quickly occupied the top spot among PHP development frameworks due to its power, security, elegance and other features. Now the Laravel framework has become the preferred framework for large Internet companies and PHP siegeers.

Learn Laravel Easily - Basics

Course playback address: http://www.php.cn/course/282.html

The teacher’s teaching style:

The lectures are friendly and natural, unpretentious, not pretentious, nor deliberately exaggerated, but talk eloquently and carefully, and the relationship between teachers and students is In an atmosphere of equality, collaboration, and harmony, silent emotional exchanges are carried out, and the desire and exploration of knowledge are integrated into simple and real teaching situations. Students gain knowledge through quiet thinking and silent approval

The more difficult point in this video is Eloquent OR:

What is Eloquent

Eloquent is Laravel's 'ORM', that is, 'Object Relational Mapping' , object relational mapping. The emergence of ORM is to help us make database operations more convenient.

Eloquent allows a 'Model class' to correspond to a database table, and encapsulates a lot of 'functions' at the bottom layer, which makes the Model class very convenient to call. Let's take 'app/models/Article.php' in Learn-Laravel-4 as an example to give a rough explanation. The code of this file is as follows:

<?php 
class Article extends \Eloquent { 
protected $fillable = []; 
}

'protected $fillable = [];' This line of code has no value here. It is automatically generated by the generator and we will not discuss it here.

This class couldn't be simpler. There is no specified namespace and no constructor. If the meaningless line of code is not included, this file only has two meaningful things: 'Article ' and '\Eloquent'. That's right, Eloquent is so awesome. You only need to inherit the Eloquent class, and you can do many, many things such as 'first() find() where() orderBy()'. This is the powerful power of object-oriented.

Basic usage of Eloquent

Without further ado, I will directly show the codes for several common usages of Eloquent. They are typed by hand in the MarkDown editor. Please forgive me if there are any spelling errors.

Find the article with id 2 and print its title

$article = Article::find(2); 
echo $article->title;

Find the article with the title "I am the title" and print the id

$article = Article::where(&#39;title&#39;, &#39;我是标题&#39;)->first(); 
echo $article->id;

Query all articles And print out all titles

in a loop
$articles = Article::all(); // 此处得到的 $articles 是一个对象集合,可以在后面加上 &#39;->toArray()&#39; 变成多维数组。 
foreach ($articles as $article) {     
echo $article->title; 
}

查找 id 在 10~20 之间的所有文章并打印所有标题

$articles = Article::where(&#39;id&#39;, &#39;>&#39;, 10)->where(&#39;id&#39;, &#39;<&#39;, 20)->get(); 
foreach ($articles as $article) {     
echo $article->title; }

查询出所有文章并循环打印出所有标题,按照 updated_at 倒序排序

$articles = Article::where(&#39;id&#39;, &#39;>&#39;, 10)->where(&#39;id&#39;, &#39;<&#39;, 20)->orderBy(&#39;updated_at&#39;, &#39;desc&#39;)->get(); 
foreach ($articles as $article) {     
echo $article->title; 
}

基础使用要点

1. 每一个继承了 Eloquent 的类都有两个 '固定用法' 'Article::find($number)' 'Article::all()',前者会得到一个带有数据库中取出来值的对象,后者会得到一个包含整个数据库的对象合集。

2. 所有的中间方法如 'where()' 'orderBy()' 等都能够同时支持 '静态' 和 '非静态链式' 两种方式调用,即 'Article::where()...' 和 'Article::....->where()'。

3. 所有的 '非固定用法' 的调用最后都需要一个操作来 '收尾',本片教程中有两个 '收尾操作':'->get()' 和 '->first()'。

4. 如果你不理解为什么 'Article' 这个类可以使用 '->where()' '->get()' 等很多方法的话,说明你需要去读一下 PHP 对象继承的文档了:对象继承。

The above is the detailed content of Learn Laravel Easily - Basics. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn