Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sammichael25's avatar

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.

0 likes
7 replies
sammichael25's avatar

Pareent.php

<?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');
    }
}
sammichael25's avatar

Student.php

<?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');

    }
}
sammichael25's avatar

StudentsController.php


public function store(Request $request)
    {
        //
        if(Auth::check()){

    $parent = Pareent::create([
                'pfname'=> $request->input('pfname1'),
                'plname'=> $request->input('plname1'),
                'type'=> $request->input('type1'),
                'address_id'=> $paddress->id,
                'contact_id'=> $pcontact->id
            ]);

    $student = Student::create([
                'fname'=> $request->input('fname'),
                'mname'=> $request->input('mname'),
                'lname'=> $request->input('lname'),
                'sex'=> $request->input('sex'),
                'dob'=> $request->input('dob'),
                'yeargroup'=> $request->input('year_group'),
                'school_id'=>$request->input('school'),
                'address_id'=>$st_address->id,
                'contact_id'=>$st_contact->id,
                'emgccontact_id'=>$emgc->id
            ]);

            $student->pareents()->attach($parent->id);
}
}
tykus's avatar
tykus
Best Answer
Level 104

Your relationship on Student is called Pareent not pareents

hanif-king's avatar

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 
1 like
sammichael25's avatar

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)

student.blade.php

<div class="row">
    <div class="col-md-4">
        <div class="form-group label-floating">
        <label for="student-school-name" class="control-label">School</label>
        <select class="form-control" name="school" id="student-school-name">
            <option selected hidden>Choose School</option>
            @foreach($schools as $school)
                @if($student->school->id === $school->id)
                    <option selected value="{{$student->school->id}}">{{$school->name}}</option>
                @else
                    <option value="{{$school->id}}">{{$school->name}}</option>
                @endif
            @endforeach
        </select>
        </div>
    </div>
</div>

Students Controller edit function

 public function edit(Student $student)
    {
        //
        $student = Student::where('id', $student->id )->first();
        $schools = School::orderBy('name', 'asc')->get();
        $cities = City::orderBy('name', 'asc')->get();
        return view('student',['student'=>$student],['cities'=>$cities],['schools'=>$schools]);
    }

tykus's avatar

Just one array:

return view('student1',['student'=>$student, 'cities'=>$cities, schools'=>$schools]);

It can be easier to read if you use PHP's compact function, which produces the same array:

return view('student1', compact('student', 'cities', 'schools'));
1 like

Please or to participate in this conversation.