Docs

Laravel Controller

Now we need to write the create function inside the controller.

<?php

namespace App\Http\Controllers;

use App\Post;
use App\Http\Requests;
use Illuminate\Http\Request;

class CreatePostController extends Controller
{
  public function create(Request $request)
  {
        $this->validate($request, [
        'name'  => 'required',
        'topic' => 'required',
        ]);
    
      $post = new Post;
      $post->name = $request->input('name');
      $post->topic = $request->input('topic');
      $post->save();
    
      return response()->success(compact('post'));
  }
  
}

🚧

This tutorial assumes you have created the model and appropriate migration for the table posts.

If you're not comfortable with this, make sure to follow the official laravel tutorials before reading this.

Prefer a screencast?


Did this page help you?