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.

1
  • what is your school_infos table structure? Commented Jan 11, 2020 at 17:29

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

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.