Tuesday, April 11, 2017

How to get records of another table which are related to other table's element id and post?

I am trying to show payment table information on payment page.

payments{student_id, course_id, payment_type_id}
students{id, name} 
courses{id, name} 
payment_types{id,name}

For the columns > course_id (in payment table) it is showing the names of courses in payment page.But for the other two columns it is not posting.Where is my mistake.Thank you in Advance!

Here is my Payment.php model

 protected $fillable = [
        'student_id',
        'course_id',
        'payment_types_list_id',
    ];

    protected $filter = [
        'page' => false,
        '_token' => false,
        'student_id' => '=',
        'course_id'  => '=',
        'payment_types_list_id' => '=',
     ];

    public function Payment(){
        return $this->hasOne('App\Payment');
    }

    public function course()
    {
        return $this->belongsTo('App\Course');
    }

    public function students()
    {
        return $this->belongsToMany('App\Student');
    }

    public function PaymentType()
    {
        return $this->belongsTo('App\PaymentType');
    }

HERE IS payments_table public function up()

> {Schema::create('payments', function(Blueprint $table)
>     {
>         $table->increments('id');
>         $table->integer('student_id')->unsigned()->index();
>         $table->foreign('student_id')->references('id')->on('students');
>         $table->integer('course_id')->unsigned()->index();
>         $table->foreign('course_id')->references('id')->on('courses');
>         $table->integer('payment_types_list_id');
>         $table->foreign('payment_types_list_id')->references('id')->on('payment_types_list');
>         $table->timestamps();
>     });
>     }

Here is PaymentController.php

> public function index(Request $request) {
>         $filter = $request->input();
>         $sort = $request->query();
>         $payments = Payment::filter($filter)->sort($sort)->paginate(10);
>         foreach(PaymentType::all() as $payment_type) {
>             $payment_types[$payment_type->id] = $payment_type->name;
>         }
>         foreach(Course::all() as $course) {
>             $courses[$course->id] = $course->name;
>         }
>         foreach(Student::all() as $student) {
>             $students[$student->id] = $student->name;
>         }
>         return view('payments.index', compact('payments', 'filter','courses','students', 'payment_types'));
>     }

And here is Payment/index.php

> @forelse ($payments as $key => $payment)
>             <tr>
>             <td></td>
>             <td></td>
>             <td></td>
>             <td></td> </tr> @endforelse



via Bukhari

Advertisement