Преглед изворни кода

用户信息添加,部门信息

kely пре 1 година
родитељ
комит
4ffe6cc4ab

+ 25 - 0
app/Http/Resources/API/SimpleDepartmentResource.php

@@ -0,0 +1,25 @@
+<?php
+/**
+ * Created by IntelliJ IDEA.
+ * User: kelyliang
+ * Date: 2024/2/18
+ * Time: 下午 03:59
+ */
+
+namespace App\Http\Resources\API;
+
+use Illuminate\Http\Request;
+use Illuminate\Http\Resources\Json\JsonResource;
+
+class SimpleDepartmentResource extends JsonResource
+{
+    public function toArray(Request $request): array
+    {
+        return [
+            'id' => $this->id,
+            'parent_dept ' => $this->parent_dept,
+            'name' => $this->name,
+
+        ];
+    }
+}

+ 1 - 0
app/Http/Resources/API/UserInfoResource.php

@@ -25,6 +25,7 @@ class UserInfoResource extends JsonResource
             'created_at' => (string)$this->created_at,
             'created_by' => new UserProfileResource($this->createdBy),
             'company' => new SimpleCompanyResource($this->company),
+            'department' =>new SimpleDepartmentResource($this->department),
             'role' => new RoleResource($this->role),
         ];
     }

+ 39 - 0
app/Models/Department.php

@@ -0,0 +1,39 @@
+<?php
+/**
+ * Created by IntelliJ IDEA.
+ * User: kelyliang
+ * Date: 2024/2/18
+ * Time: 下午 03:52
+ */
+
+namespace App\Models;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class Department extends Model
+{
+    use HasFactory;
+
+    protected $table = 'department';
+
+    // 设置主键(可选,默认是'id')
+    protected $primaryKey = 'id';
+
+    // 定义允许批量赋值的字段(可选)
+    protected $fillable = ['name', 'parent_id','manager_id'];
+
+
+    // 例如,一个部门可能有多个子部门
+    public function children()
+    {
+        return $this->hasMany(Department::class, 'parent_id');
+    }
+
+    // 或者,一个部门可能属于另一个部门
+    public function parent()
+    {
+        return $this->belongsTo(Department::class, 'parent_id');
+    }
+
+}
+

+ 6 - 0
app/Models/User.php

@@ -26,6 +26,7 @@ class User extends Authenticatable
         'email',
         'password',
         'company_id',
+        'department_id',
     ];
 
     /**
@@ -67,4 +68,9 @@ class User extends Authenticatable
     {
         return $this->belongsTo(User::class, 'created_by');
     }
+
+    public function department(): \Illuminate\Database\Eloquent\Relations\BelongsTo
+    {
+        return $this->belongsTo(Department::class);
+    }
 }

+ 30 - 0
database/migrations/2024_02_18_073118_create_department_table.php

@@ -0,0 +1,30 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::create('department', function (Blueprint $table) {
+            $table->id();
+            $table->string('parent_dept',100)->nullable();
+            $table->string('name',100)->nullable();
+            $table->string('manager_id',100)->nullable();
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::dropIfExists('department');
+    }
+};