Preview:
/**
* Controller Code
*/

use App\Models\Category;

public function store(Request $request)
{
  $this->validate($request, [
  'name' => 'required|unique:categories'
  ]);

// create new instance of model
  $category = new Category();

// create new category only works if 'name' field is $fillable
  Category::create([
  'name' => $request->name
  ]);

  return redirect(route('categories.index'));
}



/**
* Model Code
*/


namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
// lets laravel know that the 'name' field is allowed to be updated
    protected $fillable = ['name'];
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter