App \ Profile :: jenis必须返回一个关系实例,但是返回了“ null”。是否使用过“ return”关键字?

时间:2019-11-15 09:18:33

标签: php laravel eloquent

  

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

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

控制器

  <?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

请帮助我

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

尝试一下

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