简体   繁体   English

Laravel和带有关系表的MySQL

[英]Laravel and mySQL with relational tables

I need to create an API with laravel and PHP. 我需要使用laravel和PHP创建一个API。 I've created api routes to GET all users and GET all devices related to the user. 我创建API路线GET所有用户和GET与用户的所有设备。

I've made the following tables in mySQL: 我在mySQL中制作了以下表格:

Devices: 设备:

increments('id');
string('name');
longText('description');

Table for relations between users and devices: 用户和设备之间的关系表:

increments('id');
unsignedInteger('user_id');
unsignedInteger('device_id');

foreign('user_id')->references('id')->on('users');
foreign('device_id')->references('id')->on('devices');

Variables: 变量:

increments('id');
string('type');
unsignedInteger('device_id');
longText('description');

foreign('device_id')->references('id')->on('devices');

And the models have the relationscode: 模型具有关系代码:

User Model: 用户模型:

public function deviceVariables() {
    return $this->hasMany('App\DeviceVariable');
}

public function devices()
{
    return $this->belongsToMany('App\Device');
}

Device Model: 设备型号:

public function users()
{
    return $this->belongsToMany('App\User');
}

public function variables()
{
    return $this->hasMany('App\DeviceVariable');
}

And finally the DeviceVariable Model: 最后是DeviceVariable模型:

public function device()
{
    return $this->belongsTo('App\Device');
}

public function user()
{
    return $this->belongsTo('App\User');
}

I am able to show all the devices related to an authenticated user, but i am unable to show all the variables related to the devices that are related to that user. 我能够显示与经过身份验证的用户相关的所有设备,但无法显示与与该用户相关的设备相关的所有变量。

This code (index method of DeviceVariablecontroller ) is the closest i've come to getting the variables: 这段代码( DeviceVariablecontroller索引方法)是我最接近获取变量的代码:

$counter = 1;
$arrayIndex = 0;
while($counter <= 10) {
    if(auth()->user()->devices()->find($counter)) {
        $variables[$arrayIndex] = auth()->user()->devices()->find($counter)->variables;
        $arrayIndex++;
    }
    $counter++;
}

Is there a way to make an array of all the user's devices' IDs and the loop through them?- or is there a smarter way to get all the variables of all the user's devices? 有没有办法使所有用户设备的ID组成数组并循环遍历它们?或者有一种更聪明的方法来获取所有用户设备的所有变量?

EDIT: Comment got me both the devices aswell as the each device variables. 编辑:注释使我同时获得了两个设备以及每个设备变量。

$variables = auth()->user()->devices()->with('variables')->get();
return response()->json([
    'success' => true,
    'data' => $variables
]);

How can i get the variables ONLY without the device info? 如何仅在没有设备信息的情况下获取变量?

Maybe something like this: 也许是这样的:

$variables = auth()->user()->devices()->with('variables')->get();

This will eager load relationships that devices had. 这将渴望设备具有的负载关系。

For accessing only users variables you can use has-many-throught relationship like mentioned in docs: 要仅访问用户变量,您可以使用docs中提到的has-many-throught关系:

https://laravel.com/docs/5.7/eloquent-relationships#has-many-through https://laravel.com/docs/5.7/eloquent-relationships#has-many-through

You can use a BelongsToMany relationship to get the variables directly: 您可以使用BelongsToMany关系直接获取变量:

public function variables()
{
    return $this->belongsToMany('App\DeviceVariable', 'device_user',
        null, 'device_id', null, 'device_id');
}

return response()->json([
    'success' => true,
    'data' => auth()->user()->variables
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM