承接 caiochami/bundles 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

caiochami/bundles

Composer 安装命令:

composer require caiochami/bundles

包简介

A laravel set of tools for non-laravel projects

README 文档

README

A laravel-like set of tools for non-laravel projects

Installation

[composer] composer require caiochami/bundles

Usage

The [Request] class gathers and validates all http input data. If errors are found at the end of validation, it will display the information in json format. That behavior changes when no "Content-Type: application/json" is present in the headers. It will redirect to the previous page with the errors and the request data attached to the $_SESSION variable.

  1. Instantiate the class Request. Passing in a PDO instance is required when using the rule "exists".
  2. Specify the rules
  3. (Optional) Specify custom messages

Let's say we are seding a json request with the following data.

[
    "name" : "John Doe",
    "city" : "",
    "password" : "12345678",
    "password_confirmation" : "1234568",
    "id" : null,
    "patients": [
        {
            "name": "Foo",
            "age": 2
        },
        {
            "name": "Bar",
            "age": 10
        }
    ]
];

The validation will be like this:

require "vendor/autoload.php";

header("Content-Type: application/json;");

use Bundles\Request;

$request = new Request;

//all empty string variables are converted to null automatically
$request->validate([
    "name" => ["required","string", "minimum:3", "maximum:100"],
    "password" => ["required", "string", "confirmed"],
    "id" => ["nullable", "integer"],
    "patients" => ["required", "array", "gte:1"],
    "patients.*.name" => ["required", "string"],
    "patients.*.age" => ["required", "integer"]
],
[
    "required" => "The field %s is required"
]);

Let's assume we have a users table and addresses table. The model class have all the methods you need to create, update, delete and query entries. Check out the following code:

 require "vendor/autoload.php";

 use Bundles\Model;
 use Bundles\DatabaseConnection as Database;

 class User extends Model
 {

    protected static $tableName = "users";

    protected static $columns = "id,name,address.city";

    protected static $joins = "";

    protected static $key = "id";

    public function addresses(){
        $addresses = DB::use(self::$conn)
        ->table('addresses')
        ->select(['id', 'city'])
        ->where('user_id', $this->id)
        ->retrieve()
        ->get();
   
        $this->addresses = $addresses;
   
    }

 }

class Address extends Model
 {

    protected static $tableName = "addresses";

    protected static $columns = "id,city";

    protected static $joins = "";

    protected static $key = "id";

    protected static $fillable = [ "city" ];

    public function user(){
        $user = DB::use(self::$conn)
        ->table('users')
        ->select(['id', 'name'])
        ->where('id', $this->id)
        ->retrieve()
        ->first();
   
        $this->user = $user;
   
    }

    //mutator
    
    public function setCityAttribute($value){
        return strtolower($value);
    }


 }

 $connection = Database::attempt([
    "db_host" =>  "HOST",
    "db_name" =>  "NAME",
    "db_user" =>  "USER",
    "db_psw" =>  "PASSWORD"
])

 //creating a user
 $user = new User($connection);
 $user->name = "Foo";
 $user->save();

 //or

 $address = Address::create(["city"=> "My homeland"]);

 //finding and updating the user

 $user = User::find($connection, 1); 
 //or 
 $address = Address::use($connection)->where("id", 1)->first();

 $user->name = "Bar";
 $user->save();

 //or

 User::update($connection, 1, [
     "name" => "Bar"
 ]);

 // deleting a user

 User::destroy($connection, 1);

 $user = User::find($connection, 1);
 $user->delete();

 //querying all addresses

 Address::all($connection);

 //or more specific

 User::use($connection)
 ->where("name", "LIKE", "%bar%")
 ->whereBetween("birthday", "1993-01-01", "2020-01-01"),
 ->orWhere("address.city", "California")
 ->with(['addresses'])
 ->limit(15)
 ->retrieve()
 ->get();


//for debugging you can pass an array just like this
User::find($connection, 1, ["debug" => true]);

//or 

User::where("birthday", ">", "1972-11-11")
->orderBy(["name", "DESC"])
->retrieve(["debug" => true, "show_query" => true ])
->get(); 

For more examples, check out the examples/ folder

统计信息

  • 总下载量: 71
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-03

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固