我有一个想法,可以使用身份验证中间件来检索用户的产品 [英] I have an idea to use auth middleware to retrieve a user's products
本文介绍了我有一个想法,可以使用身份验证中间件来检索用户的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个想法是使用身份验证中间件检索用户的产品,但目前我无法检索该用户的产品。相反,它列出了其他用户的所有产品。你们能给我一些想法来帮助我完成它吗?谢谢!
这是我的项目控制器代码:
<?php
namespace AppHttpControllers;
use AppHttpControllersController;
use AppHttpResourcesProjectResource;
use AppModelsProject;
use IlluminateHttpRequest;
use IlluminateSupportFacadesValidator;
class ProjectController extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
public function index()
{
$projects = Project::all();
return response([
'projects' => ProjectResource::collection($projects),
'message' => 'Retrieved successfully'
], 200);
}
/**
* Store a newly created resource in storage.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function store(Request $request)
{
$data = $request->all();
$validator = Validator::make($data, [
// 'uuid' => 'required|unique:projects',
'user_id' => 'required',
'name' => 'required|max:255',
'status' => 'required',
]);
if ($validator->fails()) {
return response(['error' => $validator->errors(), 'Validation Error']);
}
$projects = Project::create($data);
return response(['projects' => new ProjectResource($projects), 'message' => 'Created successfully'], 201);
}
/**
* Display the specified resource.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function show(Project $project)
{
return response(['project' => new ProjectResource($project), 'message' => 'Retrieved successfully'], 200);
}
/**
* Update the specified resource in storage.
*
* @param IlluminateHttpRequest $request
* @param int $id
* @return IlluminateHttpResponse
*/
public function update(Request $request, Project $project)
{
$project->update($request->all());
return response(['project' => new ProjectResource($project), 'message' => 'Update successfully'], 200);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return IlluminateHttpResponse
*/
public function destroy(Project $project)
{
$project->delete();
return response(['message' => 'Deleted'], 204);
}
}
这是我的项目模型代码:
<?php
namespace AppModels;
use AppEnumsProjectStatus;
use AppHttpTraitsUuid;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use IlluminateSupportStr;
class Project extends Model
{
use HasFactory;
use Uuid;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'status',
'user_id',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'id',
'uuid',
];
protected $cast = [
'status' => ProjectStatus::class
];
public function setUuidAttribute()
{
$this->attributes['uuid'] = Str::uuid();
}
}
推荐答案
您必须首先检索用户才能筛选产品。有多种方法可以做到这一点。以下是指向Laravel关于该主题的文档的链接:https://laravel.com/docs/8.x/authentication#retrieving-the-authenticated-user
您还必须过滤您的查询(您的查询是Project::all()
),以便它只检索用户的产品。以下是一些与此相关的Laravel文档的链接:https://laravel.com/docs/8.x/eloquent#retrieving-models
最后,将这个$projects = Project::all();
替换为这个$projects = Project::where('user_id', auth()->user()->id);
应该可以奏效。
这篇关于我有一个想法,可以使用身份验证中间件来检索用户的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文