laravel總結(jié)

1.指定目錄創(chuàng)建資源型控制器并綁定模型

php artisan make:controller Admin/ArticleController --resource --model=\\Models\\Article

2.這個(gè)就是創(chuàng)建一個(gè)ArticelModel模型到/App/Model目錄下

php artisan make:model ./Model/ArticleModel

3.創(chuàng)建控制器到/App/Http/Controller/api目錄下

php artisan make:controller ./api/ArticleModel

4.創(chuàng)建遷移,表名中不需要加入表前輟:

php artisan make:migration 表名

5.運(yùn)行所有未完成的遷移:

php artisan migrate

6.查詢加入withTrashed,可以查詢已經(jīng)軟刪除的數(shù)據(jù)

$article::withTrashed()->find($navigationid);

7.萬能路由:

Route::prefix('admin')->namespace('Admin')->middleware(['web', 'checkLogin', 'checkRule'])->group(function () {

? ? Route::get('index', 'IndexController@index');

//? ? Route::get('index/main', 'IndexController@main');

//? ? Route::get('category/index', 'CategoryController@index');

? ? //萬能路由

? ? Route::any('{controller}/{action}', function ($class, $action) {

? ? ? ? $class = 'App\\Http\\Controllers\\Admin\\' . ucfirst(strtolower($class)) . 'Controller';

? ? ? ? if (!class_exists($class)) {

? ? ? ? ? ? $class = 'App\\Http\\Controllers\\Admin\\EmptyController';

? ? ? ? }

? ? ? ? $ctrl = \App::make($class);

? ? ? ? return \App::call([$ctrl, $action]);

? ? })->where(['class' => '[0-9a-zA-Z]+', 'action' => '[0-9a-zA-Z]+']);

});

8.laravel獲取當(dāng)前訪問控制器和方法名稱

$action = \Route::current()->getActionName();

list($class, $method) = explode('@', $action);

$controller = substr(strrchr($class, '\\'), 1);

var_dump($controller);var_dump($method);

9.輸出sql調(diào)試語句

use DB;//引入DB

DB::connection()->enableQueryLog();#開啟執(zhí)行日志

$allModelAuth = $datamodel::where(['publish' => ENV('PUBLISH_ON'), 'pid' => ['neq', 0]])->get('href')->toarray();

print_r(DB::getQueryLog());? //獲取查詢語句、參數(shù)和執(zhí)行時(shí)間

10.aravel在一個(gè)控制器中使用另一個(gè)控制器中函數(shù)的2個(gè)方法

方法1:直接繼承改控制器,使用$this 調(diào)用

方法2:使用use引入該控制器,實(shí)例化一個(gè)變量upimg,然后使用 upimg調(diào)用該方法

use App\Http\Controllers\admin\DictionaryController;//use引入控制器

$dictionary = new DictionaryController; //實(shí)例化變量

$data = $dictionary->cache();

11.laravel 中將DB::select 得到的內(nèi)容轉(zhuǎn)為數(shù)組

$sql = "select count(*) as num from api_log where uid='{$this->uid}'";

$data = DB::select($sql);

$data = array_map('get_object_vars', $data);

12.laravel Eloquent ORM 字段自增與自減

laravel Eloquent ORM提供了兩個(gè)方法來實(shí)現(xiàn)自增與自減,這兩個(gè)方法都至少接收一個(gè)參數(shù):需要修改的列。第二個(gè)參數(shù)是可選的,用于控制列值增加/減少的數(shù)目。

$Model->increment('votes');

$Model->increment('votes', 5);

$Model->decrement('votes');

$Model->decrement('votes', 5);

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容