0

can you please check why this code is returning me empty result in laravel. i am trying to fetch nations name based upon nation id in teachers table.

public function edit(Teacher $teacher)
{

  $teachers= DB::table('teachers','nations')
  ->join('nations', 'teachers.nation_id', '=', 'nations.id')
  ->select('teachers.*')
  ->where('teachers.id',$teacher->id)
  ->first();

return view('teachers.create',['teachers' => $teachers]);
}
2
  • dd($teacher->id) and see if you're getting id or not Commented Apr 29, 2019 at 8:56
  • @Iftikharuddin it returns id Commented Apr 29, 2019 at 9:01

3 Answers 3

1

Check this code

public function edit(Teacher $teacher)
{
  $teachers= DB::table('teachers')
    ->join('nations', 'teachers.nation_id', '=', 'nations.id')
    ->select('teachers.*','nations.name')
    ->where('teachers.id','=',$teacher->id)
    ->first();

  return view('teachers.create',['teachers' => $teachers]);
}
Sign up to request clarification or add additional context in comments.

4 Comments

then there is no rows having the matched id's
yes it returns all id's and if i remove join it returns correct result, but with join null.
check the join variables nation_id and id, check whether at least one row having similar values in those tables
SQL: select teachers.*, nations.name from teachers inner join nations on teachers.nation_id = nations.id where teachers.id = 911 limit 1)
1

Why do you need to use the query builder to perform such a not complex query?

You can achieve this easier if you add a relationship method in your Teacher model.

So in your Teacher class add the following:

public function nation() {
    return $this->belongsTo(Nation::class);
}

Then in your controller you can use it like this:

$teacher->nation;

2 Comments

i have already tried Eloquent query builder it works fine, but i have complex queries ahead, so i will stick with sql query builder as it is fast.
SQL: select teachers.*, nations.name from teachers inner join nations on teachers.nation_id = nations.id where teachers.id = 911 limit 1)
1

In case you allowed in teachers table for nation_id to be empty value or null, you need to use left join to still have all records from teachers table:

public function edit(Teacher $teacher)
{

  $teachers= DB::table('teachers','nations')
  ->leftJoin('nations', 'teachers.nation_id', '=', 'nations.id')
  ->select('teachers.*', 'nations.name')
  ->where('teachers.id',$teacher->id)
  ->first();
  return view('teachers.create',['teachers' => $teachers]);
}

4 Comments

error: SQL: select teachers.*, nations.name from teachers left join nations on teachers.nation_id = nations.id where teachers.id = 915 limit 1
What's title of column for nation name in database schema?
name is; nations.name
You could use ->select(DB::raw('teachers.id, teachers.name, nations.name'))

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.