Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Elounk从wordpress数据库检索用户和用户元表的关系_Php_Mysql_Wordpress_Eloquent_Laravel 5 - Fatal编程技术网

Php 使用Elounk从wordpress数据库检索用户和用户元表的关系

Php 使用Elounk从wordpress数据库检索用户和用户元表的关系,php,mysql,wordpress,eloquent,laravel-5,Php,Mysql,Wordpress,Eloquent,Laravel 5,我试图在自定义脚本中访问用户的名字,该脚本使用Eloquent检索数据(实际上是在Laravel 5中) 所以 下面是我如何定义关系(一对多)//正确吗??Wp大师可能会告诉我们 在User模型中,我有 public function usermeta() { return $this->hasMany('App\Usermeta','user_id'); } 在我的UserController中,我有 $users = \App\User::with('use

我试图在自定义脚本中访问用户的
名字
,该脚本使用Eloquent检索数据(实际上是在Laravel 5中)

所以

下面是我如何定义关系(一对多)//正确吗??Wp大师可能会告诉我们

User
模型中,我有

public function usermeta() {
        return $this->hasMany('App\Usermeta','user_id');
    }
在我的
UserController
中,我有

 $users = \App\User::with('usermeta')->get(); 
这里是我在视图中访问它的方式

@foreach ($users as $user)
      <tr>
            <th>{{$user->ID}}</th>
            <th>{{$user->usermeta->/*how to access particular meta key here*/}}</th>
            <th>{{$user->display_name}}</th>
      </tr>
@endforeach
但是,由于
meta_键可以是任何东西,并且可能位于其他位置。
如果
meta\u键
first\u name
,则
$usermeta[1]
具有该键的列值数组,依此类推

 if ($usermeta->count() > 0) {
        return $usermeta[/*This cannot be hard code */]->meta_value;
    }

如何解决它

Usermeta是一个hasMany关系,这意味着Eloquent将返回一个包含多个Usermeta模型的集合对象

您可以这样做:

{{ $user->usermeta[0]->first_name }}
或者这个:

@foreach($user->usermeta as $usermeta)
    {{ $usermeta->first_name }}
@endforeach
编辑


要将
meta_key
meta_value
用作键值对,可以在
User
上编写访问器方法:

public function getMetaValue($key) {
    $usermeta = $this->usermeta->filter(function ($usermeta) use ($key) {
        return $usermeta->meta_key === $key;
    });

    if ($usermeta->count() > 0) {
        return $usermeta[0]->meta_value;
    }
}
然后进入刀片,如下所示:

{{ $user->getMetaValue('first_name') }}
但在高容量时,这将变得缓慢

编辑2

上述方法实际上应如下所示,以确保数组键存在:

public function getMetaValue($key) {
    $usermeta = $this->usermeta->filter(function ($usermeta) use ($key) {
        return $usermeta->meta_key === $key;
    });

    if ($usermeta->count() > 0) {
        return $usermeta->first()->meta_value;
    }
}

是的,我在
dd($users)
中看到了这些集合,我想知道如何访问该数组。因此,当使用仍然返回null:-(您能在dd($users)或dd($users->usermeta)中看到您试图访问的usermeta属性时,
$user->usermeta[0]->first_name
)是的,我可以检查这个转储,我甚至将meta_键更改为
昵称
,因为这是在第0个位置可用的,但它仍然返回null
Usermeta
的模型属性是
meta_键
meta_值
。您应该能够访问
$user->Usermeta[0]->meta_key
$user->usermeta[0]->meta_value
但是
first_name
不是
usermeta
上的属性,所以$user->usermeta[0]->first_name将返回
null
。要使用
meta_key
meta_value
作为键值对,您可以在
User
上编写访问器方法。我将编辑我的答案以演示。
public function getMetaValue($key) {
    $usermeta = $this->usermeta->filter(function ($usermeta) use ($key) {
        return $usermeta->meta_key === $key;
    });

    if ($usermeta->count() > 0) {
        return $usermeta->first()->meta_value;
    }
}