У меня есть таблица пользователей и user_profiles. Я пытаюсь заполнить данные профилей пользователей в таблице user_profiles, но получаю ошибку
"Undefined offset: 0"
Возможно, проблема в UserProfileSeeder. php, где я вызываю объект пользователя с отношением профиля пользователя, там он возвращает пустой массив. Но я не знаю почему. Любая помощь приветствуется. Вот мой код.
ChunkSeeder. php
<?php
/**
* Trait ChunkSeeder
*/
trait ChunkSeeder
{
/**
* @param $seed
* @param callable $callback
*/
public function seedChunks($seed, $callback)
{
var_dump(count($seed[0]));
$chunkSize = floor(65535 / count($seed[0]));
$chunks = array_chunk($seed, $chunkSize);
foreach ($chunks as $chunk) {
call_user_func($callback, $chunk);
dump("Written " . count($chunk) ." records of MAX {$chunkSize}");
}
}
}
UserProfileSeeder. php
<?php
use App\User;
use App\UserAstro;
use App\UserBodyType;
use App\UserChildren;
use App\UserEducation;
use App\UserOrientation;
use App\UserProfile;
use App\UserRelationshipStatus;
use App\UserSmoke;
use Illuminate\Database\Seeder;
class UserProfileSeeder extends Seeder
{
use ChunkSeeder;
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$user = new User;
$users = $user->userProfile()->get('id')->pluck('id');
$astros = UserAstro::all('id')->pluck('id');
$bodyTypes = UserBodyType::all('id')->pluck('id');
$children = UserChildren::all('id')->pluck('id');
$education = UserEducation::all('id')->pluck('id');
$orientations = UserOrientation::all('id')->pluck('id');
$relationshipStatuses = UserRelationshipStatus::all('id')->pluck('id');
$smokes = UserSmoke::all('id')->pluck('id');
$seed = [];
foreach ($users as $userId) {
$seed[] = factory(UserProfile::class)->make(
[
'user_id' => $userId,
'astro_id' => $astros->random(1)[0],
'body_type_id' => $bodyTypes->random(1)[0],
'children_id' => $children->random(1)[0],
'education_id' => $education->random(1)[0],
'orientation_id' => $orientations->random(1)[0],
'relationship_status_id' => $relationshipStatuses->random(1)[0],
'smoke_id' => $smokes->random(1)[0],
]
)->toArray();
}
$this->seedChunks($seed, [UserProfile::class, 'insert']);
}
}
UserProfile. php
<?php
namespace App;
use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
class UserProfile extends Model
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id',
'name',
'age',
'height',
'mobile_number',
'mobile_public',
'about_me',
'distance',
'active',
'premium',
'astro_id',
'body_type_id',
'children_id',
'education_id',
'orientation_id',
'relationship_status_id',
'smoke_id',
];
public function user()
{
return $this->belongsTo(User::class);
}
public function astro()
{
return $this->belongsTo(UserAstro::class);
}
public function bodyType()
{
return $this->belongsTo(UserBodyType::class);
}
public function children()
{
return $this->belongsTo(UserChildren::class);
}
public function education()
{
return $this->belongsTo(UserEducation::class);
}
public function orientation()
{
return $this->belongsTo(UserOrientation::class);
}
public function relationshipStatus()
{
return $this->belongsTo(UserRelationshipStatus::class);
}
}
Пользователь. php
<?php
namespace App;
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 = [
'gender_id',
'username',
'email',
'first_name',
'last_name',
'date_of_birth',
'premium',
'mobile_number',
'mobile_verification_id',
'inform_new_message',
'inform_gift_received',
'inform_new_singles',
'inform_whatchlist',
'inform_when_liked',
'inform_when_matched',
];
protected $dates = [
'date_of_birth',
'email_verified_at',
'mobile_verified_at',
'premium_purchased_at',
];
/**
* 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 = [
'premium' => 'boolean',
'inform_new_message',
'inform_gift_received',
'inform_new_singles',
'inform_whatchlist',
'inform_when_liked',
'inform_when_matched',
];
public function gender()
{
return $this->belongsTo(Gender::class, 'gender_id', 'id');
}
public function userProfile()
{
return $this->hasOne(UserProfile::class);
}
public function providers()
{
return $this->hasMany(Provider::class);
}
public function photos()
{
return $this->hasMany(Photo::class);
}
public static function getIdFromName(string $name)
{
if ($user = User::where('name', $name)->first()) {
return $user->id;
}
return abort(404);
}
}
UserProfileProperties. php
<?php
use App\UserAstro;
use App\UserBodyType;
use App\UserChildren;
use App\UserEducation;
use App\UserOrientation;
use App\UserRelationshipStatus;
use App\UserSmoke;
use Illuminate\Database\Seeder;
const FOR_INSERT = [
UserAstro::class => self::ASTRO,
UserBodyType::class => self::BODY_TYPE,
UserChildren::class => self::CHILDREN,
UserEducation::class => self::EDUCATION,
UserOrientation::class => self::ORIENTATION,
UserRelationshipStatus::class => self::RELATIONSHIP_STATUS,
UserSmoke::class => self::SMOKE,
];
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
foreach (self::FOR_INSERT as $key => $value) {
$this->insert($value, $key);
}
}
/**
* @param array $data
* @param string $model
*/
private function insert(array $data, string $model)
{
foreach ($data as $item) {
$model::create(
[
'name' => $item,
]
);
}
}
}