Support the ongoing development of Laravel.io →
posted 8 years ago
Eloquent
Last updated 2 years ago.
0

I think so:

Add to your Student model:

public function invoices()
{
        return $this->hasMany('App\Invoice');
}

and:

public function results()
{
        return $this->hasMany('App\Result');
}

Inverse relations are: belongsTo

0

Yes @erdely I've done that already, I am guessing I am messing up on the tables somehow. Could you also tell me what does the 3 tables have it?

My current thing.

    public function Invoices()
    {
        return $this->hasMany('App\Invoices');
    }

    public function results()
    {
        return $this->hasMany('App\Result');
    }

And the table definitions

//The Invoices table
  public function up()
    {
        Schema::create('invoices', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('student_id')->unsigned()->nullable()->index();
            $table->string('name');
            $table->string('student_roll');
            $table->string('fees_paid');
            $table->string('fees_remaining');
            $table->string('fees_total');


            $table->timestamps();
        });
    }
//The Students table
  public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('roll');
            $table->string('address');
            $table->string('school');
            $table->string('city');
            $table->string('class');
            $table->string('email')->unique()->nullable();
            $table->string('phone');
            $table->timestamps();
        });
    }
//The results table
 public function up()
    {
        Schema::create('results', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('student_id')->unsigned()->nullable()->index();
            $table->string('name');
            $table->string('Physics');
            $table->string('Maths');
            $table->string('Chemistry');
            $table->string('Biology');
            $table->timestamps();
        });
    }

I want to have something like when I click "Student's roll number" I gets me back the related Invoices and related Results (can be many)

0
Solution

You have to define relations also in your migration: https://laravel.com/docs/5.3/migrations#foreign-key-constraints

Keep convenient table names and model names...

If you want to connect 2 tables in with third table then use pivot relationship or try: hasManyThrough

0

erdely said:

You have to define relations also in your migration: https://laravel.com/docs/5.3/migrations#foreign-key-constraints

Keep convenient table names and model names...

If you want to connect 2 tables in with third table then use pivot relationship or try: hasManyThrough

I showed you the relations in tables, i want to ask weather or not its correct.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

voidrohan voidrohan Joined 22 Dec 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.