Home Backend Development PHP Tutorial Learn Laravel Easily - Basics

Learn Laravel Easily - Basics

Aug 31, 2017 pm 02:11 PM
Base learn

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 = []; 
}
Copy after login

'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;
Copy after login

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;
Copy after login

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; 
}
Copy after login

查找 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; }
Copy after login

查询出所有文章并循环打印出所有标题,按照 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; 
}
Copy after login

基础使用要点

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP Basics Tutorial: From Beginner to Master PHP Basics Tutorial: From Beginner to Master Jun 18, 2023 am 09:43 AM

PHP is a widely used open source server-side scripting language that can handle all tasks in web development. PHP is widely used in web development, especially for its excellent performance in dynamic data processing, so it is loved and used by many developers. In this article, we will explain the basics of PHP step by step to help beginners from getting started to becoming proficient. 1. Basic syntax PHP is an interpreted language whose code is similar to HTML, CSS and JavaScript. Every PHP statement ends with a semicolon; Note

You must learn how to make a resume using Word You must learn how to make a resume using Word Mar 20, 2024 am 11:40 AM

When you are looking for a job, you will face many interviewers. A perfect resume is very attractive when you see it in front of you. The same principle applies to seeing a resume as seeing a person. It gives people different feelings, so Just need a perfect resume. Today I want to share with you how to use Word to create a resume. Next, I will introduce the steps in detail. I hope you will take a careful look! First, open the Word document, then find the [Insert] option in the toolbar, select [Table], and click [Insert Table] in the drop-down menu. In the pop-up dialog box, enter the required number of columns and rows, and then click OK to create the table (as shown in the red circle in the figure below). 2. After entering the number of columns and rows, click OK to generate the table (as shown in the figure below). 3

Can I learn Linux from scratch? What do I need to learn? Can I learn Linux from scratch? What do I need to learn? Feb 19, 2024 pm 12:57 PM

If you want to work in the IT industry, but do you want to learn programming, which technology should you choose? Of course it is Linux operation and maintenance. Linux is a very popular technology on the market, with a wide range of applications and good employment prospects, and is liked by many people. So the question is, can I learn Linux operation and maintenance with zero basic knowledge? In the server market, Linux system has a market share of up to 80% because of its advantages such as stability, security, free open source, efficiency and convenience. From this, it can be seen that Linux applications are very popular. Extensive. Whether now or in the future, learning Linux is a very good choice. As for whether it is possible to learn from scratch? My answer is of course. Oldboy Education Linux face-to-face class is specially designed for people with zero basic knowledge

Introduction to PHP Basics: How to use the echo function to output text content Introduction to PHP Basics: How to use the echo function to output text content Jul 30, 2023 pm 05:38 PM

Basic introduction to PHP: How to use the echo function to output text content. In PHP programming, you often need to output some text content to a web page. In this case, you can use the echo function. This article will introduce how to use the echo function to output text content and provide some sample code. Before starting, first make sure you have installed PHP and configured the running environment. If PHP is not installed yet, you can download the latest stable version from the PHP official website (https://www.php.net).

Learn to use jQuery to replace tag attributes in web pages in 5 steps Learn to use jQuery to replace tag attributes in web pages in 5 steps Feb 21, 2024 pm 02:24 PM

Learn to use jQuery to replace tag attributes in web pages in 5 steps. jQuery is a popular JavaScript library that can simplify the web development process and provides a series of convenient functions and methods to manipulate DOM elements. This article will introduce how to use jQuery to replace tag attributes in web pages to make web pages more interactive and dynamic. Step 1: Introduce the jQuery library First, introduce the jQuery library into the web page. It can be introduced through a CDN link or the jQuery library file can be downloaded locally.

Learn the basics of Go language variables Learn the basics of Go language variables Mar 22, 2024 pm 09:39 PM

Go language is a statically typed, compiled language developed by Google. Its concise and efficient features have attracted widespread attention and love from developers. In the process of learning the Go language, mastering the basic knowledge of variables is a crucial step. This article will explain basic knowledge such as the definition, assignment, and type inference of variables in the Go language through specific code examples to help readers better understand and master these knowledge points. In the Go language, you can use the keyword var to define a variable, which is the format of the var variable name variable type.

Detailed explanation of C language functions: basic to advanced, comprehensive analysis of the use of functions Detailed explanation of C language functions: basic to advanced, comprehensive analysis of the use of functions Feb 18, 2024 pm 02:25 PM

C language function encyclopedia: from basic to advanced, detailed explanation of how to use functions, specific code examples are required Introduction: C language is a widely used programming language, and its powerful functions and flexibility make it the first choice of many developers. In C language, function is an important concept. It can combine a piece of code into an independent module, improving the reusability and maintainability of the code. This article will introduce the use of C language functions from the basics and gradually advance to help readers master the skills of function writing. 1. Definition and calling of functions in C

PHP study notes: Basics of object-oriented programming PHP study notes: Basics of object-oriented programming Oct 09, 2023 pm 12:46 PM

PHP study notes: Basics of object-oriented programming, specific code examples are required Introduction: Object-Oriented Programming (OOP for short) is a programming way of thinking that decomposes the problem into multiple objects and defines the interaction between objects. to solve complex programming problems. As a powerful programming language, PHP also supports object-oriented programming. This article will introduce the basic concepts and common syntax of object-oriented programming in PHP, and provide specific code examples. kind

See all articles