having error with survey system where the created form by admin role can’t be assessed by student role

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!

New contributor

yuuji is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật