目次
1.MVC入門
2.laravel コアディレクトリファイル
3. ルーティング
複数のリクエストのルーティング
ルーティング パラメーター
ルーティング エイリアス
ルーティング グループ
ルーティング出力ビュー
4.コントローラー
コントロールの作成デバイス
ルート関連コントローラー
5.モデル
6.データベース
クエリ コンストラクター
Eloquent ORM
7.Blade テンプレート エンジン

Laravel学習の基礎

Jul 04, 2018 pm 02:14 PM

この記事では、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 サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

11ベストPHP URLショートナースクリプト(無料およびプレミアム) 11ベストPHP URLショートナースクリプト(無料およびプレミアム) Mar 03, 2025 am 10:49 AM

多くの場合、キーワードと追跡パラメーターで散らかった長いURLは、訪問者を阻止できます。 URL短縮スクリプトはソリューションを提供し、ソーシャルメディアやその他のプラットフォームに最適な簡潔なリンクを作成します。 これらのスクリプトは、個々のWebサイトにとって価値があります

Instagram APIの紹介 Instagram APIの紹介 Mar 02, 2025 am 09:32 AM

2012年のFacebookによる有名な買収に続いて、Instagramはサードパーティの使用のために2セットのAPIを採用しました。これらはInstagramグラフAPIとInstagram Basic Display APIです。

Laravelでフラッシュセッションデータを使用します Laravelでフラッシュセッションデータを使用します Mar 12, 2025 pm 05:08 PM

Laravelは、直感的なフラッシュメソッドを使用して、一時的なセッションデータの処理を簡素化します。これは、アプリケーション内に簡単なメッセージ、アラート、または通知を表示するのに最適です。 データは、デフォルトで次の要求のためにのみ持続します。 $リクエスト -

LaravelのバックエンドでReactアプリを構築する:パート2、React LaravelのバックエンドでReactアプリを構築する:パート2、React Mar 04, 2025 am 09:33 AM

これは、LaravelバックエンドとのReactアプリケーションの構築に関するシリーズの2番目と最終部分です。シリーズの最初の部分では、基本的な製品上場アプリケーションのためにLaravelを使用してRESTFUL APIを作成しました。このチュートリアルでは、開発者になります

Laravelテストでの簡略化されたHTTP応答のモッキング Laravelテストでの簡略化されたHTTP応答のモッキング Mar 12, 2025 pm 05:09 PM

Laravelは簡潔なHTTP応答シミュレーション構文を提供し、HTTP相互作用テストを簡素化します。このアプローチは、テストシミュレーションをより直感的にしながら、コード冗長性を大幅に削減します。 基本的な実装は、さまざまな応答タイプのショートカットを提供します。 Illuminate \ support \ facades \ httpを使用します。 http :: fake([[ 'google.com' => 'hello world'、 'github.com' => ['foo' => 'bar']、 'forge.laravel.com' =>

PHPのカール:REST APIでPHPカール拡張機能を使用する方法 PHPのカール:REST APIでPHPカール拡張機能を使用する方法 Mar 14, 2025 am 11:42 AM

PHPクライアントURL(CURL)拡張機能は、開発者にとって強力なツールであり、リモートサーバーやREST APIとのシームレスな対話を可能にします。尊敬されるマルチプロトコルファイル転送ライブラリであるLibcurlを活用することにより、PHP Curlは効率的なexecuを促進します

Codecanyonで12の最高のPHPチャットスクリプト Codecanyonで12の最高のPHPチャットスクリプト Mar 13, 2025 pm 12:08 PM

顧客の最も差し迫った問題にリアルタイムでインスタントソリューションを提供したいですか? ライブチャットを使用すると、顧客とのリアルタイムな会話を行い、すぐに問題を解決できます。それはあなたがあなたのカスタムにより速いサービスを提供することを可能にします

2025 PHP状況調査の発表 2025 PHP状況調査の発表 Mar 03, 2025 pm 04:20 PM

2025 PHP Landscape Surveyは、現在のPHP開発動向を調査しています。 開発者や企業に洞察を提供することを目的とした、フレームワークの使用、展開方法、および課題を調査します。 この調査では、現代のPHP Versioの成長が予想されています

See all articles