0

I want to store the "area" in the $area variable from $school_info array. Using $area I want more 3 rows stored into 3 different arrays like: $school_info1, $school_info2, $school_info3.

public function schooldetailviewid($id)
{
    $school_id = $id;
    $school_info = DB::table('school_infos')->where('school_id', '=', $school_id)->get();
    $area = $school_info->area;

    // recomendation start
    $school_info1 = DB::table('school_infos')->where('area', '=', $area)->get();
    $school_info2 = DB::table('school_infos')->where('area', '=', $area)->get();
    $school_info3 = DB::table('school_infos')->where('area', '=', $area)->get();
    // recomendation end

    return view('schooldetail', compact('school_info', 'school_info1', 'school_info2', 'school_info3'));
}

Problem: Storing of elements is not working and also the 3 more rows are not fetched! I am trying to make a simple recommendation system.

Error

Exception Property [area] does not exist on this collection instance.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Ariful Islam
  • 15
  • 1
  • 4

1 Answers1

1

The .get() returns an array of objects to get only one object use .first():

$school_info = DB::table('school_infos')->where('school_id', '=', $school_id)->first();
$area = $school_info->area;

Otherwise, you need to access the first element of the array:

$school_info = DB::table('school_infos')->where('school_id', '=', $school_id)->get();
$area = $school_info[0]->area;
MEDZ
  • 2,227
  • 2
  • 14
  • 18