codebullet
7 months ago

"Call to undefined method App\User::profile()"

Posted 7 months ago by codebullet

Greetings i need little help here,

I have 2 tables, a table for users, and a table for profiles ( one to one relationship)

I want to get all users without profile

From Tinker i can get $user->profile through relationship no error but when i use

 public function create()
    {
        //
        $departments = Department::all();
        $users = User::has('profile')->get();
        return view('manager.profile.create')->with(compact('departments','users'));
    }

I get this error

C:\xampp\htdocs\fidu\vendor\laravel\framework\src\Illuminate\Support\Traits\ForwardsCalls.php
            if ($matches['class'] != get_class($object) ||
                $matches['method'] != $method) {
                throw $e;
            }
 
            static::throwBadMethodCallException($method);
        }
    }
 
    /**
     * Throw a bad method call exception for the given method.
     *
     * @param  string  $method
     * @return void
     *
     * @throws \BadMethodCallException
     */
    protected static function throwBadMethodCallException($method)
    {
        throw new BadMethodCallException(sprintf(
            'Call to undefined method %s::%s()', static::class, $method
        ));
    }
}
 
Arguments
"Call to undefined method App\User::profile()"

Here is my user Model relationship


<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Laravel\Passport\HasApiTokens;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laratrust\Traits\LaratrustUserTrait;

class User extends Authenticatable
{
    use LaratrustUserTrait;
    use HasApiTokens, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'first_name',
        'last_name',
        'spouse',
    ];



    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];


    protected function blog(){
        return $this->hasMany('App\Blog');
    }

    protected function profile(){
        return $this->hasOne('App\Profile');
    }

Please advise, can't wrap my head around it.

Please help

Please sign in or create an account to participate in this conversation.