The basics of Laravel learning

不言
Release: 2023-04-02 10:50:01
Original
2465 people have browsed it

This article mainly introduces the basic knowledge about Laravel learning, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

1.MVC Introduction

The full name of MVC is Model View Controller, which is the abbreviation of Model-View-Controller
Model is the part of the application used to process application data logic
View is the part of the application that processes data display
Controller is the part of the application that handles user interaction

2.laravel core directory file

The basics of Laravel learning

  • app Contains the user's core code

  • booststrap contains framework startup and configuration loading files

  • config contains all configuration files

  • database contains database filling and migration files

  • public contains project entry and static resource files

  • resource contains views and original The resource file

  • stroage contains compiled template files as well as file-based session and file cache, log and framework files

  • tests unit tests File

  • wendor contains dependency files of compose

3. Routing

Multiple request routing

Route::match(['get', 'post']), 'match', funtion() { return 'match'; }); Route::any(['get', 'post']), funtion() { return 'any'; });
Copy after login

Routing parameters

Route::get('user/{name}', funtion($name) { return $id; })->where('name', '[A-Za-z]+'); Route::get('user/{id}/{name?}', funtion($id, $name='phyxiao') { return $id. $name; })->where(['id' => '[0-9]+', 'name'=> '[A-Za-z]+']);
Copy after login

Routing alias

Route::get('user/home', ['as' => 'home', funtion() { return route('home'); }]);
Copy after login

Routing group

Route::group(['prefix' => 'user'], funtion() { Route::get('home', funtion() { return 'home'; }); Route::get('about', funtion() { return 'about'; }); });
Copy after login

Routing output view

Route::get('index', funtion() { return view('welcome'); });
Copy after login

4.Controller

Create control Device

php artisan make:controller UserController php artisan make:controller UserController --plain
Copy after login

Route associated controller

Route::get('index', 'UserController@index');
Copy after login

5. Model

php artisan make:model User
Copy after login

6. Database

Three ways:DB facode Raw lookup,Query BuilderandEloquent ORM
Related filesconfig/database.php,.env

Query constructor

$bool = DB::table('user')->insert(['name => phyxiao', 'age' => 18]); $id = DB::table('user')->insertGetId(['name => phyxiao', 'age' => 18]); $bool = DB::table('user')->insert([ ['name => phyxiao', 'age' => 18], ['name => aoteman', 'age' => 19], ); var_dump($bool);
Copy after login
$num= DB::table('user')->where('id', 12)->update(['age' => 30]); $num= DB::table('user')->increment('age', 3); $num= DB::table('user')->decrement('age', 3); $num= DB::table('user')->where('id', 12)->increment('age', 3); $num= DB::table('user')->where('id', 12)->increment('age', 3, ['name' =>'handsome']);
Copy after login
$num= DB::table('user')->where('id', 12)->delete(); $num= DB::table('user')->where('id', '>=', 12)->delete(); DB::table('user')->truncate();
Copy after login
$users= DB::table('user')->get(); $users= DB::table('user')->where('id', '>=', 12)->get(); $users= DB::table('user')->whereRaw('id >= ? and age > ?', [12, 18])->get(); dd(users); $user= DB::table('user')->orderBy('id', 'desc')->first(); $names = DB::table('user')->pluck('name'); $names = DB::table('user')->lists('name', 'id'); $users= DB::table('user')->select('id', 'age', 'name')->get(); $users= DB::table('user')->chunk(100, function($user){ dd($user); if($user->name == 'phyxiao') return false; });
Copy after login
$num= DB::table('user')->count(); $max= DB::table('user')->max('age'); $min= DB::table('user')->min('age'); $avg= DB::table('user')->avg('age'); $sum= DB::table('user')->avg('sum');
Copy after login

Eloquent ORM

// 建立模型 // app/user.php 
        
Copy after login
// ORM操作 // app/Http/Contollers/userController.php public function orm() { //all $students = Student::all(); //find $student = Student::find(12); //findOrFail $student = Student::findOrFail(12); // 结合查询构造器 $students = Student::get(); $students = Student::where('id', '>=', '10')->orderBy('age', 'desc')->first(); $num = Student::count(); //使用模型新增数据 $students = new Student(); $students->name = 'phyxiao'; $students->age= 18; $bool = $student->save(); $student = Student::find(20); echo date('Y-m-d H:i:s', $student->created_at); //使用模型的Create方法新增数据 $students = Student::create( ['name' => 'phyxiao', 'age' => 18] ); //firstOrCreate() $student = Student::firstOrCreate( ['name' => 'phyxiao'] ); //firstOrNew() $student = Student::firstOrNew( ['name' => 'phyxiao'] ); $bool= $student->save(); //使用模型更新数据 $student = Student::find(20); $student->name = 'phyxiao'; $student->age= 18; $bool = $student->save(); $num = Student::where('id', '>', 20)->update(['age' => 40]); //使用模型删除数据 $student = Student::find(20); $bool = $student->delete(); //使用主见删除数据 $num= Student::destroy(20); $num= Student::destroy([20, 21]); $num= Student::where('id', '>', 20)->delete; }
Copy after login

7.Blade template engine

 @yield('content', '内容')  @section(‘header’) 头部 @show
Copy after login
@extends('layouts') @section(‘header’) @parent header @stop @section(‘content’) content  

{{$name}}

{{ time() }}

{{ date('Y-m-d H:i:s', time()) }}

{{ in_array($name, $arr) ? 'true': 'false' }}

{{ $name or 'default' }}

@{{$name}}

{{--模板注释--}} {{--引入子视图--}} @include('common', ['msg' => 'erro']) {{--流控制--}} @if ($name == 'phyxiao') I'm phyxiao @elseif($name == 'handsome') I'm handsome @else none @endif @unless($name == 'phyxiao') ture @endunless @for($i=0; $i < 10; $i++) {{$i}} @endfor @foreach($students as $student) {{$student->name}} @endfor @forelse($students as $student) {{$student->name}} @empty null @endforelse text text text @stop
Copy after login

The above is the entire content of this article, I hope it will be useful to everyone Learning is helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

laravel’s directory structure

The above is the detailed content of The basics of Laravel learning. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!