Laravel學習筆記

2021-08-05 19:20:49 字數 2558 閱讀 2527

在子頁面中可以使用@parent指令來顯示模板頁上的內容,前提是在布局模板中使用@section指令用來指定覆蓋的區域

@section('sidebar')

this is the master sidebar.

@show

@yield('content')

@extends('layouts.master')

@section('title', 'page title')

@section('sidebar')

@parent

@endsection

@section('content')

this is my body content.

@endsection

並沒有覆蓋掉,只覆蓋了@parent位置處

在使用@include指令引入乙個檢視,所引入檢視中的變數等,在本檢視中依然有效

@include('shared.errors')

儘管被包含的檢視繼承所有父檢視中的資料,還可以傳遞額外引數到被包含的檢視:

@include('view.name', ['some' => 'data'])
注:不要在 blade 檢視中使用__dir____file__常量,因為它們會指向快取檢視的路徑。

@}不使用}進行解析。主要針對有些前端框架使用的也是}

}表示如果給定的值($name)沒有的話,使用預設值(default)

}用法同上,三目運算

@unless 除非的意思。除非是怎麼樣,否則的話才會輸出

例:

@unless($score > 60)

不及格@endunless

如果分數大於60的話不執行裡面的語句,否則的話才會輸出。如果分數大於60不輸出內容,小於60輸出不及格

對**會進行轉義,`}不會對**進行轉義

比如有乙個變數,儲存的是一段html標籤或者js,那麼}會直接輸出這些進行顯示,而會把它當作html進行解析

例:在乙個控制器內

$name = 'hello word';

return view('index')->with('name',$name);

那麼,在檢視裡,輸出的是紅色的 hello word,而}輸出的是hello word

1、 使用php artisan make:model --migration post

id }}">
id) }}">
id]) }}">
例:

class article extends model

}

上面這個例子表示在存入資料庫之前,對published_at欄位進行處理。把接收到的時間欄位給處理後,在存入資料庫

前面的set和後面的attribute為關鍵字,中間的欄位名為駝峰寫法

例:

//controller裡使用

$articles = article::latest()->published()->get(); //使用了自定義的published方法

//model裡

public function scopepublished($query) //$query 表示傳入進來的查詢語句

注意scope為關鍵字,後面方法名為駝峰法,第乙個字母大寫,需要接收查詢語句擦拭,但是不需要再使用的時候傳遞。

如果想把自己定義的時間字段作為carbon物件進行使用的話,需要在控制器內定義乙個屬性 $dates

然後把欄位名賦值給 $dates

例:

class article extends model

carbon::now() 表示輸出當前時間

自動生成的這個欄位不屬於普通的時間,而是作為一種carbon物件來儲存的

路由裡的接收的引數可以使用正則進行過濾,使用正則可以匹配給定過來的引數是否符合規則

例:

route::get('user/', function ($name) )->where('name', '[a-za-z]+');
路由檔案裡為什麼每條路由寫一次,這樣有什麼好處?

每條路由寫一次利於管理,特別在乙個請求比較多的情況下可以知道每個檔案的請求。

在重構的時候也可以很方便的知道哪些是沒用可以刪掉的請求。

laravel學習筆記之路由

路由 寫在http下的routes.php中 基礎路由 route get basic1 function route post basic2 function 多請求路由 match 註冊乙個指定的多路由請求 route match get post multy1 function any 註冊乙...

laravel學習篇 基礎篇

route get function route get hello function route post color function route match get post color1 function route any multy function 路由引數 route get use...

Laravel 學習路線 4 控制器

控制器簡介定義控制器 namespace useuse class usercontroller extends controller 我們可以這樣定義路由 route get user usercontroller show 如果乙個請求匹配到上面uri usercontroller 的 show...