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}%")

    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:

  • 写回答

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:


    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 Python题,根本不会啊
  • ¥15 Python题,不会啦
  • ¥15 LD衰减结果文件dist的单位?
  • ¥15 Python题,回答一下下啦
  • ¥15 利用Java语言结合数据库实现-个信息管理系统(如学生管理系统、图书管理系统(可以加上借书还书等功能等)或者小游戏
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题
  • ¥20 oracle RAC 怎么配置啊,配置
  • ¥15 excel 日常使用中出现问题
  • ¥20 pdusession建立失败