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

fsdolphin's avatar

Understanding the whole process when adding items to a database table

Hi,

I have been following Jeffrey's tutorials and I'm having a hard time understanding the whole process when saving data to a database table. I know part of my misunderstanding is my poor understanding on web apps in general.

Here is the code I created following the Laravel 5 Fundamentals series, everything works I just don't fully understand the whole system. Part of my confusion I believe is because Jeffrey uses the same name for the database table, variables, URLs etc. (articles or orders in my case), and I'm not saying that's wrong by any means in fact that's probably a good naming convention but when you don't fully understand something seeing the same name confuses you since you don't know what it's referring to.

Routes

// url to show all orders and method to be called  
Route::get('orders', 'OrdersController@index');  
// url to the form and method to be called  
Route::get('orders/create', 'OrdersController@create');   
// I don't fully understand this, why the same url as the Route::get request?  
// What relation does 'orders' have with 'orders' in the form, do they need to match?  
// Does this means that the store method will be called everytime someone visits  
// the 'orders' url?  
Route::post('orders', 'OrdersController@store');

Controller

class OrdersController extends Controller {
public function index()
{
    $orders = Order::all(); // fatch all data from table orders

    return view('orders.index', compact('orders'));// pass variable orders to index view
}

public function create()
{

    return view('orders.create');// return the create view
}

public function store()
{
    $input = Request::all();// get all content from the form fields, correct?
    
    // I'm not sure what this does, is this a method inherited from the
    // Model class or this is the create method shown above???
    Order::create($input); 
    return redirect('orders');// not sure what this does???
    }
}

create view (form)

{!! Form::open(['url' => 'orders']) !!}// post request, can someone explain this in detail? 

    {!! Form::label ('item', 'Item:') !!}
    {!! Form::text('item', null) !!}

    {!! Form::label ('ingredients', 'Ingredients:') !!}
    {!! Form::text('ingredients', null) !!}

    {!! Form::label ('email', 'email:') !!}
    {!! Form::text('email', null) !!}

    {!! Form::submit('Place Order')!!}
{!! Form::close() !!}

index View

// show all items
@foreach ($orders as $order)
<article>
    <h2>{{ $order->item }}</h2>
    <p> {{ $order->ingredients}}</p>
    <p>{{ $order->email}}</p>
    <p>{{ $order->created_at}}</p>
    <p>{{ $order->updated_at}}</p>
</article>
@endforeach

Can someone explain how the whole system works?

I hope someone can explain step by step how the above code works and the relation between each file and may be change 'orders' to a unique name when possible to make it more readable.

Thanks

0 likes
14 replies
JohnRivs's avatar
Level 37

why the same url as the Route::get request?

Well, it's not the same. One is http://domain.com/orders the other one is http://domain.com/orders/create. One is meant to show all the orders and the other one is meant to show a form to create an order. Do they need to be separate? No. You could have the form and all the orders in the same view.

What relation does 'orders' have with 'orders' in the form, do they need to match?

No, your URL does not have to match what you have in a form. The 'orders' you see as a parameter in Route::get is meant to define how the URL should look like, that's it.

Does this means that the store method will be called everytime someone visits the 'orders' url?

No, the store method will only be triggered when someone makes a POST request to http://domain.com/orders. Who is going to do that? For example, the form (as long as you set the method to POST, read below).

Request::all(); // get all content from the form fields, correct?

Yes.

I'm not sure what this does, is this a method inherited from the Model class or this is the create method shown above???

That method is from the Model class. Now, if you couldn't see that ::create is not the method declared on the controller, I suggest you watch the series on OOP fundamentals.

redirect('orders');// not sure what this does???

This redirects the user back to http://domain.com/orders. The argument you see right there, is the URL where you want to send the user to. In your case, since you declared Route::get('orders', 'OrdersController@index');, the user will be redirected to the page were all the orders are shown. If you redirect the user to an URL that you haven't defined, like redirect('asdf') which would send the user to http://domain.com/asdf, Laravel will throw an exception.

post request, can someone explain this in detail?

POST is an HTTP verb or method. For example, when you type http://google.com in your address bar and hit enter, you are sending a GET request. Whenever you say Route::get() it means 'only respond to someone hitting this url, if the user made a GET request'. So, if you had a form with method="POST" (by default, the Form facade will create a POST form) and you submit the form to a route that only responds to GET, Laravel would throw an exception telling you there's not a route defined for that (or something similar).

index View

In that snippet, $orders equals Order::all(). Here's why:

$orders = Order::all();
return view('orders.index', compact('orders'));

You passed a variable to the view. Inside that view, $orders equals whatever you passed in as a second parameter to the view() helper function.

@foreach ($orders as $order)
<article>
    <h2>{{ $order->item }}</h2>
    <p> {{ $order->ingredients}}</p>
    <p>{{ $order->email}}</p>
    <p>{{ $order->created_at}}</p>
    <p>{{ $order->updated_at}}</p>
</article>
@endforeach

Here, you are going over each order (because $orders is a collection with many order records) and call a property on it.

When you fetch a record from the table, Laravel (Eloquent specifically) will make the value for each column available as properties. So $order->email will return the value in the email column for that record.

fsdolphin's avatar

@JohnRivs

Thanks a lot for the good explanation, I will go through and identify what I still don't understand and post if I missed something.

On my question why the same url as the Route::get request? I was referring to the the first and third (last) Route request.

Route::get('orders', 'OrdersController@index');  
...
Route::post('orders', 'OrdersController@store');

I know these may seem like silly questions but I need to ask otherwise I will not understand the full concept. Thanks a lot for your help.

bobbybouwmann's avatar

It is best practice to you use that convention for naming the routes if you use RESTful routing. You don't have to follow this convention and you can always rename it to whatever you want ;)

JohnRivs's avatar

@fsdolphin oh my bad.

They don't have to match. As @bobbybouwmann mentions, it's a convention.

They respond to different HTTP methods. The first, will respond if you visit the the URL from a browser for example. The second, as I mentioned in my first post, will respond to a POST request to .com/orders, for example the one your form does.

No two controller methods are being triggered at the same time.

fsdolphin's avatar

@bobbybouwmann Make sense, I also want to follow good RESTful practices but I'm referring more to variables such as...

From OrdersController/ index method

$orders = Order::all(); // why not something else?

Thanks

JohnRivs's avatar

@fsdolphin why naming $orders to the variable holding all the Orders? You can name that whatever you want.

pmall's avatar

$orders is a very good name for a list of orders :)

fsdolphin's avatar

@pmall @JohnRivs

I know $orders is a good name but I'm just pointing out where some of my confusion comes from. I guess it could be called something like, $allOrders. Again I'm not criticizing Jeff, I know he knows what he is doing I'm just confused in general. Even though I have read about GET, POST etc. and RESTFul putting them in practice for the first time is confusing.

Thanks a lot for all of your help, I really love the community, always willing to help.

JohnRivs's avatar

@fsdolphin pure personal preference :) If $allOrders makes more sense to you, totally go for it.

fsdolphin's avatar

@JohnRivs

Got it, thanks a lot.

I know these type of questions may seem overwhelming and I hope you guys don't mind answering.

Quick question. Just to identify my weakness, would you say that a person with previous experience in PHP wouldn't be as confused as I am or this is the learning curve everyone will go through when Learning a new framework (on this case Laravel)?

pmall's avatar

If you are familiar with programming languages, php should not be a problem. The problem when learning a framework is to grasp the concepts. Laravel concepts are pretty classic/common among the frameworks.

The good point is, when you'll understand laravel concepts it will be easy for you to learn any other new framework :)

fsdolphin's avatar

@pmall

I don't think I have a problem with PHP since I'm familiar with other programming languages such as C++, Objective-C Etc., I'm just new to HTTP requests and web apps in general + Laravel concepts.

Thanks

Please or to participate in this conversation.