Dalia Najeh
Dalia Najeh

Reputation: 23

How to fix route issue in Laravel

I wanna make online quizzes with Laravel. Two tables courses and questions connected to each other, the user can select from index page course name to go to quiz for that course, after submit it should go to questions.show, the id for the selected course pass also there. But when I try to go to index page the error occurred as following: ""Undefined variable: course (View: C:\xampp\htdocs\bilsan\resources\views\questions\index.blade.php)""

here is Questions index.blade:

     <form method="post" action="{{route('queations.show',$course->id)}}">
        @csrf  
        <select name="courses_id" id="courses_id" class="form-control mt-3" style="max-width:250px; margin:  auto;">
        <option>Select Courses</option>
            @foreach($courses as $course)
              <option value="{{$course->id}}">{{$course->courses_name}}</option>
            @endforeach

        </select>
  <div  class="mt-5 list-group" >
     <input type="submit" value="submit" class="btn btn-primary"  style="max-width:150px; margin: auto;">
  </div>

</form> 

In questions controller :

      public function index()
{   
    $courses =Course::all();
    return view('questions.index',compact('courses'));
}

I use Laravel 7 can any body help!!

Upvotes: 0

Views: 62

Answers (3)

Stormhammer
Stormhammer

Reputation: 497

In your <form> you're trying to set a course->id for the route action, but course does not get defined until the foreach loop. Try to post it to a more general route (where it doesn't need a course->id) or make sure the view gets the course from the controller.


EDIT: My suggestions on your code

Add in your QuestionsController:

public function store() {
    $course = Course::findOrFail(request()->course_id);
    return view('questions.show', compact('course'));
}

Edit your form the questions.index view. I modified the action of the form, as I do not know what your routefile looks like. I assumed your controller is called QuestionsController. I also modified the name of the select to be course_id instead of courses_id.

<form method="post" action="{{action('QuestionsController@store')}}">
    @csrf  
    <select name="course_id" id="course_id" class="form-control mt-3" style="max-width:250px; margin:  auto;">
    <option>Select Courses</option>
        @foreach($courses as $course)
            <option value="{{$course->id}}">{{$course->courses_name}}</option>
        @endforeach
    </select>
    <div  class="mt-5 list-group" >
        <input type="submit" value="submit" class="btn btn-primary"  style="max-width:150px; margin: auto;">
    </div>
</form> 

Upvotes: 2

Ujwal Kadariya
Ujwal Kadariya

Reputation: 145

The form should go inside the foreach loop like this

@foreach($courses as $course)
<form method="post" action="{{route('questions.show',$course->id)}}">
    @csrf  
    <select name="courses_id" id="courses_id" class="form-control mt-3" style="max-width:250px; margin:  auto;">
    <option>Select Courses</option>
          <option value="{{$course->id}}">{{$course->courses_name}}</option>

    </select>
    <div  class="mt-5 list-group" >
        <input type="submit" value="submit" class="btn btn-primary"  style="max-width:150px; margin: auto;">
    </div>
</form> 
@endforeach

In your question, blade doesnot know what $course is in your form route because the data that comes from the controller is $courses and the form also should be inside the loop so that $course variable is also defined in form action.

Upvotes: 0

prazeev
prazeev

Reputation: 93

Make your form action to route('question.store', $cource->id). The form need to submitted to store method followed by resource routing in web.php

<form method="post" action="{{route('queations.show',$course->id)}}">
    @csrf  
    <select name="courses_id" id="courses_id" class="form-control mt-3" style="max-width:250px; margin:  auto;">
    <option>Select Courses</option>
        @foreach($courses as $course)
          <option value="{{$course->id}}">{{$course->courses_name}}</option>
        @endforeach

    </select>
    <div  class="mt-5 list-group" >
        <input type="submit" value="submit" class="btn btn-primary"  style="max-width:150px; margin: auto;">
    </div>
</form> 

Upvotes: -1

Related Questions