BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::pareents()
I am trying to save data to a pivot table named pareents_student (I purposely misspelt the word parents because its a reserved keyword). It is a many-to-many relationship between pareents and students.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Pareent extends Model
{
//
protected $fillable = [
'pfname',
'plname',
'type',
'address_id',
'contact_id'
];
public function Student(){
return $this->belongsToMany('App\Student');
}
public function Address(){
return $this->hasOne('App\Address');
}
public function Contact(){
return $this->hasOne('App\Contact');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
//
protected $fillable = [
'fname',
'mname',
'lname',
'dob',
'sex',
'yeargroup',
'school_id',
'address_id',
'contact_id',
'emgccontact_id'
];
public function Address(){
return $this->belongsTo('App\Address');
}
public function Pareent(){
return $this->belongsToMany('App\Pareent');
}
public function Contact(){
return $this->hasOne('App\Contact');
}
public function Addition(){
return $this->hasOne('App\Addition');
}
public function Emgccontact(){
return $this->hasOne('App\Emgccontact');
}
public function School(){
return $this->belongsTo('App\School');
}
}
try to not Name your relationship exact like what your model is
in your case your model and relationship is same and also you are calling wrong relationship as #tykus said.
try to rename your Parent relationship from
public function Pareent(){
return $this->belongsToMany('App\Pareent');
}
// to
public function pareents(){
return $this->belongsToMany('App\Pareent');
}
// and then try
//also in your parent table for many to many specify your foreign key in your relationship
like
public function Student(){
return $this->belongsToMany('App\Student','parent_id');
}
// where parent_id is the the foreign key of child
Ok so now I want to display a selected school on the edit page but i get
Undefined variable: schools (View: C:\xampp\htdocs\stem\resources\views\student.blade.php)