0

I am facing a weird error right now

In my controller, when I import the class user like this

use Illuminate\Foundation\Auth\User;

It works when I use eloquent like

public function index()

    {

        $farms = User::where('role_id', 3)->get();
        $user = Auth::user();
        $animal = Animal::all();
        return view('clinic.index', compact('user', 'animal', 'farms'));
    }

But refuses to work when it comes to table relationships like

public function show($id)
    {
        $farms = User::with(['animals'])->findOrFail($id);
        return view('clinic.show',compact('farms'));
    }

showing me this error

"Call to undefined relationship [animals] on model [Illuminate\Foundation\Auth\User]"

But whenever I import the user class as App\User in my controller,

It works in the relationship but refuses to work with the eloquent showing this error

"Call to a member function get() on null"

Now I am kinda confused. Any help will be welcomed

App\User

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Database\Eloquent\Model;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    public static function where(string $string, int $int)
    {
    }

    public static function select(array $array)
    {
    }

    public function role(){
        return $this->belongsTo(Role::class);
    }
    public function animals(){
        return $this->hasMany(Animal::class);
    }

    public function clinics(){
        return $this->hasMany(Clinic::class);
    }

    public function slaughter(){
        return $this->hasMany(Slaughter::class);
    }

    public function address(){
        return $this->belongsTo(Address::class);
    }
    /**
     * 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',
    ];
}
9
  • 1
    You again! use App\User is what you should be using. Post up your User model please. Commented Oct 30, 2019 at 23:42
  • It should not be an array Commented Oct 30, 2019 at 23:48
  • @FAKETAXI How am I making my elequent work then Commented Oct 30, 2019 at 23:51
  • @onlineThomas what should not be an array? Commented Oct 30, 2019 at 23:52
  • 2
    @onlineThomas laravel.com/docs/5.8/eloquent-relationships#eager-loading -- array isn't the issue Commented Oct 31, 2019 at 0:06

1 Answer 1

1

The Illuminate\Foundation\Auth\User class is the parent of the App\User class and animals relation set in the App\Userclass. So you can't call animals relation from Illuminate\Foundation\Auth\User class.

You should remove these functions from the App\User Model:

public static function where(string $string, int $int)
{
}

public static function select(array $array)
{
}
Sign up to request clarification or add additional context in comments.

3 Comments

So How am I supposed to do this cause the 'where' eloquent is not working with the App\User
@lmmeyti I have added it
@Jimmyjbk Updated my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.