我正在创建一个博客,我希望能够为一篇文章分配多个类别或标签,而一篇文章可以有多个类别.
这就是我在我的数据库中所拥有的:文章,类别和连接表articles_categories.
项目:

加入表:

在我的表/ ArticlesTable.php中:
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
$this->belongsToMany('Categories', [
'alias' => 'Categories',
'foreignKey' => 'article_id',
'targetForeignKey' => 'category_id',
'joinTable' => 'articles_categories'
]);
}
Run Code Online (Sandbox Code Playgroud)
在我的表/ CategoriesTable.php中:
public function initialize(array $config)
{
$this->table('categories');
$this->displayField('name');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsToMany('Articles', [
'alias' => 'Articles',
'foreignKey' => 'category_id',
'targetForeignKey' => 'article_id',
'joinTable' => 'articles_categories'
]);
}
Run Code Online (Sandbox Code Playgroud)
当用户添加文章时,它需要添加文章,然后在连接表中的ids是我的ArticlesController/add方法:
public function add()
{
$article = $this->Articles->newEntity();
if ($this->request->is('post'))
{
$article = $this->Articles->patchEntity($article, $this->request->data);
$article->user_id = …Run Code Online (Sandbox Code Playgroud) 我有一个页面,我可以修改用户详细信息(用户名,名字,头像......).我的导航栏中有一个元素,其中包含有关当前登录用户的信息.问题是我无法弄清楚如何在修改数据后立即刷新会话.
在UsersController中:
public function edit($id = null)
{
if (!$id) {
throw new NotFoundException(__('Invalid user'));
}
$user = $this->Users->get($id);
if ($this->request->is(['post', 'put'])) {
$this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
//REFRESH SESSION ????\\
$this->request->session()->write('Auth.User', $user);
//\\
$this->Flash->success(__('User has been updated.'));
return $this->redirect(['action' => 'edit/' . $id]);
}
$this->Flash->error(__('Unable to update User details.'));
}
$this->set(compact('user'));
}
Run Code Online (Sandbox Code Playgroud) 所以我想对我的 API 端点进行单元测试。我正在使用 Laravel 5.8 并且使用 Passport 完成 api 身份验证我有以下测试:
public function guest_can_login()
{
$user = factory(User::class)->create();
$response = $this->json('POST', 'api/login', [
'email' => $user->email,
'password' => 'secret',
]);
$response
->assertStatus(200)
->assertJson([
'success' => true,
]);
}
Run Code Online (Sandbox Code Playgroud)
当我使用 Postman 执行请求时,它运行良好,对用户进行身份验证并返回一个令牌,但是当我启动测试时它失败了(“预期状态代码 200,但收到 500。”)
所以我在回复中更深入地搜索,我发现了这个:
"message": "未找到个人访问客户端。请创建一个。"
不知道为什么会发生这种情况,如果有人对此有所了解
编辑:固定见下面我的最后一个答案