dpnfjx755573 2018-11-06 12:45
浏览 58
已采纳

几种形式逻辑,Laravel

I'm still learning Laravel, as what I'm trying to accomplish at the moment is the following logic:

-An User submits a form-> The form input is saved into DB, and is 'sent' to another User who can then validate it->I save into the DB if the state is validated or not, and inform the User who sent the form.

I know this is not too hard, but I'm doing it as I learn Laravel, so I'm coming up with a few 'problems'.

First of all, I do have quite a lot of forms, and I did get the Post logic working. At the moment I'm not working with the Database yet, so I tested the Forms with SESSION, to make sure the data was being saved.

So my first question is: is it best practice in this case to keep all form logic into one Controller? Like the following(this is working with two different forms) (session is there only for testing purposes)

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{

    public function geralSuggest(Request $request)
    {

        $name = $request->input('obs');

        return redirect('sugestoes');
    }

    public function GD(Request $request)
    {

        session_start();

        $name = $request->input('nome');
        $cc = $request->input('cartao');
        $array = array ($name, $cc);
        $_SESSION["testPostSection"] = $array;        

        return redirect('declaracaogd');   
    }
}

Secondly, I have some forms where the user can add inputs. Lets say one input is for the name, and the user has a button that when he clicks he adds another name input, so he can input many names at once. This is done by Javascript on my side. What I'm wondering is how will I save this inputs, since I 'don't know' how many "names" the user will be submitting. I was thinking a for or foreach loop, where I would check with "isset", but I'm not sure if it will work. I will be testing this when I get to it regardless, and I'm sure I'll figure it out, but if you know a good logic for this and could point me in the right path, it would be appreciated.

Lastly, about my general logic, for User validation, from what I read on Laravel documentation, I think Events will be what makes the trigger to 'warn' the second User that he needs to validate a form, is that correct?

I'm sorry if I'm asking "too much at once" or if these are very basic questions. I just wanted to make sure that I'm going in the right direction.

Thanks a ton in advance!

  • 写回答

1条回答 默认 最新

  • dsg56465 2018-11-06 12:57
    关注

    Im not sure if I understand your system 100% correctly. But it seems you are trying to make a human validation program? So 1 users submits data and another users validates this data. In this case I would do this in 2 seperate controllers. Laravels explenation on controllers states: "Controllers can group related request handling logic into a single class." Providing data or validating data are 2 different things. But this isnt a strict rule.

    For the second question. Laravel has an all() method. $input = $request->all(); will give you an array will all request data. After that you could loop over the array and insert it into a db or the storage method you want.

    Edit: another way to handle this if you could have multiple names could be:

    // View
    <input type="text" name="names[]">
    <input type="text" name="names[]">
    
    // Controller
    $request->names; // This is an array of values
    

    Source: https://laracasts.com/discuss/channels/laravel/get-the-values-of-dynamically-generated-inputs

    And events are indeed the way to go if you want to send a message. so you could fire an event as soon as the data is submitted. And one of the listeners would send a email or something like that

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 表达式必须是可修改的左值
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题