I'm trying to pass data to my view and i want to visualize the name of some records not the id so in my controller I have this:
`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' => 'Класът не е записан!'
]);
}`
I'm passing it in my view like this (part of the table):
`@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`
So this thing: <td>{{ $rule->subject->name}}</td>
works perfectly and I see the name of the subject, not the id, but with the other two: <td>{{ $rule->user->full_name }}</td>
and <td>{{ $rule->clas->name}}</td>
I have this error msg:
Invalid argument supplied for foreach() (View: C:\xampp\htdocs\school_systemesources\views\adminules\list.blade.php)
I'm posting the connection of the tables (models), because I think that may have smthg in common:
User class:
`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;
}
}`
Clas 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);
}`
Subject 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);
}`
And the table that connecting them:
`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);
}
}`
I would be really thankfull if someone can help, i'm trying to fix this for days.. :)
the first problem I can see is:
$rule->clas->name
there is no clas method on ClassSubject. It should be:
$rule->classes->name
or you can rename the method, as it is not conventional to have a plural name for a "belongsTo" relationship method