1

I can't access $prospectus in the function show() but works well in the function store() in laravel version 5.6.27

public function store(Request $request) {

    $course = Course::create([
        'name' => $request['name'],
        'title' => $request['title'],
        'division_id' => $request['division_id'],
    ]);

    $prospectus = Prospectus::create([
        'years' => $request['years'],
        'name' => $course->name,
        'user_id' => null,
        'course_id' => $course->id,
    ]); 

    return view('courses.show', compact('course', 'prospectus'));
}

public function show(Course $course) {
    $prospectus = Prospectus::where('course_id', $course->id)->get();

    //return $prospectus;
    return view('courses.show', compact('course', 'prospectus'));
}

the data is passed when i use return $prospectus; but not in return view('courses.show', compact('course', 'prospectus'));

here are my routes

Route::resource('courses', 'CourseController');

Route::post('courses', 'CourseController@store')->name('courses.store');

Route::get('courses/{course}', 'CourseController@show')->name('courses.show');
6
  • i think your $prospectus is null, try to dd($prospectus); in show method; or return view('courses.show', ['course' => $course, '' => 'prospectus' => $prospectus]); Commented Jul 19, 2018 at 13:58
  • 1
    Do you get an error in your view? What exactly seems to be the problem? Commented Jul 19, 2018 at 14:00
  • $prospectus isn't null, data is returned with return $prospectus Commented Jul 19, 2018 at 14:05
  • i get an error Property [years] does not exist on this collection instance with {{ $prospectus->years }} Commented Jul 19, 2018 at 14:07
  • but the data is passed when i call {{ $prospectus }} Commented Jul 19, 2018 at 14:08

2 Answers 2

1

I supose you want a single Prospectus object, get() will give you a collection of objects.

Use the first() function to get only the first match from the database as a single object.

$prospectus = Prospectus::where('course_id', $course->id)->first();
Sign up to request clarification or add additional context in comments.

Comments

0

Confirm that $prospectus query don't return NULL

Try this:

$prospectus = Prospectus::where('course_id', $course->id)->first();

1 Comment

Worked well! Thank you!

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.