Implement Model in Controller and Allow Mass Assignment
Fri May 14 2021 23:20:41 GMT+0000 (Coordinated Universal Time)
Saved by
@slendabilt
/**
* 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'];
}
content_copyCOPY
https://laravel.com/docs/8.x/eloquent#mass-assignment
Comments