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 |
删除单个文章 |
Route::resource('api', 'Test\ApiController');
Route::apiResource('api', 'Test\ApiController');
Route::resources([
'api'=>'Test\ApiController'
]);
5.数据库分块操作
$result = DB::table('uinfo')
->orderBy('id')
->chunk(100, function ($result) {
foreach ($result as $item) {
$item->username = $item->username . '###22';
}
});
6.数据库判断数据是否存在
DB::table('uinfo')->where('id', 19)->exists();
DB::table('uinfo')->where('id', 19)->doesntExist()
7.数据库select查询
$base = DB::table('uinfo')->select('username', 'password');
$result = $base->addSelect('updated_at')->get();
$result = DB::table('uinfo')->select(DB::raw('count(*) as num'))
->groupBy('username')
->get();
$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.模型作用域:本地作用域
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)
{
return $builder->where('id', '>', 6);
}
}
protected static function boot()
{
parent::boot();
static::addGlobalScope(new NameScope());
}
12.模型访问器
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
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.模型关联(一对多)增删改
public function userCard()
{
return $this->Hasone(Ucard::class, 'u_id', 'id');
}
$result = Uinfo::find(15)->userCard()->save(new Ucard(['card' => '15646484']));
Uinfo::find(15)->userCard()->create(['card' => 2525]);
Uinfo::find(15)->userCard()->update(['card' => '修改']);
Uinfo::find(9)->userCard()->delete();
16.模型关联(多对多)增删改
public function rel_article()
{
return $this->belongsToMany(Keyword::class, 'a_k', 'a_id', 'k_id');
}
$k_id = 6;
Article::find(1)->rel_article()->attach($k_id, ['remarks' => '多对多增加']);
Article::find(1)->rel_article()->sync($k_id);
Article::find(1)->rel_article()->detach($k_id);
Article::find(1)->rel_article()->updateExistingPivot(3, ['remarks' => '修改']);