after creating a survey form as an administrator of the student survey system, the student role cannot assess the survey form even though it has been assigned to them. when the survey’s name is clicked on the student’s dashboard (which is supposed to direct them to the form to be answered), i get the following error:
enter image description here — this doesn’t only happen to one variable, it happens to most of them such as $survey, $question and many more.
pasted below is the SurveyController.php:
`<?php
namespace AppHttpControllers;
use AppExportsSurveysExport;
use AppModelsSurvey;
use AppModelsUser;
use IlluminateHttpRequest;
use MaatwebsiteExcelFacadesExcel;
class SurveyController extends Controller
{
public function index(Request $request)
{
$search = $request->input('search');
$surveys = Survey::query()
->where('title', 'like', "%{$search}%")
->orWhere('description', 'like', "%{$search}%")
->get();
// Fetch all lecturers from the users table where the role is lecturer
$lecturers = User::where('role', 'lecturer')->get();
return view('admin.surveys.survey-management', compact('surveys', 'lecturers'));
}
public function store(Request $request)
{
$validatedData = $request->validate([
'title' => 'required|string|max:255',
'description' => 'nullable|string',
'lecturer_id' => 'required|exists:users,id',
'faculty' => 'required|string',
'subject_name' => 'required|string',
'lecturer_picture' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
'questions' => 'required|array',
'questions.*.question' => 'required|string',
'questions.*.type' => 'required|string',
'questions.*.options' => 'nullable|string',
]);
$filePath = $request->file('lecturer_picture')->store('lecturer_pictures', 'public');
$survey = new Survey();
$survey->title = $validatedData['title'];
$survey->description = $validatedData['description'];
$survey->lecturer_id = $validatedData['lecturer_id'];
$survey->faculty = $validatedData['faculty'];
$survey->subject_name = $validatedData['subject_name'];
$survey->lecturer_picture = $filePath;
$survey->questions = json_encode($validatedData['questions']); // Encode as JSON
$survey->status = 'pending';
$survey->save();
return redirect()->route('admin.surveys.survey-management')->with('success', 'Survey created successfully.');
}
public function update(Request $request, $id)
{
$validatedData = $request->validate([
'title' => 'required|string|max:255',
'description' => 'nullable|string',
'lecturer_id' => 'required|exists:users,id',
'faculty' => 'required|string',
'subject_name' => 'required|string',
'lecturer_picture' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
'questions' => 'required|array',
'questions.*.question' => 'required|string',
'questions.*.type' => 'required|string',
'questions.*.options' => 'nullable|string',
]);
$survey = Survey::findOrFail($id);
$survey->title = $validatedData['title'];
$survey->description = $validatedData['description'];
$survey->lecturer_id = $validatedData['lecturer_id'];
$survey->faculty = $validatedData['faculty'];
$survey->subject_name = $validatedData['subject_name'];
if ($request->hasFile('lecturer_picture')) {
$filePath = $request->file('lecturer_picture')->store('lecturer_pictures', 'public');
$survey->lecturer_picture = $filePath;
}
$survey->questions = json_encode($validatedData['questions']); // Encode as JSON
$survey->status = $request->input('status', $survey->status);
$survey->save();
return redirect()->route('admin.surveys.survey-management')->with('success', 'Survey updated successfully.');
}
public function destroy($id)
{
$survey = Survey::findOrFail($id);
$survey->delete();
return redirect()->route('admin.surveys.survey-management')->with('success', 'Survey deleted successfully.');
}
public function showReporting()
{
$surveys = Survey::all();
return view('admin.reporting.index', compact('surveys'));
}
public function exportSurvey(Survey $survey)
{
return Excel::download(new SurveysExport($survey), 'survey-' . $survey->id . '.xlsx');
}
}`
pasted below is the StudentController.php:
`
<?php
namespace AppHttpControllers;
use AppModelsSurvey;
use AppModelsResponse;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesStorage;
use IlluminateSupportFacadesMail;
use AppMailSurveyCompleted;
class StudentController extends Controller
{
public function dashboard()
{
$user = Auth::user();
$surveys = Survey::all(); // or filter the surveys as per your logic
$pendingSurveys = $surveys->filter(function ($survey) use ($user) {
return !$survey->responses->contains('user_id', $user->id);
});
$surveysToAnswer = $pendingSurveys->count();
$reminders = $pendingSurveys; // Now, $reminders is a collection of pending surveys
$eventRecommendations = []; // Placeholder for event recommendations logic
return view('dashboard.student', compact('surveys', 'surveysToAnswer', 'reminders', 'eventRecommendations'));
}
public function showSurvey(Survey $survey)
{
return view('student.survey', compact('survey'));
}
public function submitSurvey(Request $request, Survey $survey)
{
$validatedData = $request->validate([
'answers' => 'required|array',
'answers.*' => 'required|string',
]);
$response = new Response();
$response->survey_id = $survey->id;
$response->user_id = Auth::id();
$response->answers = $validatedData['answers'];
$response->save();
// Send email to the student
Mail::to(Auth::user()->email)->send(new SurveyCompleted($survey, $response));
return redirect()->route('dashboard.student')->with('success', 'Survey submitted successfully!');
}
public function showProfile()
{
$user = Auth::user();
return view('student.profile', compact('user'));
}
public function updateProfile(Request $request)
{
$user = Auth::user();
$request->validate([
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users,email,' . $user->id,
'password' => 'nullable|string|min:8|confirmed',
'picture' => 'nullable|image|mimes:jpeg,png,jpg,gif|max:2048',
]);
$data = [
'first_name' => $request->input('first_name'),
'last_name' => $request->input('last_name'),
'email' => $request->input('email'),
];
if ($request->filled('password')) {
$data['password'] = bcrypt($request->input('password'));
}
if ($request->hasFile('picture')) {
if ($user->picture) {
Storage::delete($user->picture);
}
$path = $request->file('picture')->store('profile_pictures');
$data['picture'] = $path;
}
$user->update($data);
return redirect()->route('student.profile')->with('success', 'Profile updated successfully.');
}
public function showMockupSurvey()
{
return view('mockup-survey');
}
public function submitMockupSurvey(Request $request)
{
$validatedData = $request->validate([
'answers' => 'required|array',
'answers.*' => 'required|string',
]);
// Store the response (if required)
$response = new Response();
$response->user_id = Auth::id();
$response->survey_id = 0; // Assuming this is a mockup and doesn't correspond to a real survey in DB
$response->answers = json_encode($validatedData['answers']);
$response->save();
// Send email to the student
Mail::to(Auth::user()->email)->send(new SurveyCompleted($validatedData['answers']));
return redirect()->route('dashboard.student')->with('success', 'Survey submitted successfully!');
}
}`
any help would be really appreciated. thank you so much, have a good day ahead!
yuuji is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1