Here a want to add Appointments available for the coach by using two tables…one for date_days the second for trainer and I make a 1-m relation between two tables.
Dateday Table...
class Dateday extends Model { public function trainer() { return $this->belongsTo(Trainer::class, 'trainer_id', 'id'); } }
Trainer Table...
class Trainer extends Model { public function datedays() { return $this->hasMany(Dateday::class); } }
Trainer index ...
Show this way you give me an error, And I want To add a day and date and start_date/end_date to the index page for trainer Who can do this??
@foreach ($trainers as $trainer) <tr> <td>{{ $trainer->id }}</td> <td>{{ $trainer->firstname }}</a></td> <td>{{ $trainer->lastname }}</a></td> <td>{{ $trainer->phone }}</a></td> <-- Show this way you give me ero --> <td>{{ $trainer->dateday->day}}</a></td> <td>{{ $trainer->dateday->date}}</a></td> </tr> @endforeach
Advertisement
Answer
In your Trainer
class, you have defined a function datedays, but in your template you are using $trainer->dateday
without the s.
Regarding your edit
$trainer->datedays
is a Collection of all the datedays associated with the trainer (because of the ->hasMany
), so you cannot get the property of an individual item. If there should only be one date, the relation should be changed (->hasOne
), or if there should be more dates possible, you should think about how you want to show that. If you (for example) only want to show the first date you could use {{ $trainer->datedays->first()->day }}
or you could loop over all dates using @foreach ($trainer->datedays as $dateday)
and use {{ $dateday->day }}
in the loop.