1.單行為控制器
//控制器
public function __invoke(){
//
}
//路由
Route::get('demo19', 'Test\TestController');
2.路由回退
//沒有找到會運行此路由
//放到最底部
Route::fallback(function () {
return redirect('/');
});
3.路由資訊
//當前路由資訊
dump(Route::current());
//當前路由的別名
echo Route::currentRouteName();
//當前路由指向的方法
echo Route::currentRouteAction();
4.資源控制器
| HTTP請求方式 | URL | 控制器方法 | 路由命名 | 業務邏輯描述 |
|---|---|---|---|---|
| GET | post | index() | post.index | 展示所有文章 |
| GET | post/create | create() | post.create | 發布文章表單頁面(api無) |
| POST | post | store() | post.store | 獲取表單提交資料并保存新文章 |
| GET | post/{id} | show() | post.show | 展示單個文章 |
| GET | post/{id}/edit | edit() | post.edit | 編輯文章表單頁面(api無) |
| PUT | post/{id} | update() | post.update | 獲取編輯表單輸入并更新文章 |
| DELETE | post/{id} | destroy() | post.destroy | 洗掉單個文章 |
// php artisan make:controller UserController --resource
// php artisan make:controller UserController --api
//單個資源路由
Route::resource('api', 'Test\ApiController');
//api資源路由
Route::apiResource('api', 'Test\ApiController');
//批量資源路由
Route::resources([
'api'=>'Test\ApiController'
]);
5.資料庫分塊操作
//再有一萬條資料的時候 每次取出100條操作
//where條件下的update會有坑
$result = DB::table('uinfo')
->orderBy('id')
->chunk(100, function ($result) {
foreach ($result as $item) {
$item->username = $item->username . '###22';
}
});
6.資料庫判斷資料是否存在
//回傳 true
DB::table('uinfo')->where('id', 19)->exists();
//回傳 false
DB::table('uinfo')->where('id', 19)->doesntExist()
7.資料庫select查詢
//1.addSelect
//給已經構建好的查詢添加更多欄位
$base = DB::table('uinfo')->select('username', 'password');
$result = $base->addSelect('updated_at')->get();
//2.DB::raw()
//select()內部實作原生運算式
$result = DB::table('uinfo')->select(DB::raw('count(*) as num'))
->groupBy('username')
->get();
//3.selectRaw
//selectRaw()內部實作原生運算式
$result=DB::table('uinfo')
->groupBy('username')
->selectRaw('count(*) as num,username')
->get();
8.updateOrInsert 存在修改,不存在新增
$result = DB::table('uinfo')->updateOrInsert(
['id' => 15],
['username' => 'demo14的測驗1', 'password' => '123456', 'updated_at' => $time, 'created_at' => $time]
);
9.Laravel增加代碼提示
composer require barryvdh/laravel-ide-helper
php artisan ide-helper:generate – 為Facades 生成注釋
php artisan ide-helper:models – 為資料模型生成注釋
php artisan ide-helper:meta – 生成PhpStorm Meta file
10.模型作用域:本地作用域
//模型中 前面需要使用scope
public function scopeCheckId($query, $index)
{
return $query->where('id', '>', $index);
}
//控制器
$result = Uinfo::checkId(5)->get();
11.模型作用域:全域作用域
//創建類
namespace App\Scopes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;
class NameScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
// TODO: Implement apply() method.
return $builder->where('id', '>', 6);
}
}
//2.開啟全域作用域
protected static function boot()
{
parent::boot(); // TODO: Change the autogenerated stub
static::addGlobalScope(new NameScope());
}
12.模型訪問器
//訪問器 前:get 中:欄位名 后:Attribute
public function getUsernameAttribute($value)
{
return '[' . $value . ']';
}
//虛擬欄位
protected $appends = ['info'];
public function getInfoAttribute()
{
return '##'.$this->password;
}
13.模型修改器
public function setPasswordAttribute($value)
{
$this->attributes['password'] = '&&' . $value;
}
14.模型預加載 with
//Ucard模型
public function be_user()
{
return $this->belongsTo(Uinfo::class, 'u_id', 'id');
}
//控制器
$result = Ucard::with('be_user')->get();
$result = Ucard::with(['be_user' => function ($query) {
$query->where('id', '>', 5);
}])->get();
15.模型關聯(一對多)增刪改
//Uinfo模型
public function userCard()
{
return $this->Hasone(Ucard::class, 'u_id', 'id');
}
//控制器
//save
$result = Uinfo::find(15)->userCard()->save(new Ucard(['card' => '15646484']));
//create
Uinfo::find(15)->userCard()->create(['card' => 2525]);
//update
Uinfo::find(15)->userCard()->update(['card' => '修改']);
//delete
Uinfo::find(9)->userCard()->delete();
16.模型關聯(多對多)增刪改
//Article模型
public function rel_article()
{
return $this->belongsToMany(Keyword::class, 'a_k', 'a_id', 'k_id');
}
//控制器
$k_id = 6;
//attach 增加 a_id=1,k_id=6
Article::find(1)->rel_article()->attach($k_id, ['remarks' => '多對多增加']);
//sync 唯一新增 不存在新增(會洗掉之前)
Article::find(1)->rel_article()->sync($k_id);
//detach 洗掉 a_id=1,k_id=6
Article::find(1)->rel_article()->detach($k_id);
//updateExistingPivot 修改
Article::find(1)->rel_article()->updateExistingPivot(3, ['remarks' => '修改']);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/248577.html
標籤:其他
上一篇:為什么在 React 16 版本中 render 階段放棄了使用遞回?
下一篇:軟體架構-可視化
