[英]Linking Multiple Tables in Laravel using Eloquent
I have 3 tables user table, countries table, personal_details table.我有 3 个表用户表、国家表、personal_details 表。 The structure is as follows
结构如下
Users:用户:
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();
});
Countries:国家:
Schema::create('countries', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('countrycode');
$table->string('countryname');
$table->string('code');
$table->string('flag');
$table->timestamps();
});
Personal Details:个人资料:
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();
});
On the welcome page i need to display users profiles including country and associated flag.在欢迎页面上,我需要显示用户个人资料,包括国家和相关标志。 here is the code im using
这是我使用的代码
Route路线
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'));
});
and the View和视图
@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
I am receiving the error Trying to get property 'flag' of non-object.我收到错误尝试获取非对象的属性“标志”。 Please help
请帮忙
In this line {{$users->personaldetails->country->flag}}
you are calling for relational
data.在这一行
{{$users->personaldetails->country->flag}}
您正在调用relational
数据。 Your relational
data is working in chain.您的
relational
数据正在链中工作。 First $user
is calling relation of personal_details
table.第一个
$user
是调用personal_details
表的关系。 In the personal_details
table you have stored user_id
so the relationship is working.在
personal_details
表中,您已经存储了user_id
因此关系有效。 But when you are chaining countries
table using personaldetails
there will be no result as no relationship exists.但是,当您使用
personaldetails
链接countries
表时,将不会有任何结果,因为不存在任何关系。 You can see that your countries
table holds information of only about country, no id
of personal_details
is stored as foreign key
in your countries
table.您可以看到您的
countries
表仅包含有关国家/地区的信息,而没有personal_details
id
作为foreign key
存储在您的countries
表中。 If you want to retrieve data through relations you must store id of personal_details
table as foreign key
in countries
table.如果您想通过关系检索数据,您必须将
personal_details
表的 id 作为foreign key
在countries
表中。 So if you store personal_detail_id
in your country table, there should be no problem.所以如果你将
personal_detail_id
存储在你的country表中,应该没有问题。
Your countries
table should look like the following:您的
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.