簡體   English   中英

使用 Eloquent 在 Laravel 中鏈接多個表

[英]Linking Multiple Tables in Laravel using Eloquent

我有 3 個表用戶表、國家表、personal_details 表。 結構如下

用戶:

Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('role');
    $table->string('email')->unique();
    $table->integer('terms');
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

國家:

Schema::create('countries', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('countrycode');
    $table->string('countryname');
    $table->string('code');
    $table->string('flag');
    $table->timestamps();
});

個人資料:

Schema::create('personal_details', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->integer('user_id');
    $table->string('first_name');
    $table->string('last_name');
    $table->string('sex');
    $table->date('date_of_birth');
    $table->string('country');
    $table->string('freelancer_type');
    $table->text('about_me');
    $table->timestamps();
});

在歡迎頁面上,我需要顯示用戶個人資料,包括國家和相關標志。 這是我使用的代碼

路線

Route::get('/', function () {
    $category = \App\Category::all();
    $user = \App\User::all()->where('role', '==', 'Freelancer');
    $post = \App\Post::paginate(3);
    return view('welcome', compact('category', 'user', 'post'));
});

視圖

@foreach($user as $users)
<div class="col-lg-3 md-6 xs-6" style="text-align: center">
    <div class="card-deck">
        <div class="card shadow p-3 mb-5 bg-white rounded">
            <div class="card-body">
                <img src="/profile_images/{{$users->profileimage->image}}"
                     style="width: 100px; height: 100px; border-radius: 50%">
                <p><span style="font-weight: 900; color: #009dba">{{$users->personaldetails->first_name}} {{$users->personaldetails->last_name}}</span>
                    <br/>
                    <span style="font-size: 11px">
                          {{$users->expertise->profession}}</span>
                </p>
                <p style="font-size: 11px">{{$users->personaldetails->country}},
                    ZAR{{$users->expertise->hourly_rate}}/hr</p>
                <hr>
                <p style="font-size: 11px">{{str_limit($users->personaldetails->about_me, $limit = 80, $end =
                    '...')}}</p>
                {{$users->personaldetails->country->flag}}
                <div style="padding-top: 10px"></div>
                <a href="/users/{{$users->id}}">
                    <button>View Profile</button>
                </a>
            </div>
        </div>
    </div>
</div>
@endforeach

我收到錯誤嘗試獲取非對象的屬性“標志”。 請幫忙

在這一行{{$users->personaldetails->country->flag}}您正在調用relational數據。 您的relational數據正在鏈中工作。 第一個$user是調用personal_details表的關系。 personal_details表中,您已經存儲了user_id因此關系有效。 但是,當您使用personaldetails鏈接countries表時,將不會有任何結果,因為不存在任何關系。 您可以看到您的countries表僅包含有關國家/地區的信息,而沒有personal_details id作為foreign key存儲在您的countries表中。 如果您想通過關系檢索數據,您必須將personal_details表的 id 作為foreign keycountries表中。 所以如果你將personal_detail_id存儲在你的country表中,應該沒有問題。

您的countries表應如下所示:

Schema::create('countries', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('personal_detail_id');
        $table->string('countrycode');
        $table->string('countryname');
        $table->string('code');
        $table->string('flag');
        $table->timestamps();
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM