1. Go to this page and download the library: Download samchentw/common 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/ */
samchentw / common example snippets
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->softDeletes();
$table->setEnable();
$table->timestamps();
});
}
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;
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->softDeletes();
$table->setSort();
$table->timestamps();
});
}
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;
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;
}
}
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()
}