crocodicstudio / cbmodel
使用模型增强提升laravel数据库关系
Requires
- php: ~7.2|~7.3
- doctrine/dbal: ^2.10
- illuminate/console: ^5.7|^6.0|^7.0
- illuminate/support: ^5.7|^6.0|^7.0
README
关于laravel eloquent的替代方案
此包已弃用
请使用https://github.com/crocodic-studio/laravel-model
要求
Laravel 5.* | 6.* | 7.*
安装命令
composer require crocodicstudio/cbmodel=^2.0
1. 创建模型
从表创建模型
php artisan create:model foo_bar_table
为所有表创建模型
php artisan create:model
使用其他连接创建模型
php artisan create:model foo_bar_table --connection=con2
假设您有一个结构如下所示的books
表
id (Integer) Primary Key
created_at (Timestamp)
name (Varchar) 255
它将在/app/Models/Books.php
中自动创建一个新文件,具有以下结构
<?php namespace App\Models; use DB; use Crocodicstudio\Cbmodel\Core\Model; class Books extends Model { public $tableName = "books"; public $connection = "mysql"; public $primary_key = "id"; public $id; public $created_at; public $name; }
2. 在您的控制器中使用CB模型类
在控制器类名称顶部插入use App\Models\Books;
<?php namespace App\Http\Controllers; use App\Models\Books; class FooController extends Controller { public function index() { $books = Books::latest(); return view("books", ["bookData"=>$books]); } public function detail($id) { $book = Books::find($id); return view("book_detail", ["book"=>$book]); } public function delete($id) { Books::deleteById($id); return redirect()->back()->with(["message"=>"Book ".$book->name." has been deleted!"]); } } ?>
3. 使用具有关系的CB模型类
假设您有一个结构如下的categories
表,用于书籍关系
id (Integer) Primary Key
name (Varchar) 255
并且您的书籍结构如下
id (Integer) Primary Key
created_at (Timestamp)
categories_id (Integer)
name (Varchar) 255
现在您必须为categories
表创建一个模型,您可以按照前面的步骤进行。
假设您已创建一个categories
模型,请确保现在我们在/app/Models/
中有了两个文件
/Books.php
/Categories.php
打开Books模型,并添加以下方法
/** * @return Categories */ public function category() { return $this->belongsTo("App\Models\Categories"); } // or /** * @return Categories */ public function category() { return Categories::find($this->categories_id); }
然后打开FooController
<?php namespace App\Http\Controllers; use App\Models\Books; class FooController extends Controller { ... public function detail($id) { $book = Books::find($id); $data = []; $data['book_id'] = $book->id; $data['book_name'] = $book->name; $data['book_category_id'] = $book->category()->id; $data['book_category_name'] = $book->category()->name; return view("book_detail",$data); } } ?>
如您所见,现在我们可以通过使用->category()->name
来获取类别名称,而无需任何SQL查询或甚至数据库构建器语法。您还可以递归地向下到您的关联,无需限制。
4. 如何将DB Builder Collection输出转换为CB模型类?
您可以将简单的数据库构建器集合轻松转换为cb模型类。
$row = DB::table("books")->where("id",1)->first(); //Cast to CB Model $model = new Books($row); //And then you can use cb model normally echo $model->name;
5. 如何使用CB模型插入数据
您可以通过以下方法轻松插入数据->save()
如下所示
$book = new Books(); $book->created_at = date("Y-m-d H:i:s"); //this created_at is a magic method you can ignore this $book->name = "New Book"; $book->categories_id = 1; $book->save();
然后,如果您想获取最后插入的ID,可以这样做
... $book->save(); $lastInsertId = $book->id; // get the id from id property ...
5. 如何使用CB模型更新数据
您可以轻松地更新数据,只需先找到它
$book = Books::findById(1); $book->name = "New Book"; $book->categories_id = 1; $book->save();
5. 如何删除数据?
您可以轻松地删除数据,只需先找到它即可。
$book = Books::findById(1); $book->delete();
或者
Books::deleteById(1);
模型方法可用
/** * Find all data by specific condition. */ $result = FooBar::findAllBy($column, $value = null, $sorting_column = "id", $sorting_dir = "desc"); // or $result = FooBar::findAllBy(['foo'=>1,'bar'=>2]); /** * Find all data without sorting */ $result = FooBar::findAll(); /** * Count the records of table */ $result = FooBar::count(); /** * Count the records with specific condition */ $result = FooBar::countBy($column, $value = null); // or $result = FooBar::countBy(['foo'=>1,'bar'=>2]); /** * Find all datas and ordering the data to descending */ $result = FooBar::findAllDesc($column = "id"); // or simply $result = FooBar::latest(); /** * Find all datas and ordering the data to ascending */ $result = FooBar::findAllAsc($column = "id"); // or simply $result = FooBar::oldest(); /** * Find/Fetch a record by a primary key value */ $result = FooBar::findById($id); // or simply $result = FooBar::find($id); /** * Create a custom query, and result laravel Query Builder collection */ $result = FooBar::table()->where("foo",1)->first(); /** * Create a custom query and casting to model object */ $result = FooBar::query(function($query) { return $query->where("bar",1); }); /** * Create a custom list query and casting them to model objects */ $result = FooBar::queryList(function($query) { return $query->where("bar",1); }); /** * Find a record by a specific condition */ $result = Foobar::findBy($column, $value = null); // or $result = Foobar::findBy(['foo'=>1,'bar'=>2]); /** * To run the insert SQL Query */ $fooBar = new FooBar(); $fooBar->name = "Lorem ipsum"; $fooBar->save(); /** * To bulk insert */ $data = []; $foo = new FooBar(); $foo->name = "Lorem ipsum 1"; array_push($data, $foo); $bar = new FooBar(); $bar->name = "Lorem ipsum 2"; array_push($data, $bar); FooBar::bulkInsert($data); /** * To run the update SQL Query */ $fooBar = FooBar::findById($value); $fooBar->name = "Lorem ipsum"; $fooBar->save(); /** * To delete the record by a primary key value */ FooBar::deleteById($value); /** * To delete the record by a specific condition */ FooBar::deleteBy($column, $value = null); // or Foobar::deleteBy(['foo'=>1,'bar'=>2]); /** * To delete after you fetch the record */ $fooBar = FooBar::findById($value); $fooBar->delete();
一对一关系
class Posts extends Model { // etc /** * @return App\Models\Comments[] */ public function comments() { return $this->hasMany(Comments::class); } // or with full option /** * @return App\Models\Comments[] */ public function comments() { return $this->hasMany(Comments::class, "foreign_key", "local_key", function($condition) { return $condition->where("status","Active"); }); } }
一对多关系
class Comments extends Model { // etc /** * @return App\Models\Posts */ public function post() { return $this->belongsTo(Posts::class); } // or with full option /** * @return App\Models\Posts */ public function post() { return $this->belongsTo(Posts::class, "foreign_key", "local_key"); } }