Home > Article > PHP Framework > Share your personal recommended programming specifications for laravel or other frameworks
The following tutorial column will introduce to you the personally recommended programming specifications of laravel or other frameworks. I hope it will be helpful to friends in need! Preliminary Summary
to everyone's development habits. Many inconveniences hinder the efficiency of multi-person collaborative development. Unified specifications
···/** * @desc 获取 users.username * @param int $user_id users.id * @return string */public static function getUsername(int $user_id): string{ return self::where('id', $user_id)->value('username');}// getUsername() end/** * @desc 获取 users.age * @param int $user_id users.id * @return int */public static function getAge(int $user_id): int{ return (int)self::where('id', $user_id)->value('age');}// getAge() end···
$user_id
, I use ## In the form of #users.id
id field in the
users table).
The returned parameters are also intuitively explained, and the value is the value of the username
field in the users
table. function
The naming is distinguished according to the action. get field
takes the value, and
set field updates the value.
Unification of naming
table - users
···use Illuminate\Support\Facades\DB;··· Schema::create('balance_logs', function (Blueprint $table) { $table->id(); $table->string('username', 32)->unique()->nullable(false)->comment('名称'); $table->string('password', 128)->nullable(false)->comment('密码'); $table->unsignedInteger('age', 3)->default(0)->comment('年龄'); $table->string('token', 128)->nullable(true)->comment('登录态'); $table->dateTime('created_at')->useCurrent(); $table->dateTime('updated_at')->useCurrent(); $table->index('username', 'username_index'); }); DB::statement("ALTER TABLE `users` comment '用户表'");···
<?phpnamespace App\Http\Controllers\Api\v1;use App\Http\Controllers\Controller;use Illuminate\Http\Request;use App\Models\User;class UserController extends Controller{ public function index(Request $request) { // todo }// index() end public function show(Request $request) { // 变量命名,对应的是表字段的话,变量名建议以该字段为名, // 注释时采用 表名.字段 的形式 // users.username $username = $request->post('username'); }// show() end public function store(Request $request) { $user_id = $request->post('user_id');// users.id $age = $request->post('age'); // users.age // 更新数据 User::where('id', $user_id)->update(['age' => $age]); }// store() end}
$ php artisan my:user
table name as the keyword. Naming-Mind Map
Hope for my personal suggestions , can be promoted and popular among students. Thank you students for reading. Remember to
like, and forward.
The above is the detailed content of Share your personal recommended programming specifications for laravel or other frameworks. For more information, please follow other related articles on the PHP Chinese website!