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

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条回答 默认 最新

相关推荐 更多相似问题