jeudi 9 février 2017

Join two tables and use controller to show data in a page in Laravel 5.2

Hi I want to join two tables and pass those data in a web page, I have did some using controller but i am getting error "Invalid argument supplied for foreach() (View: F:\QuizeBangla\resources\views\QuizeBangla\chapter.blade.php)"

Here is image of two tables First Table is Table SubCategory Second Tables isTable Subject_Title

Her is the Subcategory Model

use Illuminate\Database\Eloquent\Model;

       class SubCategory extends Model
        {

            protected $table='tbl_subcategory';
            protected $fillable =["tbl_subcategory"];

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

Here Is Subject_Title Model

use Illuminate\Database\Eloquent\Model;

class SubjectTitle extends Model
{
    protected $table = 'subject_title';

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

}

This Is my PagesController

public function chapter($id)
    {
        $subject_title = SubjectTitle::where('postid',$id)->first();
         $chapter = SubCategory::where('sub_id','=','postid')->first();
        return view('chapter')
            ->with('subject_title', $subject_title)
            ->with('chapter', $chapter);
    }

Here Is the Chapter.blade.php

@foreach( $subject_title as $title)
          <h1> </h1>   
       @foreach( $chapter as $cer)
                <h1></h1>
       @endforeach
@endforeach

What I wanted to do,If the posid From Subject_tile Mach's with the sub_id from SubCategory Table than it will print out the all chapters under the each Title ID.Please help me to find out my silly mistake. tell me if there any changes.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kuGSeX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire