laravel記錄
1.使用數據庫事務的時候需要傳遞參數,使用了use關鍵字,否則的話傳遞不進去,代碼如下:
public function postVote(Request $request){ $data = $request->all(); $count = DB::table("vote")->where("workdid",$data['id'])->where("ip", $data['ip'])->count(); if(DB::table("vote")->where("workdid",$data['id'])->where("ip", $data['ip'])->count()>0){ $ret = ['state' => 'fail', 'message' => '您已經給這個作品投過一票!']; }else{ DB::transaction(function () use($data){ DB::table("work")->where("id", $data["id"])->increment("vote"); DB::table("vote")->insert(["workdid"=>$data["id"], "ip"=>$data['ip']]); }); $ret = ['state' => 'success', 'message' => '投票成功']; } return response()->json($ret); }
2.分頁的時候需要把參數帶進去,使用appends()方法,還要顯示總共多少條記錄,代碼如下,前提是$list是查詢構建起調用paginate()方法返回的結果
<div class="summary"> 共有 {{$list->total()}} 條數據 </div> @if ($list->hasPages()) <div class="pager"> {!! $list->appends($appends)->render() !!} </div> @endif
3.向主頁面中追加內容的時候,主頁面內容如下
@section('sidebar')
This is the master sidebar.
@show
子頁面內容如下:
@section('sidebar')
@parent
<p>This is appended to the master sidebar.</p>
@endsection
注意主頁面@section的結束語句是@show,不是@endsection,同時子頁面中使用@parent表明是追加的內容
4.很多javascript框架使用{{}}來表示要輸入到瀏覽器中的表達式,可以使用@告訴blade引擎該表達式保持原生格式不做改動例如:
<h1>Laravel</h1>
Hello, @{{ name }}.
就是說如果要使用javascript框架中使用到{{}},那么前面要加@
5.Laravel5路由\home 無法訪問
在apache配置文件中將所有的 AllowOverride none;設置為AllowOverride all;配置文件具體位置因系統不同而改變,ubuntu14.0.1LTS 版本在/etc/apache2/apache2.conf文件中
6.部署好之后500錯誤
安裝完laravel后,打開馬上出現了500錯誤,配置都是正確的,但是出現了500錯誤
------------------>`500 (Internal Server Error)`
要給緩存文件設置777權限,如下
chmod -R 777 storage (給storage 777權限)
7.僅能有一個AI
有一次使用php artisan migrate ------->結果報錯了
原因是主鍵id是AI,而設置一個外鍵xx_xx_id是integer類型,這就沖突了,解決方法只需將xx_xx_id改為unsigned integer類型
(像這樣的$table->integer('role_id')->unsigned();)
8.郵件發送530錯誤
郵箱發送出現了如下問題:Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required,
從錯誤嗎中看出是配置錯誤,但是我們檢查了幾次都是正確的,原因是緩存問題,這時候可以
清除緩存php artisan cache:clear或重啟服務器php artisan serv
9.使用create插入數據錯誤
如果使用create出現MassAssignmentException in Model.php line 448
從錯誤中看出是模型中沒有定義白名單字段,解決如下:
class Contacts extends Model { protected $fillable = ['name', 'mobile', 'email', 'address', 'created_at', 'updated_at']; }
10.compose update報錯
錯誤為:Fatal error: Uncaught exception 'ReflectionException' with message
解決方法可能如下:
1. composer dump-autoload
2. 配置文件錯誤,需修改,如我安裝時改了配置文件.env,然后報錯,這時候還原.env 就正常了
11.默認情況下刀片語法的{{}}已經通過php的htmlentitys函數處理避免XSS攻擊,如果不想數據被處理,顯示原生的數據可以用下面的方法
Hello, {!! $name !!}.
12.運行命令php artisan migrate莫名其妙的錯誤
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('slug')->unique();
$table->string('title');
$table->text('content');
$table->timestamps();
$table->timestamps('updated_at')->index();
});
這樣會報錯:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Fatal error: Call to a member function index() on null
改成下面就好了
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('slug')->unique();
$table->string('title');
$table->text('content');
$table->timestamps();
$table->timestamps('published_at')->index();
});
FILE->Setting->Editor->File Encoding->將UTF-8改成GBK

14.laravel命令集合
1)查看應用中所有的路由:php artisan route:list
2)創建控制器: php artisan make:controller BlogController --plain --plain參數表示命令用于創建一個空的控制器而不是標準的 RESTful 風格控制器
3)創建model: php artisan make:model --migration Post 創建Post模型并創建遷移文件
4)創建中間件:php artisan make:middleware OldMiddleware 創建一個OldMiddleware的中間件
15.定義一個token,媽蛋記不住
{'_token': '{{ csrf_token() }}', 'dir': 'product'}
{{ csrf_field() }}
<meta name="csrf-token" content="{{ csrf_token() }}">
$.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') } });
16.使用captcha驗證碼第三方插件的時候驗證隨機數是否正確,寫法如下
$data = $request->all(); $attributes = [ 'name'=>'用戶名', 'password'=>'密碼', 'captcha' =>'驗證碼' ]; $validator = \Validator::make( $data, [ 'name' => 'required', 'password' => 'required', 'captcha' => 'required|captcha', ], [ 'required' => ":attribute不能為空", 'captcha.required' => '請輸入驗證碼', 'captcha.captcha' => '驗證碼錯誤,請重試' ], $attributes );
17.發送手機驗證碼的時候使用validator驗證手機驗證碼是否正確
發送驗證碼代碼如下
public function postValidatecode(Request $request){ $data = $request->all(); $code = rand(1000, 9999); session([config('session.session_key.register')=>$code]); $content = "您好,您本次注冊的驗證碼是" . $code . ',請妥善保管。'; $result = sendsms($data['mobile'], $content); return \Response::json($result); }
驗證這個驗證碼是否正確如下
$data = $request->all(); $attributes = [ 'name'=>'用戶名', 'password'=>'密碼', 'password_confirmation'=>'確認密碼', 'mobile'=>'手機號', 'idNo'=>'身份證號', 'validateCode'=>'驗證碼', ]; $rules = [ 'name'=>'required|unique:users', 'password' => array('bail', 'required', 'confirmed', 'regex:/^\w{6,15}$/'), 'password_confirmation' => 'required', 'mobile' => array('required','regex:/^1[3|4|5|6|7|8|9]\d{9}$/', 'unique:users'), 'validateCode'=>array('required', 'validatesmsg:register'), 'idNo'=> array('required','regex:/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X|x)$/i'), 'agree' => 'required', ];
app\Providers\AppServiceProvider.php中的擴展方法如下,原理很簡單就是驗證這個session是否相等
class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { // Validator::extend('validatesmsg', function($attribute, $value, $parameters){ return $value == session(config('session.session_key.'.$parameters[0])); }); } /** * Register any application services. * * @return void */ public function register() { // } }
作者:Tyler Ning
出處:http://www.rzrgm.cn/tylerdonet/
本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,如有問題,請微信聯系冬天里的一把火
浙公網安備 33010602011771號