1.repository pattern
2.quickly make repository,service and helper
3.simple dictionary type
4.make simple router list
composer require samchentw/common
Publish the config file by running:
$ php artisan vendor:publish --provider="Samchentw\Common\CommonServiceProvider"
Samchentw\Common\Repositories\Base\Repository
Samchentw\Common\Helpers\DictionaryHelper
Samchentw\Common\Traits\Supports\HasEnable
Samchentw\Common\Traits\Supports\HasSort
$ php artisan make:repository MyRepository
$ php artisan make:service MyService
$ php artisan make:helper MyHelper
In the migration. use ( $table->setEnable();)
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->softDeletes();
$table->setEnable();
$table->timestamps();
});
}
In the model.
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Samchentw\Common\Traits\Supports\HasEnable;
class Article extends Model
{
use HasFactory,
SoftDeletes,
HasEnable;
In the migration. use ( $table->setSort();)
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->softDeletes();
$table->setSort();
$table->timestamps();
});
}
In the model.
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Samchentw\Common\Traits\Supports\HasSort;
class Article extends Model
{
use HasFactory,
SoftDeletes,
HasSort;
You can set the sorting method in the config/common.php
For example, make ArticleRepository,
$ php artisan make:repository ArticleRepository
namespace App\Repositories;
use Samchentw\Common\Repositories\Base\Repository;
use App\Models\Article;
class ArticleRepository extends Repository
{
/**
* @return string
*/
public function model(): string
{
return Article::class;
}
}
In Controller
class ArticleController extends Controller
{
private $articleRepository;
public function __construct(ArticleRepository $ArticleRepository)
{
$this->articleRepository = $ArticleRepository;
}
//for Front
public function example1()
{
// get enable true data And sorted
return $this->articleRepository->getAllForFront();
}
//for Front
public function example2()
{
$query = $this->articleRepository->getAllForFrontQuery();
// get enable true data And sorted
return $query->where('title','=','test')->get();
}
//for Admin
public function example3()
{
// get sorted data
return $this->articleRepository->getAllForAdmin();
}
//for Admin
public function example4()
{
$query = $this->articleRepository->getAllForAdminQuery();
// get sorted data
return $query->where('title','=','test')->get();
}
//else...
public function example5()
{
return Article::whereIn('id',[1,2,3,4,5])->orderBy('name')->sortByConfig()->get()
}
use Samchentw\Common\Helpers\DictionaryHelper;
$datas = [
[
"id" => 1,
"name" => "sam",
"job" => "developer"
],
[
"id" => 2,
"name" => "john",
"job" => "admin"
],
[
"id" => 3,
"name" => "vivian",
"job" => "user"
]
];
$result1 = DictionaryHelper::toDictionary($datas, 'name', 'job');
$result2 = DictionaryHelper::toDictionary($datas, 'id');
//output:
$result1 = [
"sam" => "developer",
"john" => "admin",
"vivian" => "user"
];
$result2 = [
1 => [
"id" => 1,
"name" => "sam",
"job" => "developer"
],
2 => [
"id" => 2,
"name" => "john",
"job" => "admin"
],
3 => [
"id" => 3,
"name" => "vivian",
"job" => "user"
]
];
$ php artisan output:router-list
url: http://127.0.0.1:8000/router-list
show method setting in config/common.php.
For Example:
// config/common.php
return [
"model_sort" => env('MODEL_SORT', 'asc'),
/**
* If you just want to show GET method.
* Run:
* php artisan optimize
* php artisan output:router-list
**/
"router_list_methods" => [
"GET",
// "HEAD",
// "POST",
// "PUT",
// "PATCH",
// "DELETE"
]
];