0
votes

community!

I was learning laravel framework and got stuck at resource controller concept

I made a resource controller with the help of artisan command i.e php artisan make:controller PostController --resource

Here’s the code: create.blade.php(which is views/posts folder)

@extends('main')
@section('content')
<div class="row">
    <div class="col-mid-8 col-md-offset-2" >
    <h1>Create New Post</h1>
    <hr>
   
    <form action="posts/create" method="GET">
        <div class="form-group">
          <input type="text" class="form-control" 
                  name="title" aria-describedby="emailHelp">
        </div>
        <div class="form-group">
          <input type="text" class="form-control" 
                  name="body" aria-describedby="emailHelp">
        </div>
        <button type="submit" class="btn btn-primary">Submit</button>
      </form>
</div>
</div>
    @endsection

PostController.php

class PostController extends Controller
{
    

    
    public function create()
    {
        return view('posts.create');
    }

web.php (route)

<?php

use Illuminate\Support\Facades\Route;

Route::resource('posts', 'PostController');

please, guide me what wrong I am doing.

2
Add method name in web.php like this, Route::resource('posts', 'PostController@create'); - DevSavata
Or try changing method name from create() to index() in PostController.php - DevSavata
can you paste your url too during create ? - Meeraj Adhikari

2 Answers

0
votes
Route::post('posts/create', 'PostController@create')->name('posts');

And change your .blde form action and method

action="{{route('posts')}}"  method="post"

Also change your controller method

public function create(Request $r){}
0
votes

You should follow design guides on the official laravel website. When you write

Route::resource('posts', 'PostController');

it creates specific routes here documentation.
In your case you want to display that route

GET /posts/create   create  posts.create



<form action="{{route("here route_name for post method")}}" method="POST">