douchen2025 2018-04-01 12:26
浏览 62
已采纳

Laravel REST API句柄查询

I'm creating REST API with Laravel 5.6 (I have to say I'm new because I might have used the wrong terms.)

I have get method like this , with this method I can get all parties :

http://127.0.0.1:8000/api/parties

but I want to get specific query like this :

http://127.0.0.1:8000/api/parties?party_category=club

How can I handle my queries ?

class PartiesController extends Controller
{
    public function index() {
      $parties = Parties::all();
       return Fractal::includes('places')->collection($parties,new PartyTransformer);
    }

    public function show(Parties $parties) {
      return Fractal::includes('places')->item($parties,new PartyTransformer);
    }

}

My route :

Route::middleware('auth:api')->get('/user', function (Request $request) {
    return $request->user();
});

Route::post('/register',['as' => 'register', 'uses' => 'RegisterController@index']); 

Route::group(['namespace' => 'Parties', 'prefix' => '/parties', 'middleware' => ['auth:api']], function(){
    Route::get('/',['as' => 'parties', 'uses' => 'PartiesController@index']); 
    Route::put('/',['as' => 'parties.store', 'uses' => 'PartiesController@store']); 
    Route::get('/{parties}',['as' => 'parties.show', 'uses' => 'PartiesController@show']);
    Route::get('/{user}',['as' => 'user.show', 'uses' => 'PlacesController@show']); 
    Route::post('/{parties}',['as' => 'parties.update', 'uses' => 'PartiesController@update']); 
    Route::delete('/{parties}',['as' => 'parties.destroy', 'uses' => 'PartiesController@destroy']); 
});
  • 写回答

1条回答 默认 最新

  • douan5151 2018-04-01 12:40
    关注

    You can check if party_category exists on url or not.

    public function index(\Illuminate\Http\Request $request) {
       if($request->has('party_category')){
          $parties = Parties::where('party_category', $request->party_category)->get();//your new query here
       }
       else{
          $parties = Parties::all();
       }
       return Fractal::includes('places')->collection($parties,new PartyTransformer);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'