- Forums
- laravel
- Laravel Inertiajs With Vuejs Submit Data With Forms
Use these files to create a form with VueJs in a Laravel Application with Interia or Enersia or some people even calle it inersia. Anyways, you know what I mean right? [5276], Last Updated: Mon Jun 24, 2024
edwin
Sat Dec 30, 2023
0 Comments
315 Visits
This a simple no style form with component, seeder, table, model, controller, route and vue:
Source: /g/xampp8/htdocs/noprem/SELECTED-Version/inertia/inertia-shop-vuejs
Commands used to generate these files:
$ php artisan make:model Post -mf
$ php artisan make:controller PostController --model=Posts --requests
web.php (route)
https://youtu.be/V-FhBu7agiM?si=eY_5KTpS2O1kx18f&t=1588
Route::get('/post/create', [PostController::class, 'create']);
Route::post('/post', [PostController::class, 'store']);
PostContorller:
public function create()
{
//https://youtu.be/V-FhBu7agiM?si=-fPT7oH3UBrj2HqW&t=1513
return Inertia::render('Create');// MUST CAPITALIZED!!!!!!
}
/**
* Store a newly created resource in storage.
*/
public function store(StorePostRequest $request)
{
//https://youtu.be/V-FhBu7agiM?si=ulusEB7A0djM0TJc&t=1514
$data = $request->validated(); // all fields in StorePostRequest get validated
//https://youtu.be/V-FhBu7agiM?si=WrHk1riLunElyK24&t=1553
$post = Post::create($data);
}
Post.php (model)
class Post extends Model
{
use HasFactory;
//https://youtu.be/V-FhBu7agiM?si=zAXg40LozwiREGRH&t=1555
protected $guarded = []; // all fields are fillable
}
StorePostRequest.php
public function rules(): array
{
return [
'title'=>'required|max:200',
'author'=>'required|max:200',
'content'=>'required|max:200',
];
}
PostFactory.php
public function definition(): array
{
//https://youtu.be/V-FhBu7agiM?si=fb6RjCVrVmkQyqld&t=693
return [
'title'=> fake()->text(50),
'author'=>fake()->name(),
'content'=>fake()->text(200)
];
}
***_create_posts_table.php
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('author');
$table->text('content');
$table->timestamps();
});
DatabaseSeeder.php ($ php artisan db:seed)
Post::factory(20)->create();
Tutorial:
V-FhBu7agiM