Santosraj
Santosraj

Reputation: 61

error in Laravel 5.4 resource controller using various methods

I created a NotesController with php artisan command

php artisan make:controller NoteController --resource 

Route

Route::resource('notes','NoteController');

I checked them using route:list command too. All route exists. But when I try to send the form to notes.store it gets redirected to localhost/blog/notes/ and shows

Index of localhost/blog/public/notes/

[ICO]   Name    Last modified   Size    Description
[PARENTDIR] Parent Directory        -    
Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.9 Server at localhost Port 80

Create form is

{!! Form::open(['method' => 'POST', 'route' => 'notes.store', 'class' => 'form-horizontal']) !!}
    {!! Form::hidden('user_id', Auth::user()->id) !!}

    {!! Form::label('level', 'Level') !!}
    {!! Form::select('level', $level,null, ['class' => 'form-control', 'required' => 'required']) !!}

      {!!Form::label('faculty','Faculty:')!!}
      {!!Form::text('faculty',null, array('class'=> 'form-control'))!!}

      {!! Form::label('notecatagory', 'Note Catagory') !!}
      {!! Form::select('notecatagory', $notecatagory,null, ['class' => 'form-control', 'required' => 'required']) !!}

      {!!Form::label('title','Title:')!!}
      {!!Form::textarea('title',null, array('class'=> 'form-control'))!!}


      <div class="btn-group pull-right">
          {!! Form::submit("Create Post", ['class' => 'btn btn-block btn-success', 'style'=>'margin-top : 20px;margin-bottom: 20px;']) !!}
      </div>
  {!! Form::close() !!}

I manually entered and checked data notes.show and notes.update works properly but I have problem with notes.index and notes.store I have other posts controller which works fine. I tried editing posts controller but problem is still the same

Note Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Relations\Relation;
use Session;
use App\Note;
use 
App\User;

class NoteController extends Controller
{
    public function __construct(){
      $this->middleware('auth');

    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('notes.index');
    }


public function store(Request $request)
{
   $this->validate($request, array(
     'user_id'=> 'required |number',
     'level'=>'required',
     'faculty'=>'required',
     'notecatagory'=>'required',
     'title'=> 'required'
   ));

   $note = new Note;
   $note->user_id = $request->user_id;
   $note->level = $request->level;
   $note->faculty = $request->faculty;
   $note->notecatagory = $request->notecatagory;
   $note->title = $request->title;

   $note->save();

   Session::flash('success','Note successfully created');

   return redirect()->route('notes.show', $note->id);
}

Upvotes: 1

Views: 933

Answers (1)

Santosraj
Santosraj

Reputation: 61

I don't know what is wrong in laravel. After trying all day, I made some changes in route and it started working. I changed the route from

Route::resource('notes','NoteController');

to

Route::resource('note','NoteController');

and changed folder name from notes to note and all other routes to note.index note.store . Without any other changes the same files started working. I still don't know what happened. If you know please let me know

Upvotes: 1

Related Questions