Laravel Getting Started notes

Source: Internet
Author: User

Laravel is a simple, elegant frame that can be said to be the second choice after the introduction TP.

Catalog section:

    • Code written by App--
    • Controller, HTTP
    • Bootstrap-Frame self-loading files
    • Config--config file
    • Database--About databases
    • Public-Static files
    • Resources--View
    • Storage, compiling static files
    • Tests for testing code

The file that is accessed by default is: public->index.php

Routing:

Storage path: app/http/routes.php

TP access mode is User access--controller->model-> view

Laravel access Mode user access--routing--controller->model-> view

In a nutshell, Laravel access to the various controllers through the road

    

Type: (2 of the commonly used types)
Get:route::get (' Basic1 ', function () {

Return ' Hello world ';

});

Post:route::p ost (' Basic2 ', function () {

Return ' Hello World2 ';

});//cannot be accessed directly by URL

  

Multi-Request Routing
Match:route::match ([' Get ', ' post '], ' multy ', function () {

Return ' Hello World3 ';

});//can be accessed either by get or post, that is, the specified way of request.

Any:route::any (' Matrh ', function () {

Return ' Hello World4 ';

});//respond to all HTTP requests.

  

Route parameters
Get:route::get (' User/{id} ', function ($id) {

Return ' user-id-'. $id;

});

Default value
Route::get (' user/{name} ', function ($name = ' sean ') {

Return ' user-name-'. $name;

});


Judging with regular expressions
Route::get (' user/{name} ', function ($name) {

Return ' user-name-'. $name;

})->where (' name ', ' [a-za-z]+ ');//where (' to match regular Fields ', ' regular rules, with [] contains ')


Judging multiple fields with regular
Route::get (' user/{id}/{name} ', function ($id, $name) {

Return ' user-id-'. $id. ' user-name-'. $name;

})->where ([' id ' = ' = ' [0-9]+ ', ' name ' = ' [a-za-z]+ ']);

Route aliases
Route::get (' User/member-user ', [' as ' = ' member ', function ()} {

Return ' Member-user ';

}]);

Routing groups
Route::group ([' prefix ' = ' member ', function ()} {
Route::get (' user/{name} ', function ($name) {
Return ' user-'. $name;
});

Rount::get (' User/{id} ', function ($id) {
Return ' id = '. $id;
})
}]);//access to routes within the routing group via the member prefix, for example:
/* Originally a Http://localhost/Laravel/laravel52/public/user/name
Add a prefix and become http://localhost/Laravel/laravel52/public/menber/user/name
You can also access
*/

Output view in route
Route::get (' View ', function () {
Return view (' Welcome ');
});

Summary: The code in the project cannot be written here. Routing is used only to accept requests and to process them into methods in the controller.

  

Controller
Path: app/httpcontrollers

Create a new controller
1. As membercontroller.php as TP
2.namespace app\http\controllers;
3. Write a class
Eg:class Membercontroller extends Controller
{
Public Function info ()
{
Return ' Member-info ';
}
}

Routing and Controller affinity
Route::get (' Member/info ', ' [email protected] ')//route::get (' Controller name/controller method name ', ' Controller name controllers @ Controller method ');

The second way:
Route::get (' Member/info ', [' uses ' = ' [email protected] ']);

Controller with parameters
Route::any (' Member/{id} ', [' uses ' = ' [email protected] ');
Inside the Controller method:
Public Function info ($id)
{
return $id;
}

When you want to limit the parameter type
Route::any (' Member/{id} ', [' uses ' = ' = ' [email protected] ')->where (' id ', ' [0-9]+ ');

View
Path: Resources/views

Output view
Public Function info ()
{
Return view (' Directory name/filename ');
}

The premise is to build this file in the view layer.
Naming rules: file names. blade.php;

Generally a controller corresponds to a directory, if the controller is membercontroller.php set up a member Directory, the controller of the view to be stored in this directory

The above summary: same as TP

With variables in the controller
Public Function Inin ()
{
Return view (' Menber ', [
' Name ' = ' Chen Yaqing ',
' Age ' = ' old Not dead '
]);
}
Output variables in templates
{{$name}} {{$age}}

Model
Path: App

Create a new model:
namespace App;

Use Illuminate\database\eloquent\model;

Class Member extends Model
{
public static function GetMember ()
{
Return ' Sean ';
}
}

Using in the Controller
1. At the start position
Use app/model name;

2. Because the model above is defined by a static method
So call time: Model Name:: The method name inside the model ();

manipulating databases

Three different ways
DB Facade (Original lookup)
Query Builder
Eloquent ORM
Path:
config/database.php
' Prefix ' = ' table prefix ' (no ignorable)
corresponding to the. env file
Modify Database Configuration
Vendor/.env
Db_connection=mysql
db_host=127.0.0.1//server address
db_port=3306//Port number
db_database=homestead//database name
db_username=homestead//User Name
db_password=secret//Password

DB: Native Statement query
$res = Db::select (' SELECT * from goods '); DD ($res);(equivalent to dump () in TP;
Increase
$res = Db::insert (' INSERT into goods (name, age) VALUES (?,?) ', [' Sean ', 18]);
Change
$res = db::update (' update goods set age =? WHERE name =? ', [' + ', ' Sean ']);
Delete
$res = DB::d elete (' Delete from goods where id =? ', [' 1001 ']);

Query Builder (emphasis) uses built-in methods to make database deletions

  

Brief introduction:
Provides a convenient, fluent interface for building and performing database operations
Use the PDO parameter bindings to protect your application from SQL injection so passing in data does not require additional escaping of special strings
Basically, all database operations can be satisfied.

To access this data method, you need to add a route, enter the URL of the access to call this route access to the controller inside the method.

Increase
Db::table (' goods ')->insert ([
[' Name ' = ' name1 ', ' age ' = 18],
[' Name ' = ' name2 ', ' age ' = 12]
]);

Db::table (' goods ')->insertgetid ([
[' Name ' = ' name1 ', ' age ' = 18],
[' Name ' = ' name2 ', ' age ' = 12]
]);//Get to the last inserted ID

Modify
Db::table (' goods ')->where (' id ', 2)->update ([' age ' = 13]);//change goods's id=2 to age=13;
Self-increment
Db::table (' goods ')->where (' id ', 2)->increment (' Age ', 3);//Add id=2 to 3 years old, without data default self-increase one year old
Self-reduction
Db::table (' goods ')->where (' id ', 3)->decrement (' Age ', 3)///id=3 is 3 years old, no data added by default self-increase one year old
Change other data when self-increment/decrement
Db::table (' goods ')->where (' id ', 2)->increment (' Age ', 3, [' Name ', ' II ']);//Add name to II at the same time
Delete
Db::table (' goods ')->where (' id ', ' >= ', 3)->delete ();//delete id>=3 data
Db::table (' goods ')->truncate ();//clear database, use sparingly

Inquire
Get method: $res = db::table (' goods ')->get ();//Get all the data for the table
DD ($res);//laravel Printing method

First Order method: $res = db::table (' goods ')->first ();//order to get a table of data

First Reverse method: $res = db::table (' goods ')->orderby (' id ', ' desc ')->first ();//Flashback gets a data for the table

Where method: $res = db::table (' goods ')->where (' id ', ' <= ', 1001)->get ();//Get Table data conditionally

Where multiple conditional methods: $res = db::table (' goods ')->where (' ID >=? and age >=? ', [' 1001 ', ') ')->get ();//Get table data on multiple criteria

Pluck returns a field: $name = db::table (' goods ')->pluck (' name ');//returns only the value of the field name

Lists returns a field: $name = db::table (' goods ')->lists (' name ', ' ID ');//return only the value of the Name field, and you can specify a subscript

Select Specifies the field of the query: $name = db::table (' goods ')->select (' name ', ' ID ')->get ();//query only the name, the value of the ID field

The chunk limit returns several times at a time: $name = db::table (' goods ')->chunk (2, function ($goods) {
DD ($goods);
Return false;//, with this condition, is only queried once,
});//query only two at a time, until the end of the check

Laravel Getting Started notes

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.