那是控制器
public function hitung($request, $response, $args)
{
$datauser = User::with(['usia','aktivitas'])->find($args['id']);
$tinggi = $datauser['tinggi'];
$berat = $datauser['berat'];
$nama = $datauser['nama'];
$umur = $datauser['umur'];
$aktivitas = $datauser['aktivitaas_id'];
$usia = $datauser['usia_id'];
$nilai = $datauser->aktivitas->nilai;
$energy = $datauser->usia->energy;
$protein = $datauser->usia->protein;
$lemak = $datauser->usia->lemak;
$karbohidrat = $datauser->usia->karbohidrat;
$amb = 655 + (9.6 * $berat) + (1.8 * $tinggi) - (4.7 * $umur);
$amb = round($amb);
$energytotal = $amb * $nilai + $energy;
$energytotal = round($energytotal);
$protein = (15 * $energytotal / 100) + $protein;
$protein = round($protein);
$lemak = (25 * $energytotal / 100) + $lemak;
$lemak = round($lemak);
$karbohidrat = ($energytotal - ($protein + $lemak)) + $karbohidrat;
$karbohidrat= round($karbohidrat);
return $response ->withJson([
'Nama' => $nama,
'total_energy' => $energytotal ,
'Protein'=> $protein,
'lemak'=> $lemak,
'Karbohidrat'=> $karbohidrat,
]);
}
这就是用户模型:
<?php
namespace App\models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public $timestamps = false;
protected $fillable = ['username', 'password', 'nama', 'tinggi', 'berat', 'umur', 'usia_id', 'aktivitas_id'];
protected $table = "users";
public function usia()
{
return $this->belongsTo('App\Models\Usia');
}
public function aktivitas()
{
return $this->belongsTo('App\Models\Aktivitas');
}
public function forum()
{
return $this->hasMany('App\Models\Forum');
}
}
这就是 aktivitas 模型:
<?php
namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Aktivitas extends Model
{
protected $table = "aktivitas";
public function user()
{
return $this->hasMany('App\Models\User');
}
}
这就是usia模式,
<?php
namespace App\models;
use Illuminate\Database\Eloquent\Model;
class Usia extends Model
{
protected $table = "usia";
public function user()
{
return $this->hasMany('App\Models\User');
}
}
此代码在本地工作,但是当我托管它时,它没有工作,请帮助我的考试...
我认为问题出在这段代码中,
$datauser = User::with(['usia','aktivitas'])->find($args['id']);
有什么不同的方法吗?