问题描述
我的用户模型:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'firstName','secondName','email','city','phoneNumber','password','profilePicture'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password','remember_token',];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',];
public function ratings()
{
return $this->belongsToMany(Ratings::class,'ratings_has_users','users_id','ratings_id')->withTimestamps();
}
}
我的评分模型:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Ratings extends Model
{
public function user(){
return $this->belongsToMany(User::class,'ratings_id')->withTimestamps();
}
}
迁移以创建表“ ratings_has_users”
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRatingsHasUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('ratings_has_users',function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('users_id');
$table->unsignedBigInteger('ratings_id');
$table->timestamps();
});
Schema::table('ratings_has_users',function (Blueprint $table) {
$table->foreign('users_id')
->references('id')
->on('users');
});
Schema::table('ratings_has_users',function (Blueprint $table) {
$table->foreign('ratings_id')
->references('id')
->on('ratings');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('ratings_has_users');
}
}
迁移到创建“用户”表
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users',function (Blueprint $table) {
$table->id();
$table->string('firstName',55);
$table->string('secondName',55);
$table->string('email',55)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('city',55);
$table->text('description')->nullable();
$table->string('phoneNumber',11);
$table->string('profilePicture',255)->default('profile.jpg');
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
迁移以创建“评分”表:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateRatingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('ratings',function (Blueprint $table) {
$table->id();
$table->integer('stars');
$table->mediumText('note');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('ratings');
}
}
然后在刀片中进行编码:
@foreach($ratings as $rating)
{{ dd($rating->user->firstName) }}
@endforeach
我不知道为什么会看到此错误消息:此集合实例上不存在属性[firstName]。 (查看:/data/www/twoj-trener/resources/views/trainer_page/reviews.blade.php)
当我更改此代码时:
@foreach($ratings as $rating)
{{ dd($rating->user) }}
@endforeach
我知道了:
Illuminate\Database\Eloquent\Collection {#1291 ▼
#items: []
}
解决方法
评级具有用户(更好的名称为用户,因为属于ToMany )。如果 belongsToMany 关系,则该关系方法(在这种情况下为 user )将提供集合(列出相关记录)。
您可以执行以下操作
from pyspark.sql.functions import col
rows = [
("Ali",[100]),("Barbara",1,[300,250,100]),("Cesar",[350,("Dongmei",[400,("Eli",2,[250]),("Florita",[500,300,("Gatimu",3,100])
]
df = spark.createDataFrame(rows,["name","department","score"])
df.orderBy(col("department"),col("name").desc()).show()
,
评分与用户之间的关系应为users()
(以S表示复数),因为它是一个带有数据透视表的beforeToMany。
如果您希望每个用户的分级都是唯一的,请考虑更改分级表的迁移
public function up()
{
Schema::create('ratings',function (Blueprint $table) {
$table->id();
$table->integer('stars');
$table->mediumText('note');
$table->unsignedBigInteger('user_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
并将关系更改为belongsTo
如果您的结构正确无误,则需要先获取用户,然后才能访问其属性
@foreach($ratings as $rating)
{{ dd($rating->users->first() ?? $rating->users->first()->firstName) }}
@endforeach