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

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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?