I have User Model and under it I have Trainers and under that, I have TrainerExperience & TrainerAchievement.
My User Model looks like this:
public function trainer(){
return $this->hasOne(Trainer::class);
}
My Trainer Model:
public function user(){
return $this->belongsTo(User::class);
}
public function achievements() {
return $this->hasMany(TrainerAchievement::class);
}
public function experience() {
return $this->hasMany(TrainerExperience::class);
}
TrainerExperience Model:
protected $casts = [
'experience' => 'array',
];
public function trainer() {
return $this->belongsTo(Trainer::class);
}
TrainerAchievement Model:
protected $casts = [
'achievement' => 'array'
];
public function trainer() {
return $this->belongsTo(Trainer::class);
}
I am trying to save the trainers data with its experience and achievement, into its table. But it keeps on throwing me the error.
Call to undefined method Illuminate\Database\Eloquent\Relations\HasOne::experience()
My Trainer Controller:
<?php
namespace App\Http\Controllers;
use App\User;
use App\Trainer;
use Illuminate\Http\Request;
class TrainerController extends Controller
{
//
public function edit($user) {
$user = User::findOrFail($user);
return view('trainer.edit',[
'user' => $user
]);
}
public function index($user) {
$user = User::findOrFail($user);
return view('trainer.index',[
'user' => $user
]);
}
public function update($user) {
// dd($data = request());
$data = request()->validate([
'dob' => 'required|date',
'mobile' => 'required|numeric',
'address' => 'required',
'id_proof' => 'required',
'id_registration' => 'required',
'high-school-name' => '',
'graduate-college-name' => '',
'graduate-college-course' => '',
'post-graduate-college-name' => '',
'post-graduate-school-course' => '',
'phd' => '',
'training_experience' => 'required',
'people-trained' => 'required',
'paid-training' => 'required',
'course-material' => 'required',
'training_category.*' => 'required',
'youtube' => '',
'twitter' => '',
'instagram' => '',
'facebook' => '',
'blog' => '',
'website' => '',
'meetup' => '',
'linkedin' => '',
'experiencesDesignation.*' => '',
'experiencesOrganization.*' => '',
'achievements.*' => '',
]);
// dd($data);
auth()->user()->trainer()->create([
'date_of_birth' => $data['dob'],
'mobile_number' => $data['mobile'],
'address' => $data['address'],
'id_proof' => $data['id_proof'],
'id_registration_number' => $data['id_registration'],
'high_school_name' => $data['high-school-name'],
'graduation_college_name' => $data['graduate-college-name'],
'graduation_course_name' => $data['graduate-college-course'],
'post_graduation_college_name' => $data['post-graduate-college-name'],
'post_graduation_course_name' => $data['post-graduate-school-course'],
'phd_field_of_study' => $data['phd'],
'total_duration_of_training' => $data['training_experience'],
'no_of_people_trained' => $data['course-material'],
'paid_training' => $data['paid-training'],
'training_category' => $data['dob'],
'course_material_ready' => $data['course-material'],
'youtube_link' => $data['youtube'],
'twitter_link' => $data['twitter'],
'youtube_link' => $data['youtube'],
'instagram_link' => $data['instagram'],
'facebook_link' => $data['facebook'],
'linkedin_link' => $data['linkedin'],
'blog_link' => $data['blog'],
'website_link' => $data['website'],
'meetup_group_link' => $data['meetup'],
'application_submitted' => '1',
]);
$trainer_experience =array(
'experiencesDesignation' => $data['experiencesDesignation'],
'experiencesOrganization' => $data['experiencesOrganization'],
);
$trainer_achievement = array(
'achievements' => $data['achievements'],
);
auth()->user()->trainer()->experience()->create([
'experience' => $trainer_experience,
]);
auth()->user()->trainer()->achievements()->create([
'achievement' => $trainer_achievement,
]);
return redirect("/home");
}
}
I haven't used hasOne anywhere except User. My experience table is connected to Trainer. What is the reason of such error?
from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/315VCnP
via IFTTT
Aucun commentaire:
Enregistrer un commentaire