php - App\Profile::jenis 必须返回关系实例,但返回了 "null"。是否使用了 "return"关键字?

标签 php laravel eloquent

App\Profile::jenis must return a relationship instance, but "null" was returned. Was the "return" keyword used? (View: C:\xampp\htdocs\user_manage\resources\views\profile\profile.blade.php) (View: C:\xampp\htdocs\user_manage\resources\views\profile\profile.blade.php)

Model Jenis.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\Profile;

class Jenis extends Model
{
public $timestamps = false;
protected $table="tbl_jenis_penyedia";
protected $primaryKey="id_jenis_penyedia";
protected $fillable=['jenis_penyedia'];

public function profile(){
    return $this->belongsTo(Profile::class);
}
}

Model Profile.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

    public function jenis(){
        $this->hasMany(Jenis::class, 'id_jenis_penyedia', 'id_profil_penyedia');
}
}

Controller

  <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Profile;
use App\Jenis;




class ProfileController extends Controller
{
    public function index()
    {
        $profile = Profile::all();
         return view('profile/homeprofile',['profile' => $profile]);
}
}

view

@foreach($profile as $p)
            <tr>
              <td>{{ $no++ }}</td>
              <td>
                {{ $p->jenis->jenis_penyedia }}</td>
</tr>
@endforeach

请帮帮我

最佳答案

你忘了把 return 放在 jenis 方法中。

Profile.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

    public function jenis(){
        return $this->hasMany(Jenis::class, 'id_jenis_penyedia', 'id_profil_penyedia'); // PUT A `return` HERE
    }
}

关于php - App\Profile::jenis 必须返回关系实例,但返回了 "null"。是否使用了 "return"关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58873739/

相关文章:

laravel - Laravel Eloquent禁用自动快速加载

php - 加入三个表并计数

mysql - 获取具有关系的嵌套 json 数据 Laravel Eloquent 模型

javascript - Json 给出 301 永久移动

php - 如何测试受限页面? (PHP)

php - Laravel request()->fingerprint() 函数以及加密用户数据记录的可能性?

php - 无法在 Laravel 中检索多个 mysql 查询值

php - 确定 PHP 文件是否作为 `phar` 存档的一部分运行

php - Laravel - 如何创建多个用户角色并为其分配权限?

javascript - Dropzone JS 只上传一个文件