rcbytes/fetch

获取您的eloquent模型

dev-master / 1.0.x-dev 2015-11-29 14:42 UTC

This package is not auto-updated.

Last update: 2024-09-11 23:13:44 UTC


README

Software License

轻松查询Eloquent模型。非常适合使用Laravel作为API的用户

安装

通过Composer

$ composer require rcbytes/fetch

config/app.php

服务提供者

rcbytes\fetch\FetchServiceProvider::class

外观

'Fetch' => rcbytes\fetch\Fetch::class

用法

控制器

...

class UserController extends Controller

	public function index(Request $request)
	{
	   
	    // your criteria will overwrite criteria from Request->where  
	    $where = [ "account_id" => $request->user()->account_id ];
  		return Fetch::all(App\User::query(), $where);
	
	}
	
	public function show($id)
	{
	
	    // third parameter $key defaults to "id"
  		return Fetch::one(App\User::query(), $id);
	
	}

...

模型

class User implements AuthenticatableContract, CanResetPasswordContract
{
    ...
    public function friends()
    {
    
    	return $this->hasMany(\App\User::class);
    	
    }
    
    public function posts()
    {
    
    	return $this->hasMany(\App\Model\Post::class);
    	
    }
    ...
}

请求

获取所有带有他们的 PostsFriendsUsers

GET /user?with=["posts", "friends"]

获取年龄等于30的 Users 带有他们的 PostsFriends

GET /user?with=["posts", "friends"]&where={age: 30}

获取至少有一个年龄大于30的 FriendUsers 带有他们的 PostsComments 以及 Friends

GET /user?with=["posts.comments", "friends"]&where={"friends.age":[ ">", 30]}

获取 key = 1User 带有他们的 PostsFriendsFriends of Friends

GET /user/1?with=["posts", "friends.friends"]

参数

  • array with:它期望模型具有关系方法。接受点表示法,使用时同时接受模型。例如:?with:["posts.comments"]。必须存在 User::postsPost::comments 方法
  • object|mixed where:它期望模型具有查询的列。接受点表示法,查询关系,如果找到至少一个符合条件的关系,则返回模型。例如:?where:{age: 30}?where:{age:[">", 30]}?where:{"friends.age":[">", 30]}
  • integer take:获取此数量的模型
  • integer paginate:也称为每页数量
  • integer page:页码

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

测试

$ to do

贡献

请参阅CONTRIBUTINGCONDUCT以获取详细信息。

安全

如果您发现任何与安全相关的问题,请通过电子邮件lab.rafamello@gmail.com联系,而不是使用问题跟踪器。

致谢

许可

MIT许可证(MIT)。请参阅许可文件以获取更多信息。