dtc99987 2019-02-23 03:16
浏览 53
已采纳

输入参数的搜索功能问题是from_date和to_date,返回具有相同输入日期时间的项目列表

This is the problem that I am meeting with - problem on search function with parameter is $from_date and $to_date in Laravel.

I have a function getByDate() in ProjectRepository with the following code:

public function getByDate() {
    $from_date = Carbon::parse(request('startDate'))->startOfDay();
    $to_date = Carbon::parse(request('endDate'))->endOfDay();

    $project = DB::table('projects')
    ->whereBetween('startDate', [$from_date, $to_date])
    ->get();

    return $project;
}

From the ProjectController, I inject the repository into searchProject() in Controller, in this one I have:

public function searchProject(){
    $project = $this->repository->getByDate();

    if (is_null($project)) {
        return $this->sendError('Projects not found. Try again :)');
    }

    return response()->json([
        'status' => true,
        'data' => $project,
    ]);
}

In the api.php where I declare Route for mapping API URL, I have this line:

Route::get('search_project', 'Project\ProjectController@searchProject');

And when I test on POSTMAN with the URL localhost:8000/api/search_project?from_date=2018-01-12&to_date=2018-03-03, it return the result

"message": "",
"exception": "Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException",
"file": "D:\\project-porfolio\\vendor\\laravel\\framework\\src\\Illuminate\\Routing\\RouteCollection.php",
"line": 179,

Can anyone give me some advise what I do have something wrong and suggest for another way?

  • 写回答

1条回答 默认 最新

  • dongya9346 2019-02-23 05:19
    关注

    I believe that you are not capturing the right params from the code you have posted. So in your repository you are looking for startDate and endDate but the url in question has from_date=2018-01-12&to_date=2018-03-03. I believe it should be;

    $from_date = Carbon::parse(request('from_date'))->startOfDay();
    $to_date = Carbon::parse(request('to_date'))->endOfDay();
    

    PS: By the way you are using a custom action of searchProjects() in your ProjectsController. Its a best practice that you would create an invokable class i.e. single action controller ( if thats the only action you require ) and then have your logic there. so may be something like below;

    // Route
    Route::get('search_projects', Project\SearchProjectsController::class)
    
    // Controller
    class SearchProjectsController extends Controller 
    {
        public function __invoke(Request $request)
        {
           // your logic
        }
    }
    

    here is a link to further explore this topic https://www.youtube.com/watch?v=MF0jFKvS4SI

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料