PHP code example of cblink / model-library

1. Go to this page and download the library: Download cblink/model-library library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cblink / model-library example snippets




return [
	// ...
	
	'paginate' => [
		'all_key' => 'is_all',
		'page_key' => 'pre_page',
	]
];



namespace App;
use Cblink\ModelLibrary\Laravel\PageableTrait;

class User extends Model 
{
	use PageableTrait;
	// ...
}



// 每页数量字段使用配置名 'app.paginate.page_key'
User::query()->page();

// 使用简单分页(不包含页码),每页数量字段使用配置名 'app.paginate.page_key'
User::query()->simplePage();

// 如果前端传入了字段 `app.paginate.all_key`,则获取所有数据,如不传入,则输出分页数据,其他同上
User::query()->pageOrAll();

// 同上
User::query()->simplePageOrAll();



namespace App;
use Cblink\ModelLibrary\Laravel\SearchableTrait;

class User extends Model 
{
	use SearchableTrait;
	// ...
}


// 所有的搜素都必须保证两个条件才会触发
// 1. 前端有传入值,如果前端没有传入值将不会触发搜索
// 2. 填写了默认值,default字段

User::query()->search([
	// 这里的 query 为前端传入的参数名将使用参数值进行搜索匹配
	'query' => [
		"filed" => "匹配数据库的字段名",
		"type" => "搜索类型",
		"mix" => "混合条件",
		"group" => "搜索分组",
		"default" => "默认值",
		"relate" => "关联查询",
	]
])->get();

User::search([
	'status' => ['type' => 'in']
]);
// 等价搜索 , is_array部分无需后端处理,根据前端传值来处理
$query->whereIn('status', is_array($status) ? $status : [$status]);

User::search([
	'number' => ['relate' => 'order', 'field' => 'order_no'],
]);

// 等价搜索
User::whereHas('order', function($query){
	$query->where('order_no', $number);
});