国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

增強(qiáng)使用者模型
P粉311464935
P粉311464935 2024-01-10 17:17:58
0
1
504

我正在嘗試使用另一個(gè)表(個(gè)人資料)擴(kuò)展用戶模型以獲取個(gè)人資料圖片、位置等。

我可以重寫(xiě)使用者模型的 index() 函數(shù)來(lái)做到這一點(diǎn)嗎?

目前型號(hào)代碼:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;

    protected $fillable = [
        'name',
        'email',
        'password',
        'user_group'
    ];

    protected $hidden = [
        'password',
        'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

P粉311464935
P粉311464935

全部回覆(1)
P粉378890106

您想要做的是在User 模型和新的Profile 模型之間建立關(guān)係。為此,您首先需要建立一個(gè)模型 Profile 及其關(guān)聯(lián)的 Tabble profiles

php artisan make:model Profile --migration

database\migrations中應(yīng)該有一個(gè)名為2022_11_28_223831_create_profiles_table.php

#的文件

現(xiàn)在您需要新增一個(gè)外鍵來(lái)指示此設(shè)定檔屬於哪個(gè)使用者。

public function up()
{
    Schema::create('profiles', function (Blueprint $table) {
        $table->id();
        // $table->string('path_to_picture')
        // user id
        $table->foreignId('user_id')->constrained()->onDelete('cascade');
        $table->timestamps();
    });
}

現(xiàn)在在您的使用者模型中加入以下函數(shù)

public function profile()
{
    return $this->hasOne(Profile::class);
}

在您的個(gè)人資料模型中

public function user()
{
    return $this->belongsTo(User::class);
}

運(yùn)行php artisan migrate,一切都應(yīng)該按預(yù)期工作

如果您想測(cè)試關(guān)係是否如預(yù)期運(yùn)作,請(qǐng)建立一個(gè)新的測(cè)試案例

php artisan make:test ProfileUserRelationTest

#在tests\Feature\ProfileUserRelationTest.php

#
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use App\Models\User;
use App\Models\Profile;
use Illuminate\Support\Facades\Hash;

class ProfileUserRelationTest extends TestCase
{
    use RefreshDatabase;
    public function test_the_relation_between_user_and_profile_works()
    {
        $user = User::create([
            'name' => 'John Doe',
            'email' => 'jd@example.com',
            'password' => Hash::make('password'),
        ]);
        $profile = new Profile();
        $profile->user_id = $user->id;
        $profile->save();

        $this->assertEquals($user->id, $profile->user->id);
        $this->assertEquals($user->name, $profile->user->name);
        $this->assertEquals($profile->id, $user->profile->id);
    }
}

現(xiàn)在您可以執(zhí)行 php artisan test 來(lái)查看是否一切正常。

小心這會(huì)刷新您的資料庫(kù)! 所以不要在生產(chǎn)中進(jìn)行測(cè)試。

輸出應(yīng)該是這樣的

PASS  Tests\Unit\ExampleTest
  ? that true is true

   PASS  Tests\Feature\ExampleTest
  ? the application returns a successful response

   PASS  Tests\Feature\ProfileUserRelationTest
  ? the relation between user and profile works

  Tests:  3 passed
  Time:   0.35s

了解有關(guān) Laravel 中關(guān)係的更多資訊:https://laravel.com/docs/ 9.x/雄辯關(guān)係

了解更多有關(guān)遷移的資訊:https://laravel.com/docs/9.x/遷移

替代方案

$user = User::create([
    'name' => 'John Doe',
    'email' => 'jd@example.com',
    'password' => Hash::make('password'),
]);

$user->profile()->create(...); // replace the ... with the things you want to insert you dont need to add the user_id since it will automatically added it. It will still work like the one above.
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板