Laravel 将对象从视图传递到控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40642977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel pass object from view to controller
提问by Andrew
Looking for best practices, given these 3 tables:
寻找最佳实践,给出这 3 个表:
Task (id, title, description) User (id, name) User_task (user_id, task_id)
任务(id, title, description) User (id, name) User_task (user_id, task_id)
My class/Object User has a function addToCompletedTasks(Task task) to add a task to his list of completed task (that adds the task into the table User_task).
我的类/对象用户有一个函数 addToCompletedTasks(Task task) 将任务添加到他的已完成任务列表(将任务添加到表 User_task 中)。
I'm looking for best practice to send my completed task FROM my view TO my controller. Right now I'm sending an ID but I'm wondering if it's possible to send the object so I don't have to instantiate the task in my controller to add it to the completedtasks list.
我正在寻找将我完成的任务从我的视图发送到我的控制器的最佳实践。现在我正在发送一个 ID,但我想知道是否可以发送对象,这样我就不必在控制器中实例化任务以将其添加到已完成的任务列表中。
public function insertCompletedTask(Request $request)
{
$task_id = $request->input('task_id');
$user = \Auth::user();
$task = Task::whereId($task_id)->first();
$update = $user->assignCompletedTask($task);
}
回答by Roadirsh
You have to send the ID via a route
您必须通过路由发送 ID
Route::get('/your-url/{id}', 'YourController@addCompletedTasks');
Then you need to have this function declared in your User model:
然后你需要在你的 User 模型中声明这个函数:
public function tasks(){
return $this->belongsToMany('Task');
}
And then in your controller have this function:
然后在你的控制器中有这个功能:
public function addCompletedTasks($id) {
// if you want to you can also just do
Auth::user()->tasks()->attach($id);
}
If you want to send it via POST in the request and not via the ID in the URL your function would need to be like that:
如果您想通过请求中的 POST 而不是通过 URL 中的 ID 发送它,您的函数需要是这样的:
public function addCompletedTasks(Request $request) {
Auth::user()->tasks()->attach($request->get('id'));
}
In either case you can't send a full php object or you will have to serialize it and deserialize it, easier to just send the ID.
在任何一种情况下,您都无法发送完整的 php 对象,或者您必须对其进行序列化和反序列化,更容易发送 ID。
But in my opinion you should check first if a task is related to the id first. But if you have this control on the database level it's ok.
但在我看来,您应该首先检查任务是否与 id 相关。但是如果你在数据库级别有这个控制就可以了。
回答by Arnold
If you return a view you can send back data to the view like this:
如果你返回一个视图,你可以像这样将数据发送回视图:
return view('view-name',array('variable_view' => $variable));
You will use the key from the array as variable in the view. and the value from the array ($variable) is the variable from the controller. So in your view you would type $variable_view and it would use the $variable, and so you can send the task object to the view.
您将使用数组中的键作为视图中的变量。数组中的值 ($variable) 是来自控制器的变量。因此,在您的视图中,您将键入 $variable_view 并且它将使用 $variable,因此您可以将任务对象发送到视图。
In my opinion I would use as little PHP as possible in the view.
在我看来,我会在视图中尽可能少地使用 PHP。