Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mankowitz's avatar

bringing it all together (vue, ajax, model, mysql, bootstrap, forms, routes)

I'm new to laravel, so this may sound ungrateful, but is there a convenient way to manage the whole stack from presentation of a form to storing it in a database? The way I understand it now, I have to

  1. Create the database with migrations
  2. Create the model (which remains agnostic to the database structure)
  3. Add a route and create a controller to serve the form as well as process its reply
  4. Create an input form view using traditional html elements and bootstrap classes. If validation is used, each element has to have its own way of handling the server-side validation.

So, my question is this: is there a simpler way to create form elements and automatically wire them to a database. Back when I used straight html and php, this was straightforward. now that there are a half dozen levels of indirection, it gets confusing, but not any easier to write or maintain. What am I missing?

0 likes
4 replies
burlresearch's avatar

You don't really have to use any of the fancy machinery. If all you want is HTML to populate a table (say: names), here is a stand-alone blade file that will do the trick:

<!doctype html>
<body>
@php
    if(!empty(data_get($_REQUEST, 'name'))) \DB::table('names')->insert($_REQUEST);
@endphp

<form>
    <input type="text" name="name" placeholder="enter a name" autofocus>
    <input type="submit" value="submit">
</form>

<ul>
    @foreach( \DB::table('names')->get() as $row )
        <li>{{ $row->name }}</li>
    @endforeach
</ul>
</body>
</html>

All of the fancy stuff is there to serve a variety of purposes. All around modern software architecture, security, and reuse. At some point building websites using "straight html and php" became repetitive and difficult to maintain, so frameworks have evolved to try to make it easier to build tool-sets around. But - there is a learning curve.

The good thing is that you don't have to use any of this stuff, until you realize how much easier life becomes when you start to need it.

I don't know if this helps answer your question, but I for one, love the facility that Laravel brings to building modern sites.

1 like
robrogers3's avatar

use route::model binding

with this say updating a model is

public function update(Model $model)
{
    $this->model->foo = request('foo');
    $this->model->save()
    return response('done');
}

then in the routes file (web.php) do this

Route::post('update'/{model}, 'YourModelController@update');

then in your form the action would be:

 <form action="/update/{{$model->id}}" method="POST">

<input>

1 like
mankowitz's avatar

Rob and Burl -- Thanks for your replies. Each is useful, but I guess what I'm looking for is a single place where I could define

  1. The database fields (including relationships)
  2. The labels for the form
  3. Whatever stylistic or business logic I want to add

Somehow, magically, laravel will create the Model, the display form, the controller, the validation and the code to store data in the db, gracefully displaying errors if the data isn't right.

Does that exist?

robrogers3's avatar

I disagree learn laravel. You will thank yourself!

Sign up to laracasts and you'll be sufficient in a matter of days. really!!!

2 likes

Please or to participate in this conversation.