Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dr24's avatar
Level 2

Problem with seeding user profile data in Laravel

I have users and user_profiles table. I am trying to seed user profiles data in user_profiles table but I get error

"Undefined offset: 0"

It is possible that the problem is in UserProfileSeeder.php where I call user object with user profile relationship, there it returns empty array. But I don't know why. Any help is appreciated. Here is my code.

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);
    }

}

User.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);
        
    }
}
0 likes
4 replies
ahkeravi's avatar
 var_dump(count($seed[]));

remove 0 offset in ChunkSeeder.php

dr24's avatar
Level 2

Now I get Cannot use [] for reading error

a4ashraf's avatar

Hello @gacho

the problem in you UserProfileSeeder.php following code will fix your issue

public function run()
{

    $user =  User::all();

    $astros = UserAstro::pluck('id');
    $bodyTypes = UserBodyType::pluck('id');
    $children = UserChildren::pluck('id');
    $education = UserEducation::pluck('id');
    $orientations = UserOrientation::pluck('id');
    $relationshipStatuses = UserRelationshipStatus::pluck('id');
    $smokes = UserSmoke::pluck('id');

    foreach ($users as $user) {

        $seed[] = factory(UserProfile::class)->make(
            [
                'user_id' => $user->id->random(1)[0],
                '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,
            ]
        )->toArray();
    }

    $this->seedChunks($seed, [UserProfile::class, 'insert']);
}

now you need to want to show the array in your trait file you should follow this


        dd(count($seed));

hope this will help you, keep here updated

a4ashraf's avatar

@gacho

even you can more clean the code like this



public function run()
{

    $user =  User::all();

    $astros = UserAstro::pluck('id');
    $bodyTypes = UserBodyType::pluck('id');
    $children = UserChildren::pluck('id');
    $education = UserEducation::pluck('id');
    $orientations = UserOrientation::pluck('id');
    $relationshipStatuses = UserRelationshipStatus::pluck('id');
    $smokes = UserSmoke::pluck('id');

    foreach ($users as $user) {

        $seed[] = factory(UserProfile::class)->make(
            [
                'user_id' => $user->id->random(),
                'astro_id' => $astros->random(),
                'body_type_id' => $bodyTypes->random(),
                'children_id' => $children->random(),
                'education_id' => $education->random(),
                'orientation_id' => $orientations->random(),
                'relationship_status_id' => $relationshipStatuses->random(),
                'smoke_id' => $smokes,
            ]
        )->toArray();
    }

    $this->seedChunks($seed, [UserProfile::class, 'insert']);
}

Please or to participate in this conversation.