1
votes

I have created a view to create new courses 'create.blade.php'. And I am trying to store this data in the DB however I am getting the following error:

BadMethodCallException Method Illuminate\Http\Request::request does not exist.

I am not sure what is causing the error as I have referred to the the request namespace in my controller. See below;

CoursesController.php;

<?php

namespace App\Http\Controllers\Admin;

use Gate;
use App\User;
use App\Course;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;


class CoursesController extends Controller
{
    public function __construct()
    {
        //calling auth middleware to check whether user is logged in, if no logged in user they will be redirected to login page
        $this->middleware('auth');
    }

    public function index()
    {
        if(Gate::denies('manage_courses')){
            return redirect(route('home'));
        }

        $courses = Course::all();
        return view('admin.course.index')->with('course', $courses); //pass data down to view
    }

    public function create()
    {
        if(Gate::denies('create_courses')){
            return redirect(route('home'));
        }

        $courses = Course::all()->pluck('title');
        $instructors = User::all()->pluck('name', 'id'); //defining instructor variable 

        return view('admin.course.create', compact('instructors')); //passing instructor to view
    }

    public function store(Request $request)
    {
        $course = Course::create($request->all()); //request all the data fields to store in DB
        $course->courses()->sync($request->request('courses', [])); //

        if($course->save()){
            $request->session()->flash('success', 'The course ' . $course->title . ' has been created successfully.');
        }else{
            $request->session()->flash('error', 'There was an error creating the course');
        }

        return redirect()->route ('admin.courses.index');
    }

}

Create.blade.php

@extends('layouts.app')

@section('content')
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Create Course</div>
                <div class="card-body">

                        <form method="POST" action="{{ route('admin.courses.store') }}" enctype="multipart/form-data">
                            @csrf

                            <div class="form-group">
                                    <label class="required" for="name">Course Title</label>
                                    <input class="form-control {{ $errors->has('title') ? 'is-invalid' : '' }}" type="text" name="title" id="id" value="{{ old('title', '') }}" required>
                                    @if($errors->has('name'))
                                    <div class="invalid-feedback">
                                        {{ $errors->first('name') }}
                                    </div>
                                @endif
                            </div>

                            <div class="form-group">
                                    @if (Auth::user()->isAdmin())
                                            {!! Form::label('Instructor', 'Instructor', ['class' => 'control-label']) !!}
                                            {!! Form::select('Instructor[]', $instructors, Input::get('Instructor'), ['class' => 'form-control select2', 'multiple' => 'multiple']) !!} 
                                            @if($errors->has('Instructor'))
                                                    {{ $errors->first('Instructor') }}
                                                </p>
                                            @endif
                            </div>


                            <div class="form-group">
                            <button class="btn btn-danger" type="submit">
                                Save
                            </button>
                        </div>
                    </div>
                     @endif
                    </form>
                </div>
            </div>
            @endsection

I am new to laravel so i would appreciate any help. Thanks.

2

2 Answers

3
votes

The error message

BadMethodCallException Method Illuminate\Http\Request::request does not exist

speaks to an attempt to call a method/function named request on the Illuminate\Http\Request class, and that function not existing.

it looks like you are indeed trying to use a request() method here:

$course->courses()->sync($request->request('courses', []));

You most likely want the input() method instead, which would get data posted as 'courses'.

$course->courses()->sync($request->input('courses', []));

as described at https://laravel.com/docs/master/requests#input-trimming-and-normalization

I hope this helps!

0
votes

change

$course->courses()->sync($request->input('courses', []));