【问题标题】:App\Profile::jenis must return a relationship instance, but "null" was returned. Was the "return" keyword used?App\Profile::jenis 必须返回一个关系实例,但返回的是“null”。是否使用了“return”关键字?
【发布时间】:2020-02-24 16:52:02
【问题描述】:

App\Profile::jenis 必须返回一个关系实例,但“null”是 回来。是否使用了“return”关键字? (看法: C:\xampp\htdocs\user_manage\resources\views\profile\profile.blade.php) (看法: C:\xampp\htdocs\user_manage\resources\views\profile\profile.blade.php)

模型 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);
}
}

模型配置文件.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');
}
}

控制器

  <?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]);
}
}

查看

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

请帮帮我

【问题讨论】:

    标签: php laravel eloquent


    【解决方案1】:

    您忘记将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
        }
    }
    

    【讨论】:

      【解决方案2】:

      试试这个

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

      【讨论】:

      • 试图获取非对象的属性(查看:C:\xampp\htdocs\user_manage\resources\views\profile\profile.blade.php)(查看:C:\xampp\htdocs\ user_manage\resources\views\profile\profile.blade.php)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      • 2017-04-22
      • 1970-01-01
      • 2018-05-23
      • 1970-01-01
      相关资源
      最近更新 更多