doubinei1457 2018-09-21 15:23
浏览 39
已采纳

Laravel和mySQL与关系表

I need to create an API with laravel and PHP. I've created api routes to GET all users and GET all devices related to the user.

I've made the following tables in 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:

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:

$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?

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?

  • 写回答

2条回答 默认 最新

  • dpa31905 2018-09-21 15:59
    关注

    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:

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加