繁体   English   中英

[PHP][Laravel] 无法使用 Laravel Eloquent ORM 从另一个表中显示/获取列

[英][PHP][Laravel] Cant show/get column from another table using Laravel Eloquent ORM

我正在尝试使用 eloquent laravel 从另一个表中显示或获取列,但似乎我遗漏了某些内容或拼写错误。 我使用的是 Laravel 9,我已经按照所有说明视频和文档进行操作,但它们似乎都不起作用。

错误消息显示:尝试读取 null 上的属性“deskripsi”

迁移:

  • 表格业务:

 public function up() { Schema::create('t_bisnis', function (Blueprint $table) { $table->id(); $table->string('deskripsi', 255); $table->string('pemilik', 255); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('t_bisnis'); }

  • 表组:

 public function up() { Schema::create('t_grup_layanan', function (Blueprint $table) { $table->id(); $table->foreignId('bisnis_id')->nullable()->index('fk_bisnis_to_grup'); $table->string('deskripsi', 255); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('t_grup_layanan'); }

楷模:

  • 公司新闻:

 class Business extends Model { use HasFactory; protected $table = 't_bisnis'; protected $guarded = []; protected $primary_key = 'id'; protected $with = ['group']; protected $fillable = [ 'deskripsi', 'pemilik' ]; public function group(){ return $this->hasOne(GroupService::class); } }

  • 团体:

 class GroupService extends Model { use HasFactory; protected $table = 't_grup_layanan'; protected $guarded = []; protected $primary_key = ''; protected $fillable = [ 'bisnis_id', 'deskripsi' ]; protected $with = []; public function service(){ return $this->hasMany(Service::class); } public function business() { return $this->belongsTo(Business::class); } }

我的刀片:

 @foreach ($list as $item) <tr> <td>{{ $loop->iteration }}</td> <td>{{ $item->id }}</td> <td>{{ $item->business->deskripsi }}</td> <td>{{ $item->deskripsi }}</td> <td>{{ $item->updated_at->diffForHumans() }}</td> <td> {{-- <a href="#" class="badge bg-success"><span data-feather="eye"></span></a> --}} <a href="{{ route('gruplayanan.edit', $item->id) }}" class="badge bg-warning"><span data-feather="edit"></span></a> <form action="{{ route('gruplayanan.delete', $item->id) }}" method="post" class="d-inline"> @method('delete') @csrf <button class="badge bg-danger border-0" onclick="return confirm('Are you sure?')"><span data-feather="x-circle"></span></button> </form> </td> </tr> @endforeach

我的 controller:

 class GroupServiceController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $list = GroupService::all(); return view('dashboard.gruplayanan.main', compact('list')); }

当您在 model 中创建belongsTo关系时,它会自动假定关系键为{函数名称}_id

在您的情况下,它会在t_grup_layanan表中查找business_id字段,因为您的 function 名称是 business。

您可以通过为属于 function 的第二个参数提供外键名称来覆盖此行为。

public function business()
{
    return $this->belongsTo(Business::class, 'bisnis_id');
}

这应该可以解决问题。

如果您在这里向下滚动一点,您可以在官方文档中找到有关此的信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM