PHP code example of goenitz / simple-model
1. Go to this page and download the library: Download goenitz/simple-model 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/ */
goenitz / simple-model example snippets
\Goenitz\SimpleModel\Model::connect([
'host' => '127.0.0.1',
'port' => 3306,
'database' => 'yourdatabasename',
'username' => 'yourdatabaseusername',
'password' => 'yourpassword'
]);
namespace App;
use Goenitz\SimpleModel\Model;
class Article extends Model
{
//protected $table = 'articles'; // 表名, 多数情况下会自动使用复数形式
protected $fillable = ['title', 'content']; // 可以插入的字段
//protected $identifier = 'id'; // 自增主键,默认为id
//protected $hidden = ['content']; // 转换json, array 时隐藏的字段
//用于设置属性,一般情况下不需要
//protected function setTitleAttribute($value)
//{
// $this->attributes['title'] = strtoupper($value);
//}
//获取属性,一般情况下不需要
//protected function getTitleAttribute()
//{
// return $this->attributes['title'] . 'xyz';
//}
}
use App\Article;
Article::create([
'title' => 'test',
'content' => 'just a test'
]);
$article = new Article();
$article->title = 'another test';
$article->content = 'another content';
$article->save();
$newArticles = Article::createMany([
[
'title' => 'test1',
'content' => 'content1'
],
[
'title' => 'test2',
'content' => 'content2'
]
]);
dd($newArticles);
$article = Article::first();
echo $article->title . '<br>';
echo $article['title'] . '<br>';
echo $article->content . '<br>';
echo $article['content'] . '<br>';
$articles = Article::all();
$article = Article::find(1);
$articles = Article::limit(10)->skip(10)->orderBy('id', 'desc')->get();
dump($articles);
$article = Article::select(['id', 'title'])->first();
$article = Article::where('id', 50)->get(['title', 'content']);
$articles = Article::where('id', '50')->get();
$articles = Article::where(['id', '50'])->get();
$articles = Article::where('id', '>', '50')->get();
$articles = Article::where(['id', '>', '50'])->get();
$articles = Article::where([
['id', '>', '50'],
['title', '<>', 'test'],
])->get();
$articles = Article::where('id', '>', '50')->orWhere('id', '<', '30')->get();
$articles = Article::where('id', '>', '50')->limit(10)->skip(10)->orderBy('id')->get();
//还可以闭包使用,但是不支持闭包内部再使用闭包
$articles = Article::where(function ($query) {
$query->where(['id', '>', '50']);
$query->where(['title', '<>', 'test']);
})->get();
$articles = Article::where(function ($query) {
$query->where([
['id', '>', 50],
['title', '<>', 'test']
]);
})->get()
$articles = Article::where(function ($query) {
$query->where(['id', '>', 50]);
$query->orWhere(['title', '<>', 'test']);
})->get()
$articles = Article::whereIn('id', [10, 15, 20])->get();
$articles = Article::whereIn('id', [10, 15, 20], true)->get(); // not in
$article = Article::find(5);
$article->title = 'updated';
$article->save();
$article = Article::find(5);
$article->update([
'title' => 'foo',
'content' => 'bar'
]);
Article::where('title', 'test')->update(['title', 'foo']);
Article::whereIn('id', [5, 10, 15])->update(['title', 'foo']);
$article = Article::first();
$article->delete();
Article::destroy([5, 10, 15]);
//或
Article::destroy(5, 10, 15);
Article::where('id', '18')->delete();
Article::whereIn('id', [19, 20, 21])->delete();
Article::find(30)->toJson();
//{"id":"30","title":"foo"}
Article::find(30)->toArray();
/*[
"id" => "30",
"title" => "foo"
]*/
$connection = \Goenitz\SimpleModel\Model::$connection;
// do everything you want.