In which Controller should I write logic for Models that are in a relationship

business logic in controller or service
separate business logic from controller
where should business logic reside
should an api contain business logic
mvc business logic layer example
model view controller
business logic vs models
should there be logic in the controller

I am making a blog with users who each have posts. Naturally, I have a UserController, PostController, and a User and Post model.

The User model has a one-to-many relationship with the Post model like so

// User Model
public function posts()
        return $this->hasMany('App\Post');

I would like to get all the posts of a specific user by using

// UserController or PostController...
$posts = User::find($id)->posts()->get()

Should I write this logic in my UserController (because it uses the User model) or should I write the logic in my PostController (because it returns posts)

Note: Please forgive me if this question is not formatted properly, this is my first Stack Overflow question, thanx

Actually, you just need to create the relationship as you did, and then you will be able call it wherever you want, just like as you described, with User::find($id)->posts()->get().

So, if you need to return a user's post on a view, for instance, you can do something like that on your UserController:

public function postsView(User $user)
    $posts = $user->posts()->get();
    return view('user.posts', compact('posts'));

Also, if you want the other way around (to know who is the user behind the post), you can create a relationship on your Post model using belongsTo().

// Post Model
public function user()
    return $this->belongsTo('App\User');

Let me know if this helped.

Multivariable Predictive Control: Applications in Industry, The details of this task are specific to the type of process control computer, Proper execution of the controller should be verified as well. Normally model file contains process information in terms of process model, which is used to define process relationship between Special handling and logic is also defined in CCF. A model could be a single object (rather uninteresting), or it could be some structure of objects. There should be a one-to-one correspondence between the model and its parts on the one hand, and the represented world as perceived by the owner of the model on the other hand. Views. A view is a (visual) representation of its model.

You can write the query anywhere in UserController or PostController

$user = User::with('posts')->whereId($id)->first();
echo '<pre>';

You'll get user with all posts

How strict are you about what logic you put in your models vs your , How strict are you about what logic you put in your models vs your controllers is a relationship there that could be handled with relationship modeling using  A1: Business Logic goes to Model part in MVC. Role of Model is to contain data and business logic. Controller on the other hand is responsible to receive user input and decide what to do. A2: A Business Rule is part of Business Logic. They have a has a relationship. Business Logic has Business Rules. Take a look at Wikipedia entry for MVC.

In your Model User put the relationship.

public function posts()
    return $this->hasMany('App\Post','foreign_key','local_key');

In your UserController or PostController get the results by using

$posts = User::find($id);
foreach($posts->posts as $post)
  echo $post->title;

PHP for Absolute Beginners, The database design must support that any one comment can be related to only one blog entry. A logical solution is to create the comment table with a column for entry_id, This kind of relationship is known as a one-to-many relationship have to write a model for inserting comments and update your controller, so it can​  The model should have no knowledge of how the data is to be displayed (e.g., formatting) or the workflow. "He also believes that any call that returns a Json object should happen in the model, not in the controller. The model would return an array to the controller, which would then return this as a Json object." NO. The Model should never

You can query from both controllers. From where you want to query the relationship depends on where and how you want to fetch data from.

For example, if you are showing posts related to user on a user profile page with other bio data etc then you will call the relationship in UserController.

On other hand, let's say you want to show posts related to an author on a search page then it will be better to have the posts queried via relationship in PostController.

In short, it's a choice based on convenience and optimization to be made.

Developing Quality Metadata: Building Innovative Tools and , Occasionally, after some editing, you can save some disk space by writing a a Model-ViewController (MVC) paradigm may help you build a better system. complete description of the data system including the business logic required to Relationships An entity relationship diagram is a plan Object Modeling Your Data 45. Basic Example of a Logic Model. The following example is intended to further portray the nature of inputs, processes, outputs and outcomes. The logic model is for an organization called the Self-Directed Learning Center (SDLC). Logic models for programs are often more detailed.

Handbook of Dynamic System Modeling, dynamic) Sensor Controller Actuator Interaction model for Handler's geometry Based on these rules, we could induce an interaction model for the boiling water Additional association relationships, such as “has a link” and “has a hander,” Write and store logical constraints using Protégé Axiom Language (​PAL) and  The following discussion of logic model weaknesses is intended to help agencies assess their current use of logic models and decide if moving in another direction makes more sense. Ten Reasons to Reconsider the Use of Logic Models 1. Logic models start in the wrong place with means and not ends.

Why put the business logic in the model? What happens when I , ElYusubov's answer mostly nails it, domain logic should go into the model and application logic into the controller. Two clarifications: The term  For the where to add logic I think that it's important to refer to the Single Responsibility Principle. Also, my answer considers that you are working on a medium / large project. If it's a throw-something-on-a-page project, forget this answer and add it all to controllers or models. The short answer is: Where it makes sense to you (with services).

MVC - Database search logic in View or Model, A controller serving the view will need to make the list of results available to the view page. Model The model at least defines the relationship between neatObjects  4. Diverse stakeholder voices are included in creating the logic model 5. The language is clear, detailed, and concise Some logic model formats also depict complex influences outside the program itself. While a basic logic model should include the five basic components of participants, inputs, activities, outputs, and outcomes, similarities end

  • Actually, you just need to create the relationship as you did, and then you can call it whatever you want. Also, if you want the other way around (to know who is the user behind the post), you can create a relationship on your Post model using belongsTo().
  • Thank you so much, Lucas, This has definitely helped a lot, I think I now have a bit more clarity on how to organize my methods
  • @JonathandeKock you are welcome, sir. Glad I could help!