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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题