建表:php artisan make:migration create_tasks_table --create=tasks
建模型:php artisan make:model Task
一個User實例對應多個Task實例,而一個Task實例從屬于某個User:
/**
* Get all of the tasks for the user.
*/
public function tasks()
{
return $this->hasMany(Task::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
路由:
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('welcome');
})->middleware('guest');
Route::get('/tasks', 'TaskController@index');
Route::post('/task', 'TaskController@store');
Route::delete('/task/{task}', 'TaskController@destroy');
Route::auth();
});
設置中間件:
class TaskController extends Controller{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
}
依賴注入:
我們創建一個app/Repositories目錄并在其中創建一個TaskRepository類。記住,Laravel項目的app文件夾下的所有目錄都使用 PSR-4 自動加載標準被自動加載,所以你可以在其中隨心所欲地創建需要的目錄:
~~~
<?php
namespace App\Repositories;
use App\User;
use App\Task;
class TaskRepository{
/**
* Get all of the tasks for a given user.
*
* @param User $user
* @return Collection
*/
public function forUser(User $user)
{
return Task::where('user_id', $user->id)
->orderBy('created_at', 'asc')
->get();
}
}
public function __construct(TaskRepository $tasks)
{
$this->middleware('auth');
$this->tasks = $tasks;
}
授權:
php artisan make:policy TaskPolicy
class TaskPolicy{
use HandlesAuthorization;
/**
* Determine if the given user can delete the given task.
*
* @param User $user
* @param Task $task
* @return bool
*/
public function destroy(User $user, Task $task)
{
return $user->id === $task->user_id;
}
}
最后,我們需要關聯Task模型和TaskPolicy,這可以通過在app/Providers/AuthServiceProvider.php文件的policies屬性中添加注冊來實現,注冊后會告知Laravel無論何時我們嘗試授權動作到Task實例時該使用哪個策略類進行判斷:
/**
* The policy mappings for the application.
*
* @var array
*/
protected $policies = [
'App\Task' => 'App\Policies\TaskPolicy',
];
~~~
- 關于我
- laravel
- quickstart
- quickstart-intermediate
- swoole
- (一)快速起步
- php7
- swoole異步高性能
- 開發中常見問題
- event擴展的安裝
- phptrace
- 用C/C++寫php擴展
- 無聊的筆試題
- rewrite二級目錄轉二級域名
- php多進程
- rpc-yar
- php專家列表
- php守護進程
- php函數防止超時
- php分析報錯信息
- gdb調試php
- php-cli模式
- composer/pear
- 基礎
- sublime+xdebug
- 開啟opcache
- 前端
- js
- linux
- Xshell連接不上Ubuntu解決方式
- xshell
- centos安裝中文輸入
- centos下安裝谷歌瀏覽器
- centos安裝phpstorm
- php7之phpredis安裝
- 磁盤大小
- dns
- TCP/IP協議
- HTTP
- tcpdump
- zbacktrace
- gdb調試php擴展
- lsof
- perf
- lnmp
- first
- 重定向
- echo
- 鍵盤高效操作
- 權限控制
- 進程
- 環境變量
- vi
- 軟件包管理
- 網絡
- 查找文件
- 壓縮
- 正則
- sed/awk
- 編譯程序
- shell腳本
- shell認識
- sh腳本
- sh調試相關
- win共享文件夾給虛擬機
- git
- git的安裝
- 常用命令
- 本地到遠程倉庫
- 遠程到本地倉庫
- 分支管理
- bug分支
- feature
- 標簽
- 多人協作
- FAQ
- C/C++
- 難點
- 修飾符
- 數組
- 字符串
- 指針
- 引用
- 面向對象
- 類訪問修飾符
- 構造函數
- 操作文件
- mysql集群
- 使用navicat操作MySQL數據庫能不能整個數據庫搜索一條數據?
- 幫助的使用
- 存儲引擎的選擇
- 數據類型/字符集
- 索引
- kafka集群
- rabbitmq集群
- (一)初識rabbitmq
- (二)原理
- (三)消息模型
- (四)rabbitmq&php基礎
- (五)持久化&route&指定exchange
- (六)發布訂閱
- (七)route key
- (八)topic
- elasticsearch集群
- (一)服務端搭建
- (二)elasticsearch&php
- (三)head插件
- redis集群
- github
- 設計模式
- createType
- factory_method.php
- abstract_factory.php
- mysql_singleton.php
- builder.php
- prototype.php
- structType
- adapter.php
- 數據結構與算法
- python