0

I have a page where I want to list some countries and states I have in my database, each one has their own controllers. I'd like to know if this is the proper way to do it:

<!DOCTYPE html>
<html>
    <head>  </head>
    <body>
        <?php $states = App\Http\Controllers\StatesController::getStates(); ?>
        @foreach($states as $state)
            <p>{{$state->name}}</p>
        @endforeach

        <?php $countries= App\Http\Controllers\CountriesController::getCountries(); ?>
        @foreach($countries as $country)
            <p>{{$country->name}}</p>
        @endforeach
    </body>
</html>

The controllers are perfoming SQL queries and returning them as arrays, such as:

 public static function getStates() {
        $states= DB::table('states')->get();

        return $states;
    }

Since I'm not using view and not setting up at any routes to do this, is this ok according to the MVC format? If not, how could I make it?

3
  • 3
    If you're not using a view generated by a controller action then IMO it's not really MVC. Any reason you're doing it like this? You can of course do as you please, but it depends to what extent you want to stick to the MVC pattern. Commented May 27, 2022 at 8:50
  • @ADyson I don't know how to get both of these gets using the controller, given each controller will return a different view Commented May 27, 2022 at 8:54
  • 1
    Sounds like they should potentially be functions which a controller action would call to get data and put it into a combined ViewModel object, which would then be returned to a view. So rather than being controller actions themselves, they would be functions in the background logic/data layer of your application. Commented May 27, 2022 at 8:58

2 Answers 2

4

Your approach is not wrong but not correct in the context of an MVC.

The workflow would be Route -> Controller -> View.

web.php

Route::get('/', [App\Http\Controllers\YourController::class, 'index']);

YourController.php

public function index() {
    return view('index', [
       // 'states' => DB::table('states')->get(),
       'states' => \App\Models\States::all(),
       'countries' => \App\Models\Countries::all(),
     ]);
}

index.blade.php

<!DOCTYPE html>
<html>
    <head>  </head>
    <body>
        @foreach($states as $state)
            <p>{{$state->name}}</p>
        @endforeach

        @foreach($countries as $country)
            <p>{{$country->name}}</p>
        @endforeach
    </body>
</html>
Sign up to request clarification or add additional context in comments.

10 Comments

@AkariOozora If I have understood you correctly now :-) In general, you can say that each view has a corresponding controller method. This is the common way.
@AkariOozora Basically yes, without going into more detail. You can view the model in laravel as a repository. Model::all(), Model::find(1), Model:where("id",1)->get()
@AkariOozora No you don't need to. The controller receives the request for that one particular resource (for example /index.html). The controller now takes care of loading all the data from different models. For more complex requests, it makes sense to work with a service and the controller only validates the requests. but you don't need that in this particular case.
@AkariOozora short answer: no ;-)
@AkariOozora Long answer: Not every model needs a controller. As I said, the controller accepts the request for a particular resource and organises the data. Small example why: If you don't want to show your users to your site visitors via the resource user/{id} you don't need a controller userController. But in your dashboard you already want to show the user his data. So for example the DashboardController would use the User Model.
|
-3

In Laravel, the Controller is a central component of the MVC (Model-View-Controller) pattern, responsible for handling user requests, orchestrating logic, and returning responses. To properly use a Controller in Laravel, follow these guidelines

1 Comment

Which are "these guidelines"? If you want to help the OP, please share more details such that others can learn from your answer

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.