关系数据未出现在laravel视图中

时间:2019-11-14 08:25:20

标签: php laravel eloquent relation

关系数据未出现在laravel视图中

enter image description here

enter image description here enter image description here enter image description here

>查看

import Foundation

let json = """
{
"nama": [
    "Tigor Franky",
    "Achmad Fauzi",
    "Mario David Lela Muda",
    "Mily Anggreini Hutasuhut",
    "Arif Fahmi ST",
    "Satwika Narindra Dhipa",
    "Mujib Burahman",
    "Aresty Puji Harjanty",
    "Rio Jatmiko",
    "Halasson Sitanggang"
],
"u_id": [
    196,
    113,
    114,
    149,
    115,
    116,
    117,
    118,
    119,
    120
],
"totalakhir": [
    14.15,
    1.74,
    1.25,
    0.75,
    0,
    0,
    0,
    0,
    0,
    0
   ]
 }
""".data(using: .utf8)!

struct DataModel: Codable {
    let nama: [String]
    let uId: [Int]
    let totalakhir: [Double]
    private enum CodingKeys: String, CodingKey {
        case nama
        case uId = "u_id"
        case totalakhir
    }
}

let em1 = try JSONDecoder().decode(DataModel.self, from:json)

print(em1)

coba

>模型Jenis.php

              <td>
                @foreach($p->jenis as $a)
                {{ $a->jenis_penyedia }},
                @endforeach
              </td>

>模型Profile.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Jenis extends Model
{
protected $table="tbl_jenis_penyedia";
protected $fillable=['id_jenis_penyedia','jenis_penyedia'];

public function profile(){
    return $this->belongsTo('App\Profile');
}
}

型号

>控制器

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
protected $table="tbl_profil_penyedia";
protected $fillable=['id_profil_penyedia','id_jenis_penyedia','nama', 'no_ktp', 'file',  'npwp', 'bank', 'no_rek', 'email', 'no_telp', 'keahlian', 'pengalaman', 'alamat', 'pendidikan'];

    public function jenis(){
        return $this->hasMany('App\Jenis');
    }
}

控制器

没有错误 只有数据不显示

enter image description here

1 个答案:

答案 0 :(得分:2)

检查数据库迁移文件。在create_tbl_profil_penyedia_table中定义它: 迁移它。

 $table->unsignedInteger('id_jenis_penyedia')->unsigned();
 $table->foreign('id_jenis_penyedia')->references('id_jenis_penyedia')->on('tbl_jenis_penyedia');

编辑您的Jenis模型

public function jenis(){
     return $this->hasMany('App\Jenis', 'id_jenis_penyedia');
  }


根据您的控制器,您的视图应如下所示:

 @foreach($profile as $p)
     @foreach($p as $a)
      {{ $a->jenis->jenis_penyedia }},
     @endforeach
 @endforeach