0

can someone please tell me, why i am not able to select section from sections table using left join. i want list of teachers in a table, i am able to access all data from teachers table, but i am not able to see sections using left join. Teacher Table have section_id column, which should access data from sections table on section column.

right now it is giving error when i try to fetch data in view using {{$teacher->section}} Below is my code.

public function listteachers(Request $request)
{
  $teachers = DB::table('teachers')
  ->select('teachers.*')
  ->leftjoin('sections', 'teachers.section_id', '=', 'sections.id')
  ->orderBy('pass_exp', 'ASC')
  ->get();
return view('teachers.list',compact('teachers'));
}
5
  • ->leftJoin() instead of ->leftjoin()? Commented May 20, 2019 at 9:01
  • same error: Undefined property: stdClass::$section Commented May 20, 2019 at 9:02
  • 1
    i guess you are not selecting sections. Just try removing ->select('teachers.*') this line Commented May 20, 2019 at 9:05
  • @vijaykumar Missing required parameters for [Route: teachers.edit] Commented May 20, 2019 at 9:09
  • Now, it is an other issue. edit routes need an ID as a parameter. The error comes from your view I guess. Commented May 20, 2019 at 9:11

2 Answers 2

3

You need to select the columns you want from the sections table in your query.

For example:

$teachers = DB::table('teachers')
    ->select('teachers.*', DB::raw('sections.name as section_name'))
    ->leftJoin('sections', 'teachers.section_id', '=', 'sections.id')
    ->orderBy('pass_exp', 'ASC')
    ->get();
Sign up to request clarification or add additional context in comments.

Comments

1

Change the code to the following

  $teachers = DB::table('teachers')
        ->select('teachers.*','sections.name as section_name')
        ->leftJoin('sections', 'teachers.section_id', '=', 'sections.id')
        ->orderBy('pass_exp', 'ASC')
       ->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.