首页 > 解决方案 > 试图获取非对象的属性“user_nicename”

问题描述

我制作了许多数据库应用程序,在我的数据库中具有相同的表名和相同的文件。我怎样才能显示所有数据

这是我的模型

这是我的模型用户

class Users extends Model
{
    // protected $connection = 'mysql4';
    protected $table = ('ilkom_akbar.table_users');
    protected $primaryKey = 'ID';
    protected $fillable = [
        'ID',
        'user_login',
        'user_pass',
        'user_nicename',        
        'user_email',
        'url',
        'user_registered',
        'user_activation_key',
        'user_status',
        'display_name'
    ];

    public function posts()
    {
        return $this->hasMany(Posts::class,'post_author');
    }
}

这是我的模型 users1

class Users1 extends Model
{
    protected $table = 'ilkom_adeputra.table_users';
    protected $primaryKey = 'ID';
    protected $fillable = [
        'ID',
        'user_login',
        'user_pass',
        'user_nicename',
        'user_email',
        'url',
        'user_registered',
        'user_activation_key',
        'user_status',
        'display_name'
    ];

    public function posts()
    {
        return $this->hasMany(Posts1::class,'post_author');
    }
}

这是我的模特帖子

class Posts extends Model
{
    protected $table = 'ilkom_akbar.table_posts'; 
    protected $primaryKey = 'ID';
    protected $fillable = [
        'ID',
        'post_author',
        'post_date',
        'post_date_gmt',
        'post_content',
        'post_title',
        'post_excerpt',
        'post_status',
        'comment_status',
        'ping_status',
        'post_password',
        'post_name',
        'to_ping',
        'pinged',
        'post_modified',
        'post_modified_gmt',
        'post_content_filtered',
        'post_parent',
        'guid',
        'menu_order',
        'post_type',
        'post_mime_type',
        'comment_count'
    ];

    public function users()
    {
        return $this->belongsTo(Users::class,'ID');
    }
}

这是我的模特帖子1

class Posts1 extends Model
{
    protected $table = 'ilkom_adeputra.table_posts'; 
    protected $primaryKey = 'ID';
    protected $fillable = [
        'ID',
        'post_author',
        'post_date',
        'post_date_gmt',
        'post_content',
        'post_title',
        'post_excerpt',
        'post_status',
        'comment_status',
        'ping_status',
        'post_password',
        'post_name',
        'to_ping',
        'pinged',
        'post_modified',
        'post_modified_gmt',
        'post_content_filtered',
        'post_parent',
        'guid',
        'menu_order',
        'post_type',
        'post_mime_type',
        'comment_count'
    ];

    public function users()
    {
        return $this->belongsTo(Users1::class,'ID');
    }
}

这是获取数据

@foreach ($karyawan1->posts as $data)
    <tr>
        <td>{{ $no++}}</td>
        <td>{{ $data->post_title}}</td>
        <td>{{ $data->post_name}}</td>
        <td>{{ $data->post_modified}}</td>
        <td>{{ $data->post_modified_gmt}}</td>
@endforeach

标签: laravellaravel-5.8multi-database

解决方案


我相信你的关系方法中缺少一些。你必须手动指定foreign_keyand owner_key,否则 laravel 会自动指定the laravel way

在你的模特帖子中。

public function users()
{
    return $this->belongsTo(Users::class, 'post_author', 'ID');
}

在你的模特岗位1

public function users1()
{
    return $this->belongsTo(Users1::class, 'post_author', 'ID');
}

推荐阅读