简体   繁体   English

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

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

im trying to show or get column from another table using eloquent laravel, but it seems i have miss something or typo.我正在尝试使用 eloquent laravel 从另一个表中显示或获取列,但似乎我遗漏了某些内容或拼写错误。 Im using Laravel 9, and i've followed all the instructions video and documentation and all of them seems to be not working.我使用的是 Laravel 9,我已经按照所有说明视频和文档进行操作,但它们似乎都不起作用。

Error message says: Attempt to read property "deskripsi" on null错误消息显示:尝试读取 null 上的属性“deskripsi”

Migrations:迁移:

  • table bisnis:表格业务:

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

  • table group:表组:

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

Models:楷模:

  • bisnis:公司新闻:

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

  • group:团体:

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

My blade:我的刀片:

 @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

My controller:我的 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')); }

When you create a belongsTo relation in a model it automatically assumes that the relation key is {the name of the function}_id .当您在 model 中创建belongsTo关系时,它会自动假定关系键为{函数名称}_id

In your case it looks for a business_id field in the t_grup_layanan table, because your function name is business.在您的情况下,它会在t_grup_layanan表中查找business_id字段,因为您的 function 名称是 business。

You can override this behavior by giving a second parametor to the belongs to function which is the foreign key name.您可以通过为属于 function 的第二个参数提供外键名称来覆盖此行为。

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

This should do the trick.这应该可以解决问题。

You can find information about this in the official documentation if you scroll down just a little here如果您在这里向下滚动一点,您可以在官方文档中找到有关此的信息

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

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