doushou8299 2019-04-28 09:54
浏览 271
已采纳

Laravel 5.8 Restful API中的搜索功能

I'm implementing an API for front end and mobile apps now i'm working on search function where a user may type column phone number or any data i should be able to provide the data they request in a JSON format

i really don't know how to query based on the data they input, so for i did this

My Controller,

public function searchMember(Request $request)
{
    $data = $request->get('data');
    $member_info = Member::where('phone_number', 'like', "%{$data}%")
             ->get();

    return Response()->json([
        'status' => 'success',
        'data' => $member_info
    ], 200);
}

My Model,

class Member extends Model
{
    protected $fillable = ['name','member_id','service_id','batch_id','phone_number','office_name','designation','email','image','remember_token'];
}

And my routes

Route::get('search?data=phone_number', 'MemberController@searchMember');

When I run the API resource its return 404 | not found. I can not find the solution how to solve this. In postman I run the API something like that:

http://localhost:8000/api/v1/search?data=0179826****
  • 写回答

1条回答 默认 最新

  • dongzhong7299 2019-04-28 10:53
    关注

    I know you solved your problem but just in case some one wants to know the answer. the correct way to pass data in url in rout is:

    Route::get('search/{phone_number}', 'MemberController@searchMember');
    

    and in your controller get it like:

    public function searchMember(Request $request, $phone_number)
    

    then your url would look like:

    http://localhost:8000/api/v1/search/0179826****
    

    other than that if you are going to send variable through request you don't need to clarify it in rout. so the route would be like:

    Route::get('search', 'MemberController@searchMember');
    

    the controller and the url would be as you posted.

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

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加