将数据传递给2个控制器,1个返回视图,1个返回JSON响应

将数据传递给2个控制器,1个返回视图,1个返回JSON响应,json,laravel-5,Json,Laravel 5,我正在从视图中获取数据 {!! Form::open() !!} {!! Form::input('date', 'from', date('Y-m-d'), array('class' => 'form-control')) !!} {!! Form::input('date', 'to', date('Y-m-d'), array('class' => 'form-control')) !!} {!! Form::submit('Go', array('c

我正在从视图中获取数据

{!! Form::open() !!}
    {!! Form::input('date', 'from', date('Y-m-d'), array('class' => 'form-control')) !!}
    {!! Form::input('date', 'to', date('Y-m-d'), array('class' => 'form-control')) !!}
    {!! Form::submit('Go', array('class' => 'btn btn-warning')) !!}
 {!! Form::close() !!}
一旦用户提交,我就有一个控制器来获取输入并返回一个视图。在我的ReportsController中,我有:

public function report(Request $request)
{

    $from = \Request::input('from');
    $to = \Request::input('to');

    $resources = Resource::where('resources.created_at', '>=', $from)
        ->where('resources.created_at', '<=', $to)
        ->get();

    return view('reports.index', compact('resources'));
}
但是,我希望使用来自用户的相同数据输入来更新将在上面返回的reports.index视图中使用的json。在同一控制器中,我希望有如下内容:

public function json(Request $request)
{

    $from = \Request::input('from');
    $to = \Request::input('to');

    $resources = Resource::where('resources.created_at', '>=', $from)
        ->where('resources.created_at', '<=', $to)
        ->get();

    return Response::json(['data'=> $resources]);
}

我怎样才能做到这一点?非常感谢你的帮助

如果在同一个视图中使用json,为什么不直接将json传递给报表函数中的视图呢

大概是这样的:

public function report(Request $request)
{
    [....]
    return view('reports.index', compact('resources'))
       ->with('json'=>Response::json(['data'=> $resources]));
}

最后想到了一个解决方案——将日期存储在表中。虽然不那么优雅,但我现在可以避免学习ajax了。嗨。谢谢你的帮助。我在执行您建议的操作时遇到了这个错误:语法错误,意外的“=>”T\u双箭头。无论如何,我会在其他地方寻找正确的语法。再次感谢!