Laravel Eloquent与MVC原则

huangapple go评论58阅读模式
英文:

Laravel Eloquent with MVC principles

问题

在控制器中,我看到了许多示例中使用以下方法来保存数据:

$emp = new Employee();
$emp->emp_name = $request->emp_name;
$emp->emp_no = $request->emp_no;
$emp->email = $request->email;
$emp->save();

我的疑问是,根据MVC原则,控制器用于连接模型和视图。这是否是正确的做法,或者应该在模型中执行此部分?

我正在寻找一些建议,以了解最佳实践。

英文:

I am quite new to laravel. Using Laravel 8. When saving data I have seen below method in many examples.

In the controller

$emp = new Employee();
$emp->emp_name = $request->emp_name;
$emp->emp_no = $request->emp_no;
$emp->email = $request->email;
$emp->save();

My doubt is according to MVC principles controller is used to connect model and the view. Is this is a correct practice or have to do this part in the model ?

I am looking for some guidance to learn the best practice.

答案1

得分: 1

你也可以尝试以下方式:

模型

class Employee extends Model
{
    protected $fillable = ['emp_name', 'emp_no', 'email'];
}
$emp = Employee::create($request->all()); // 通常我避免这种方式
$emp = Employee::create($request->only(['emp_name', 'emp_no', 'email']));
英文:

You can try following way as well:

Model

class Employee extends Model
{
    protected $fillable = ['emp_name', 'emp_no', 'email'];
}

$emp = Employee::create($request->all()); // generally i avoid this way
$emp = Employee::create($request->only(['emp_name', 'emp_no', 'email']));

答案2

得分: 0

在MVC架构中,模型层负责管理数据,包括验证、存储和检索数据等任务。控制器层处理用户的输入并控制应用程序的流程,而视图层负责向用户呈现UI。

在您提供的代码中,控制器创建了一个Employee模型的新实例,设置其属性,然后调用save方法将数据持久化到数据库。这是Laravel中的一种常见做法,也是完全可接受的。

然而,为了严格遵循MVC原则,理想情况下,数据持久化的逻辑应该放在模型层。您可以在Employee模型中创建一个接受数据数组并处理保存过程的方法。这样,控制器只需将数据传递给模型,而模型会处理其余部分。

在控制器中:

$emp = new Employee();
$emp->saveEmployee($request->all());

在模型中:

class Employee extends Model
{
    protected $fillable = ['emp_name', 'emp_no', 'email'];

    public function saveEmployee($data)
    {
        $this->fill($data);
        $this->save();
    }
}

在这个重构后的代码中,控制器只是将请求数据传递给模型中的saveEmployee方法,模型负责将数据保存到数据库。

这种方法保持了模型对数据的管理责任,而控制器只是充当模型和视图之间的中介。这是一个良好的实践,使您的代码更有组织性,更容易维护,并符合MVC原则。

英文:

In the MVC architecture, the model layer is responsible for managing the data, which includes tasks like validating, storing, and retrieving data. The controller layer handles the user's input and controls the flow of the application, while the view layer is responsible for rendering the UI to the user.

In the code you provided, the controller is creating a new instance of the Employee model, setting its properties, and then calling the save method to persist the data to the database. This is a common practice in Laravel, and it's perfectly acceptable to do so.

However, to strictly adhere to the MVC principles, the logic for persisting the data should ideally be placed in the model layer. You can create a method in the Employee model that accepts an array of data and handles the saving process. This way, the controller only needs to pass the data to the model, and the model takes care of the rest.

In the controller

$emp = new Employee();
$emp->saveEmployee($request->all());

In the model

class Employee extends Model
{
    protected $fillable = ['emp_name', 'emp_no', 'email'];

    public function saveEmployee($data)
    {
        $this->fill($data);
        $this->save();
    }
}

In this refactored code, the controller is simply passing the request data to the saveEmployee method in the model, and the model takes care of saving the data to the database.

This approach keeps the model responsible for managing the data, and the controller simply acts as a mediator between the model and the view. It's a good practice to follow as it makes your code more organized, easier to maintain, and adheres to the MVC principles.

huangapple
  • 本文由 发表于 2023年4月4日 18:14:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75928157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定