-1

I make many database applications, in my database have the same table name and the same file. how can i display all the data

this is my model

this ins my model users

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

this ins my model 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');
    }
}

this ins my model posts

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

this ins my model posts1

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

this is get data

@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

IGP
  • 14,160
  • 4
  • 26
  • 43
iqbal
  • 1
  • 1
    where are you trying to get the property?? you missed that part completely..add that too in your question.. – zahid hasan emon Dec 27 '20 at 02:53
  • [Please edit your post with minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), your current code snippet is too big to analyze, and it seems that it does not contain instructions which could lead to your error. – validname Dec 27 '20 at 16:08

1 Answers1

0

i believe the some missing from your relation method. you have to specified the foreign_key and owner_key manually, or laravel will do that automatically the laravel way.

in your model post.

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

in your model post1

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