Laravel学習の基礎

不言
リリース: 2023-04-02 10:50:01
オリジナル
2508 人が閲覧しました

この記事では、Laravel学習に関する基礎知識を中心に紹介しており、ある程度の参考価値があるので、皆さんにも共有しておきますので、困っている友人は参考にしてください

1.MVC入門

MVC の完全名は Model View Controller で、Model-View-Controller の略称です。
Model は、アプリケーション データ ロジックを処理するために使用されるアプリケーションの一部です。
View は、アプリケーションの一部です。プロセスデータ表示
Controller はユーザー操作を処理するアプリケーションの一部です

2.laravel コアディレクトリファイル

Laravel学習の基礎

  • app ユーザーのコア コードが含まれます

  • booststrap にはフレームワークの起動ファイルと設定ロード ファイルが含まれます

  • config にはすべての設定ファイルが含まれます

  • database にはデータベースの入力ファイルと移行ファイルが含まれます

  • public にはプロジェクト エントリと静的リソース ファイルが含まれます

  • resource にはビューが含まれますリソース ファイル

  • stroage には、コンパイルされたテンプレート ファイル、ファイルベースのセッション、ファイル キャッシュ、ログ、およびフレームワーク ファイル

  • tests が含まれています。単体テスト ファイル

  • wendor には compose の依存関係ファイルが含まれています

3. ルーティング

複数のリクエストのルーティング

Route::match(['get', 'post']), 'match', funtion()
{
    return 'match';
});
Route::any(['get', 'post']),  funtion()
{
    return 'any';
});
ログイン後にコピー

ルーティング パラメーター

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');
}]);
ログイン後にコピー

ルーティング グループ

Route::group(['prefix' => 'user'], funtion()
{
    Route::get('home', funtion()
   {
    return 'home';
   });
    Route::get('about', funtion()
   {
    return 'about';
   });
});
ログイン後にコピー

ルーティング出力ビュー

Route::get('index',  funtion()
{
    return view('welcome');
});
ログイン後にコピー

4.コントローラー

コントロールの作成デバイス

php artisan make:controller UserController
php artisan make:controller UserController --plain
ログイン後にコピー

ルート関連コントローラー

Route::get('index',  'UserController@index');
ログイン後にコピー

5.モデル

php artisan make:model User
ログイン後にコピー

6.データベース

3つの方法:DBファコード生のルックアップQuery Builder および Eloquent ORM
関連ファイルconfig/database.php.env

クエリ コンストラクター

$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();
ログイン後にコピー
$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');
ログイン後にコピー

Eloquent ORM

// 建立模型
// 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;

}
ログイン後にコピー

7.Blade テンプレート エンジン

<!--展示某个section内容 占位符-->
@yield(&#39;content&#39;, &#39;内容&#39;)
<!--定义视图片段-->
@section(‘header’)
头部
@show
ログイン後にコピー
@extends(&#39;layouts&#39;)
@section(‘header’)
    @parent
    header
@stop
@section(‘content’)
    content
    <!--模板输出php变量-->
    <p>{{$name}}</p>
    <!--模板调用php代码-->
    <p>{{ time() }}</p>
    <p>{{ date(&#39;Y-m-d H:i:s&#39;, time()) }}</p>

    <p>{{ in_array($name, $arr) ? &#39;true&#39;: &#39;false&#39; }}</p>
    <p>{{ $name or &#39;default&#39; }}</p>

    <!--原样输出-->
    <p>@{{$name}}</p>

    {{--模板注释--}}

    {{--引入子视图--}}
    @include(&#39;common&#39;, [&#39;msg&#39; => &#39;erro&#39;])

    {{--流控制--}}
    @if ($name == &#39;phyxiao&#39;)
        I&#39;m phyxiao
    @elseif($name == &#39;handsome&#39;)
        I&#39;m handsome
    @else
        none
    @endif

    @unless($name == &#39;phyxiao&#39;)
        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

    <a herf = "{{url(&#39;url&#39;)}}">text</a>
    <a herf = "{{action(&#39;UserController@index&#39;)}}">text</a>
    <a herf = "{{route(&#39;url&#39;)}}">text</a>

@stop
ログイン後にコピー

上記がこの記事の全内容です。誰にとっても役立ちます 学習は役に立ちます 関連コンテンツについては、PHP 中国語 Web サイトに注目してください。

関連する推奨事項:

laravel のディレクトリ構造

以上がLaravel学習の基礎の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!