1
私は、モデルとリポジトリの違いをlaravel5で知りたいと思います。両方とも同じかどうか、リポジトリのメリットは何ですか?laravel 5のモデルとリポジトリの違いは何ですか?
私は、モデルとリポジトリの違いをlaravel5で知りたいと思います。両方とも同じかどうか、リポジトリのメリットは何ですか?laravel 5のモデルとリポジトリの違いは何ですか?
モデルとリポジトリは同じではありません。
投稿者documentation - Models allow you to query for data in your tables, as well as insert new records into the table.
これは、モデルがデータベーステーブルへのアクセスを開くかどうかを示しています。また、個々のクエリを記述することなくデータを引き出すために他のモデルに関連付けることもできます。
リポジトリを使用すると、コントローラ内で大量のクエリを記述することなくモデルを処理できます。一方で、コードをより細かく、モジュール化して、デバッグしやすくするために、何らかのエラーが発生した場合に備えてください。
あなたは、次のようにリポジトリを使用することができます。
public function makeNotification($class, $title, $message)
{
$notification = new Notifications;
...
$notification->save();
return $notification->id;
}
public function notifyAdmin($class, $title, $message)
{
$notification_id = $this->makeNotification($class, $title, $message);
$users_roles = RolesToUsers::where('role_id', 1)->orWhere('role_id', 2)->get();
$ids = $users_roles->pluck('user_id')->all();
$ids = array_unique($ids);
foreach($ids as $id) {
UserNotifications::create([
'user_id' => $id,
'notification_id' => $notification_id,
'read_status' => 0
]);
}
}
とコントローラ内:
protected $notification;
public function __construct(NotificationRepository $notification)
{
$this->notification = $notification;
}
public function doAction()
{
...
$this->notification->notifyAdmin('success', 'User Added', 'A new user joined the system');
...
}