php - Laravel 不显示数据

标签 php laravel

我想显示 user 中的数据与 profile 链接的表 table 。我在 vardump 中获取了数据,但我不知道如何一一显示它。 这是我的功能:

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();
}

它返回这个:

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)
      }
  }
}

但是当我 echo $user->profile->age;它什么也没显示。 有关信息,我链接了表格 userprofile与彼此return $this->belongsTo('App\User');return $this->hasMany('App\Profile'); 。我获取了数据,但无法一一显示。

最佳答案

一对一:

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

请记住,如果用户有多个配置文件,则关系函数名称应该是复数,例如 public functionprofiles()

关于php - Laravel 不显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38410927/

相关文章:

Php Redis 连接问题

php - 如何在android studio中从mysql检索数据

php - 如何将此动态列表拆分为 3 列?

Laravel Artisan make 命令问题

Javascript - 选择网格元素时显示 div

php - Laravel 5 hasManyThrough

javascript - laravel 方法内部如何在 javascript 代码中使用 javascript 变量?

javascript - 是否可以在应用程序中同时使用 Vue.js 组件和传统 Vue 实例?

php - Apache/PHP 和 Systemd/node 可读的环境变量

php - 使用ajax获取javascript变量