我有两个表,汇总和Competency,当我试图将来自Competency的外键数据显示到表汇总时,遇到了这个错误。
以下是我的总结模型:
class Summary extends Model
{
protected $table = "summary";
protected $primaryKey = "id";
protected $fillable = [
'id', 'competency_id', 'price'
];
public function competency_id()
{
return $this->belongsTo(Competency::class);
}
}以下是我的能力模型:
class Competency extends Model
{
protected $table = "competency";
protected $primaryKey = "id";
protected $fillable = [
'id', 'competency_name'
];
public function summary()
{
return $this->hasMany(Summary::class);
}
}我的SummaryController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Summary;
class SummaryController extends Controller
{
public function index()
{
$summaryData = Summary::with('competency')->get();
return view('dashboard', compact('summaryData'));
}
}我的dashboard.blade.php
<tbody>
@foreach ($summaryData as $item)
<tr>
<td>{{$loop->iteration}}</td>
<td>{{$item->competency->competency}}</td>
<td>{{$item->price}}</td>
<td>
<a href="{{url('edits', $item->id)}}" class="btn btn-primary">Edit</a>
<a href="{{route('delete', $item->id)}}" class="btn btn-danger" >Delete</a>
</td>
</tr>
@endforeach
</tbody>我真的很感激你的回答。
发布于 2022-08-02 08:42:47
在您的模型中,您将方法名声明为:
public function competency_id当你急切地呼叫它时,你做到了:
$summaryData = Summary::with('competency')->get();您正在尝试调用competency relationship,而它的名称是competency_id。只需将模型中的方法名更改为competency,就可以了
发布于 2022-08-02 08:57:33
您正在调用关系的错误方法名称。
您的关系方法名为competency_id,您将其称为“能力”,这将引发错误。
您可以将方法名称更改为能力,或者调用如下所示:
$summaryData = Summary::with('competency_id')->get();希望这会有帮助。
发布于 2022-08-02 09:25:26
在总结课上,你刮胡子。
取代
public function competency_id()
{
return $this->belongsTo(Competency::class);
}至
public function competency()
{
return $this->belongsTo(Competency::class);
}https://stackoverflow.com/questions/73204425
复制相似问题