1

I got a User Class

class User extends Model implements
    AuthenticatableContract,
    CanResetPasswordContract,
    AuthorizableContract,
    MustVerifyEmailContract {

    //use HasApiTokens, HasFactory, Notifiable, MustVerifyEmail;


    use MustVerifyEmail, HasApiTokens,Authenticatable, Authorizable, CanResetPassword, Notifiable;


    protected $table = 'users';

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'username',
        'email',
        'password'

    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];


    public function devices(){
   
        if($this->is_admin){
            return Device::query();
        }
       
        return $this->belongsToMany(Device::class);
    }

    public function testseries(){
        $devices = $this->devices();
        dd($devices->getRelation('testseries')->get()->toArray());
    }
}

In my case, should I do $devices->get() it gives me a empty collection.

Now I want to get the relation from devices to testseries. But i dont want to get an collection.

I want to call manually later somewhere $user->testseries()->get() because maybe I want to modify something with where.

Is that possible?

Currently $devices->getRelation('testseries')->get() returns an collection of all testseries while $devices->get() give me an empty collection of devices. But I expect an empty collection too because there are no devices, so there shouldnt be any testseries for the user.

I dont want to use $devices->with('testseries') because then I'll get a Collection of Device models with the relation. But I Want a collection of testseries instead.

Is there any way or workaround to get that?

I tried it with pluck, load and with. But nothing gives me my expected result and they are already loaded in a wrong collection format because I want a collection of testserie Model.

EDIT:

Here are all other Model classes:

class Testserie extends Model
{
    use HasFactory;



    /**
     * @var string[]
     */
    protected $fillable = [
        'device_id',
        'date',

    ];

    public function measurements(): HasMany {
        return $this->hasMany(Measurement::class);
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function device(): \Illuminate\Database\Eloquent\Relations\BelongsTo {
        return $this->belongsTo(Device::class);
    }
}
class Device extends Model
{
    use HasFactory;


    /**
     * @return BelongsToMany
     */
    public function users(): BelongsToMany {
        return $this->belongsToMany(User::class);
    }

    /**
     * @return HasMany
     */
    public function testseries(): HasMany {
        return $this->hasMany(Testserie::class);
    }


}

Here is a picture of the database structure

V1337
  • 13
  • 1
  • 6
  • 2
    what's the difference between **device** and **tesetseries** . I'm trying figure out what you are trying to achieve – ket-c Dec 16 '22 at 14:17
  • Devices are Scales. Scales can measure a series, called testseries. There is also a table called "measurements" which contains all single values with a foreign key to the testseries. I want to load all devices, which the user has access to and then all testseries across all devices. The testseries has the main Informations about a series like (measurement date, internal id for the scale because the devices doesnt have a access to the database) – V1337 Dec 16 '22 at 14:42
  • so do you have Devices table, Series table and TestSeries table? – ket-c Dec 16 '22 at 15:39
  • I have `testseries` `devices` `users` `measurements` and `device_user`. The table `testseries` is connected with `devices` with 1:n relation. `device` and `user` are connected via pivot with n:m relation. `measurements` and `testseries` are connecteid with 1:n relation. So a User can only access to the `testseries` table via the relation to the `devices` table. So I have to go from the User table through the `devices` Table to the `testseries` table. – V1337 Dec 16 '22 at 16:06
  • 1
    The function you need to use is **belongsToManyThrough** since you have a pivot table. But i dont know exactly how they are connected so I can write the correct codes for you. If you can list the schema of these table I can come up with something for you – ket-c Dec 16 '22 at 22:30
  • @V1337 update your original question with answers to the questions ^^^ it will help a lot solving your problem – UnderDog Dec 19 '22 at 06:51
  • 1
    @UnderDog I updated my question with more code and a picture of the database structure as ER Diagram. Thanks for ur help! Im new to Laravel and trying to learn it while trying to clone a project which is made in a other language and I want to make it as Webapp. – V1337 Dec 19 '22 at 16:44
  • @ ket-c Why I need belongsToManyThrough? If I made in the User model `belongsToMany(Device::class)` and inside the device Class `belongsToMany(User::class)` then its automatic connected between both tables. Also it works at the moment. Dont know if it will messed something up later. I updated my Code above in the question. – V1337 Dec 19 '22 at 16:45

0 Answers0