Laravel 학습의 기본

不言
풀어 주다: 2023-04-02 10:50:01
원래의
2508명이 탐색했습니다.

이 글은 주로 Laravel 학습에 대한 기본 지식을 소개하며, 이는 어느 정도 참고할만한 가치가 있습니다. 이제 도움이 필요한 친구들이 참고할 수 있도록 하겠습니다.

1.MVC 소개#🎜🎜 #

MVC의 전체 이름은 Model View Controller, 즉 model-view-controller의 약자입니다.

모델은 애플리케이션의 데이터 로직을 처리하는 데 사용되는 부분입니다
View는 데이터 표시를 처리하는 애플리케이션의 일부입니다
Controller는 사용자 상호 작용을 처리하는 애플리케이션의 일부입니다

2.laravel 코어 디렉터리 파일

#🎜 🎜##🎜 🎜#

Laravel 학습의 기본

app에는 사용자의 핵심 코드가 포함되어 있습니다.
  • booststrap에는 프레임워크 시작 및 구성이 포함되어 있습니다. 파일 로드 중# 🎜🎜#
  • config에는 모든 구성 파일이 포함되어 있습니다.

  • 데이터베이스에는 데이터베이스 채우기 및 마이그레이션 파일이 포함되어 있습니다.

    #🎜 🎜#
  • public에는 프로젝트 항목 정적 리소스 파일이 포함되어 있습니다.

  • resource에는 뷰와 원본 리소스 파일이 포함되어 있습니다.

  • stroage에는 컴파일된 템플릿 파일과 파일 기반 세션 및 파일 캐시, 로그 및 프레임워크 파일이 포함되어 있습니다.

  • 테스트 단위 테스트 파일#🎜 🎜#

  • wendor에는 compose
  • 3의 종속성 파일이 포함되어 있습니다. ###
    Route::match(['get', 'post']), 'match', funtion()
    {
        return 'match';
    });
    Route::any(['get', 'post']),  funtion()
    {
        return 'any';
    });
    로그인 후 복사
    ###라우팅 매개 변수#####rreee###라우팅 별명###
    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]+']);
    로그인 후 복사
    ###라우팅 그룹###
    Route::get('user/home',  ['as' => 'home', funtion()
    {
        return route('home');
    }]);
    로그인 후 복사
  • 라우팅 출력보기#########

    4.Controller

  • 컨트롤러 생성
Route::group(['prefix' => 'user'], funtion()
{
    Route::get('home', funtion()
   {
    return 'home';
   });
    Route::get('about', funtion()
   {
    return 'about';
   });
});
로그인 후 복사

경로 관련 컨트롤러

Route::get('index',  funtion()
{
    return view('welcome');
});
로그인 후 복사

5.모델

php artisan make:controller UserController
php artisan make:controller UserController --plain
로그인 후 복사

6. 데이터베이스

세 가지 방법:

DB facode 원본 검색

,

Query builder

및 #🎜 🎜#Eloquent ORM#🎜 🎜#

관련 파일

config/database.php

,

.env

쿼리 생성자

Route::get('index',  'UserController@index');
로그인 후 복사
php artisan make:model User
로그인 후 복사
$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);
로그인 후 복사
$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']);
로그인 후 복사
$num= DB::table('user')->where('id', 12)->delete();
$num= DB::table('user')->where('id', '>=', 12)->delete();
DB::table('user')->truncate();
로그인 후 복사

Eloquent ORM
$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;
});
로그인 후 복사
$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');
로그인 후 복사

7.Blade 템플릿 엔진

// 建立模型
// app/user.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
    //指定表名
    protected $table = &#39;user&#39;;
    //指定id
    protected $primaryKey= &#39;id&#39;;
    //指定允许批量赋值的字段
    protected $fillable= [&#39;name&#39;, &#39;age&#39;];
    //指定不允许批量赋值的字段
    protected $guarded= [];
    //自动维护时间戳
    public $timestamps = true;

    protected function getDateFormat()
    {
        return time();
    }
    protected function asDateTime($val)
    {
        return val;
    }
}
로그인 후 복사
// 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(&#39;id&#39;, &#39;>=&#39;, &#39;10&#39;)->orderBy(&#39;age&#39;, &#39;desc&#39;)->first();
    $num = Student::count();


    //使用模型新增数据
    $students = new Student();
    $students->name = &#39;phyxiao&#39;;
    $students->age= 18;
    $bool = $student->save();

    $student = Student::find(20);
    echo date(&#39;Y-m-d H:i:s&#39;, $student->created_at);


    //使用模型的Create方法新增数据
    $students = Student::create(
        [&#39;name&#39; => &#39;phyxiao&#39;, &#39;age&#39; => 18]
    );
    //firstOrCreate()
    $student = Student::firstOrCreate(
        [&#39;name&#39; => &#39;phyxiao&#39;]
    );
    //firstOrNew()
    $student = Student::firstOrNew(
        [&#39;name&#39; => &#39;phyxiao&#39;]
    );
    $bool= $student->save();


    //使用模型更新数据
    $student = Student::find(20);
    $student->name = &#39;phyxiao&#39;;
    $student->age= 18;
    $bool = $student->save();

    $num = Student::where(&#39;id&#39;, &#39;>&#39;, 20)->update([&#39;age&#39; => 40]);


    //使用模型删除数据
    $student = Student::find(20);
    $bool = $student->delete();
    //使用主见删除数据
    $num= Student::destroy(20);
    $num= Student::destroy([20, 21]);

    $num= Student::where(&#39;id&#39;, &#39;>&#39;, 20)->delete;

}
로그인 후 복사
위 내용은 이 기사의 전체 내용입니다. 모든 사람의 학습에 도움이 되기를 바랍니다. 도움이 필요하시면 PHP 중국어 웹사이트에서 더 많은 관련 콘텐츠를 확인하세요! 관련 권장 사항:
laravel의 디렉터리 구조

위 내용은 Laravel 학습의 기본의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!