-
-
Save loktarjugg/5f8bbcb75935d7d2de5414d0863482b3 to your computer and use it in GitHub Desktop.
字段映射小技巧
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: jacob | |
* Date: 2017/5/3 | |
* Time: 16:41 | |
*/ | |
namespace App\Traits; | |
/** | |
* Class BaseRepository | |
* @package App\Traits | |
*/ | |
trait BaseRepository | |
{ | |
/** | |
* 通过主键获取数据 | |
* @param $id | |
* @return mixed | |
*/ | |
public function find($id) | |
{ | |
return $this->model->findOrFail($id); | |
} | |
/** | |
* 获取所有数据 | |
* @return mixed | |
*/ | |
public function lists() | |
{ | |
return $this->model->get(); | |
} | |
/** | |
* 分页数据 | |
* 自动增加查询参数 | |
* @param int $count 每页显示数量 | |
* @param string $sortColumn 排序字段 | |
* @param string $sort 排序方式 desc asc | |
* @return mixed | |
*/ | |
public function paginate($count = 15 , $sortColumn = 'id' , $sort ='desc') | |
{ | |
$count = \Request::has('count') ? \Request::get('count') : $count; | |
$queryParams = array_diff_key($_GET, array_flip(['page'])); | |
$sort = \Request::has('sort') ? \Request::get('sort') : $sort; | |
$params = \Request::all(); | |
$fields = isset($this->searchField) ? $this->searchField :[]; | |
if (count($fields) > 0 ){ | |
foreach ($params as $param => $value ) { | |
if (array_has($fields , $param )){ | |
$this->model = $this->model->where( array_get( $fields , $param ) , $value); | |
} | |
} | |
} | |
return $this->model | |
->orderBy($sortColumn , $sort) | |
->paginate($count) | |
->appends($queryParams); | |
} | |
public function update(array $data , $id) | |
{ | |
$model = $this->find($id); | |
return $this->save($model , $data ); | |
} | |
public function destroy($id) | |
{ | |
$model = $this->find($id); | |
return $model->delete(); | |
} | |
/** | |
* 保存模型 | |
* @param $data | |
* @return mixed | |
*/ | |
public function store($data) | |
{ | |
return $this->save($this->model , $data); | |
} | |
/** | |
* @param $model | |
* @param $data | |
* @return mixed | |
*/ | |
public function save($model , $data) | |
{ | |
$model->fill($data); | |
$model->save(); | |
return $model; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Models; | |
use App\Traits\ModelTimestampTrait; | |
use Illuminate\Database\Eloquent\Relations\BelongsToMany; | |
use Illuminate\Notifications\Notifiable; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
use Illuminate\Support\Facades\Auth; | |
use Laravel\Passport\HasApiTokens; | |
class User extends Authenticatable | |
{ | |
use | |
Notifiable, | |
HasApiTokens; | |
protected $guarded = [ | |
'email', | |
'phone', | |
'balance', | |
'pending', | |
'is_admin' | |
]; | |
protected $hidden = [ | |
'password', | |
]; | |
public $searchField =[ | |
'username' => 'name', | |
'type' => 'user_type', | |
'game_id' => 'game_type_id', | |
'status' => 'user_status', | |
]; //在这里做字段映射 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: jacob | |
* Date: 2017/6/7 | |
* Time: 09:31 | |
*/ | |
namespace App\Repositories; | |
use App\Models\User; | |
use App\Traits\BaseRepository; | |
class UserRepository | |
{ | |
use BaseRepository; | |
protected $model; | |
protected $searchField; //搜索字段映射 | |
public function __construct(User $user) | |
{ | |
$this->model = $user; | |
$this->searchField = $user->searchField; | |
} | |
public function search() | |
{ | |
return $this->paginate(15,'outcome_id'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment