douzhuiqing1151 2016-07-16 11:51
浏览 73
已采纳

Laravel不显示数据

I want to display data from the user table linked with the profile table. I get the data in my vardump but I don't know how to display it one for one. This is my function :

public function show($id)
{
    //Get data from user
    $user = User::find($id);
    //Get the horse count connect to the user
    $horse_count = DB::table('horses')->where('user_id', $id)->count();

    var_dump($user->profile);
    die();
}

It returns this :

object(Illuminate\Database\Eloquent\Collection)#188 (1) {
  ["items":protected]=>
  array(1) {
    [0]=>
    object(App\Profile)#189 (24) {
      ["connection":protected]=>
      NULL
      ["table":protected]=>
      NULL
      ["primaryKey":protected]=>
      string(2) "id"
      ["keyType":protected]=>
      string(3) "int"
      ["perPage":protected]=>
      int(15)
      ["incrementing"]=>
      bool(true)
      ["timestamps"]=>
      bool(true)
      ["attributes":protected]=>
      array(10) {
        ["id"]=>
        int(1)
        ["user_id"]=>
        int(2)
        ["country"]=>
        string(11) "Netherlands"
        ["age"]=>
        string(2) "21"
        ["recent_online"]=>
        string(0) ""
        ["visitors"]=>
        string(4) "2143"
        ["profile"]=>
        string(17) "Hoi ik ben Martin"
        ["remember_token"]=>
        NULL
        ["created_at"]=>
        NULL
        ["updated_at"]=>
        NULL
      }
      ["original":protected]=>
      array(10) {
        ["id"]=>
        int(1)
        ["user_id"]=>
        int(2)
        ["country"]=>
        string(11) "Netherlands"
        ["age"]=>
        string(2) "21"
        ["recent_online"]=>
        string(0) ""
        ["visitors"]=>
        string(4) "2143"
        ["profile"]=>
        string(17) "Hoi ik ben Martin"
        ["remember_token"]=>
        NULL
        ["created_at"]=>
        NULL
        ["updated_at"]=>
        NULL
      }
      ["relations":protected]=>
      array(0) {
      }
      ["hidden":protected]=>
      array(0) {
      }
      ["visible":protected]=>
      array(0) {
      }
      ["appends":protected]=>
      array(0) {
      }
      ["fillable":protected]=>
      array(0) {
      }
      ["guarded":protected]=>
      array(1) {
        [0]=>
        string(1) "*"
      }
      ["dates":protected]=>
      array(0) {
      }
      ["dateFormat":protected]=>
      NULL
      ["casts":protected]=>
      array(0) {
      }
      ["touches":protected]=>
      array(0) {
      }
      ["observables":protected]=>
      array(0) {
      }
      ["with":protected]=>
      array(0) {
      }
      ["morphClass":protected]=>
      NULL
      ["exists"]=>
      bool(true)
      ["wasRecentlyCreated"]=>
      bool(false)
      }
  }
}

But when I echo $user->profile->age; it shows nothing. For info, I linked the tables user and profile with eachother with return $this->belongsTo('App\User'); and return $this->hasMany('App\Profile');. I get the data but I just can't display it one for one.

  • 写回答

1条回答 默认 最新

  • douweidao3882 2016-07-16 12:13
    关注

    One per one:

    foreach (User::find($id)->profile as $profile) {
         // Do what you want, e.g.
         echo $profile->age;
    }
    

    Remember, if a user has many profiles then the relation function name should be plural like public function profiles()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题