dongmuyuan3046 2018-05-18 20:27
浏览 72

如何将对象从视图传递到控制器

I have an object $trial that fills up while in the view. I would like to pass it to route as POST to call a function using it's data. This is to create an event for fullcalendar. I'm pretty sure that I've been sitting on this so long, that I'm way overthinking it.

Wanted to do this with eventRender callback but couldn't figure out how to pass the data into it, tried simple $.post just to get method not allowed or unknown status in the console.

I have some dummy data at the moment.

The goal here is to create event through marked time range.

Controller function to add new record to database if I manage to pass data

public function addEvent(Request $request)
{
    //dd($request);

    $event = new Event;
    $event->title = $request['title'];
    $event->start_date = $request['start_date'];
    $event->end_date = $request['end_date'];
    $event->save();

    \Session::flash('success','Event added successfully.');
    return redirect('/events');
}

web.php (routing)

Route::get('/events', 'EventController@index');
Route::post('/events', 'EventController@addEvent');

and then there is the index function on which I work the most currently to modify the table (make it editable, etc).

public $trial = [];

//
public function index()
{
    $events = [];
    $data = Event::all();
    if($data->count()) {
        foreach ($data as $key => $value) {
            $events[] = Calendar::event(
                $value->title,
                true,
                new \DateTime($value->start_date),
                new \DateTime($value->end_date.' +1 day'),
                null,
                // Add color and link on event
                [
                    'color' => '#f05050',
                    'url' => '/events',


                ]
            );
        }
    }

    $calendar = Calendar::addEvents($events) //add an array with addEvents
    //->addEvent($eloquentEvent, [ //set custom color fo this event
        //'color' => '#800',
    //])
        ->setOptions([ //set fullcalendar options
        'firstDay' => 1,
        'selectable' => true,
        'unselectAuto' => false,
        'selectOverlap' => false,
        'editable' => true,
        'businessHours' => [
            'dow' => [ 1, 2, 3, 4, 5 ],
            'start'=> '08:00',
            'end' => '19:00',
        ]

    ])->setCallbacks([ //set fullcalendar callback options (will not be JSON encoded)
        'eventClick' => 'function(event) {
            console.log("You clicked on an event with a title of: " + event.title);
     }',

        'select' => 'function(start, end) {
            console.log("Selection start: " + start.format() + " selection end: " + end.format());

            $trial = {
                title: "rent",
                start_date: start.format(),
                end_date: end.format()
            };
            console.log($trial);


        }',
    ]);

any suggestions would be welcome.

EDIT: generally, pretty much the only way i know how to pass data to be used in functions in controller is submitting it through form

  • 写回答

1条回答 默认 最新

  • doupang9614 2018-05-18 23:57
    关注

    I don't use Angular specifically, but it's got many similarities with Vue. I use a platform-agnostic package called Axios which allows you to send requests to your server.

    To backtrack a second, a form is basically a general way to send a post request. The url is specified in the form, and the values are the input fields.

    With any package, you would do something similar:

    • specify the url
    • specify the request type (post, get, etc)
    • pass parameters (but not required)

    With Axios, it would look something like:

    axios.post('/mySite.com/something', { datum1: 'value', datum2: true })
          .then(response => () {
             this.someFunction(response.data);
          });
    

    This has some ES6 magic in it (arrow functions), but this is very similar to many requests. Like I said, the second parameter is optional, or you can even pass a data object.

    • Don't forget to also include the csrf token. This is easiest if you just add in as a meta tag in your page head (see the Laravel Docs), but you can also pass it in directly as a _csrf parameter.
    评论

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 解riccati方程组