gpt4 book ai didi

php - 显示的不是id,而是与外键连接的表中某个数据的名称。 - 拉维尔 5.2

转载 作者:行者123 更新时间:2023-11-30 22:25:03 26 4
gpt4 key购买 nike

我正在尝试将数据传递到我的 View ,我想可视化一些记录的名称而不是 id,所以在我的 Controller 中我有这个:

`public function getIndex() {

return view('admin.rules.list', [
'rules' => ClassSubject::get(),
'classes'=> Clas::lists('name', 'id'),
'subjects' => Subject::lists('name', 'id'),
'teachers'=>User::where('type','=','teacher')->get()->lists('full_name','id')
]);


}

public function postIndex(Request $request) {
$rule = ClassSubject::create([
'class_id'=> $request->get('class_id'),
'subject_id'=> $request->get('subject_id'),
'teacher_id'=> $request->get('teacher_id'),
]);

if($rule->id) {
return redirect()->back()->with('message', [
'type'=> 'success',
'message' => 'Успешно записан нов клас!'

]);
}

return redirect()->back()->with('message', [
'type'=> 'danger',
'message' => 'Класът не е записан!'
]);
}`

我在我的 View 中像这样传递它(表格的一部分):

`@foreach($rules as $rule)
<tr>
<td>{{ $rule->user->full_name }}</td>
<td>{{ $rule->subject->name}}</td>
<td>{{ $rule->clas->name}}</td>
<td>{{ $rule->created_at->format('d.m.Y, H:i') }}</td>
<td>{{ $rule->updated_at->format('d.m.Y, H:i') }}</td>
<td>
<a href="{{ url('admin/rule/edit/'.$rule->id) }}" class="btn btn-sm btn-success">Редактирай</a>
<a href="{{ url('admin/rule/delete/'.$rule->id) }}" class="btn btn-sm btn-danger">Изтрий</a>
</td>
</tr>
@endforeach`

所以这个东西:<td>{{ $rule->subject->name}}</td>工作完美,我看到主题的名称,而不是 id,但与其他两个:<td>{{ $rule->user->full_name }}</td><td>{{ $rule->clas->name}}</td>

我有这个错误消息:

为 foreach() 提供的参数无效(查看:C:\xampp\htdocs\school_system\resources\views\admin\rules\list.blade.php)

我发布了表(模型)的连接,因为我认为这可能有共同点:

用户类:

`public function setPasswordAttribute($password)
{
$this->attributes['password'] = bcrypt($password);
}

/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];

public function classes() {
return $this->belongsTo(Clas::class);
}

public function subjects() {
return $this->hasMany(Subject::class);
}

public function classSubject() {
return $this->hasMany(ClassSubject::class);
}

public function studentMark() {
return $this->hasMany(StudentMark::class);
}

public function getFullNameAttribute()
{
return $this->first_name . ' ' . $this->last_name;
}

}`

类(class):

`protected $fillable = [
'name',
'profile_id'

];

protected $table = 'classes';

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

public function user() {
return $this->hasMany(User::class);
}

public function classSubject() {
return $this->hasMany(ClassSubject::class);
}

public function student() {
return $this->hasMany(Student::class);
}

public function studentMark() {
return $this->hasMany(StudentMark::class);
}`

学科类:

`protected $fillable = [
'name'
];

public function classSubject() {
return $this->hasMany(ClassSubject::class);
}

public function studentMark() {
return $this->hasMany(StudentMark::class);
}

public function user() {
return $this->belongsTo(User::class);
}

public function teacher() {
return $this->hasMany(Teacher::class);
}`

以及连接它们的表:

`class ClassSubject extends Model
{
protected $fillable = [
'class_id',
'subject_id',
'teacher_id',
'method_of_study_id',

];

public function subject()
{
return $this->belongsTo(Subject::class);
}

public function classes()
{
return $this->belongsTo(Clas::class);
}

public function user()
{
return $this->belongsTo(User::class);
}


}`

如果有人能提供帮助,我将非常感激,我已经尝试了好几天来解决这个问题..:)

最佳答案

我看到的第一个问题是:

$rule->clas->name

ClassSubject 上没有 clas 方法。应该是:

$rule->classes->name

或者您可以重命名该方法,因为“belongsTo”关系方法使用复数名称是不常见的

关于php - 显示的不是id,而是与外键连接的表中某个数据的名称。 - 拉维尔 5.2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35508733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com