laravel数据库查询中的问题?

时间:2016-11-17 11:13:43

标签: laravel laravel-5 laravel-5.3

我正在尝试运行查询,它将在给定日期之间获取数据但我没有得到所需的结果。我有一个空白数组作为输出。

这是我在控制器中的代码:

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use DB;
use \Carbon\Carbon;

class reportController extends Controller
{
    public $response;

public function contact_report(Request $request){
    if($request->header('content-type')=='application/json'){
        $from = \Carbon\Carbon::parse($request->from)->format('Y-m-d');
        $to = \Carbon\Carbon::parse($request->to)->format('Y-m-d');
        $cont_report = DB::connection('mysql_freesubs')->table('contact as c')
                        ->select("c.fname", "c.lname","c.dob", "cl.value","c.doc" )             
                        ->join("communication_link AS cl", "c.id", "=", "cl.cont_id")
                        ->join("contact_communication AS cc", "cl.coco_id", "=", "cc.id")
                        ->where("cc.type", "like","mobile%")
                        ->whereBetween("c.doc", [new Carbon($from), new Carbon($to)])
                        ->get();
        $response = $cont_report;

    }
    else
        $response =  response()->json(['data'=>[],'error'=>1,'success'=>0,'error_msg'=>'not application/json', 'message'=>'Content type should be application/json']);

    return $response;
}
}

2 个答案:

答案 0 :(得分:0)

您是否尝试过在if语句中返回class reportController extends Controller { public $response; public function contact_report(Request $request){ if($request->header('content-type')=='application/json'){ $from = \Carbon\Carbon::parse($request->from)->format('Y-m-d'); $to = \Carbon\Carbon::parse($request->to)->format('Y-m-d'); $cont_report = DB::connection('mysql_freesubs')->table('contact as c') ->select("c.fname", "c.lname","c.dob", "cl.value","c.doc" ) ->join("communication_link AS cl", "c.id", "=", "cl.cont_id") ->join("contact_communication AS cc", "cl.coco_id", "=", "cc.id") ->where("cc.type", "like","mobile%") ->whereBetween("c.doc", [new Carbon($from), new Carbon($to)]) ->get(); $response = $cont_report; return $response; } else $response = response()->json(['data'=>[],'error'=>1,'success'=>0,'error_msg'=>'not application/json', 'message'=>'Content type should be application/json']); return $response; } } ,如此?

  $uibModalInstance.dismiss($scope.item.Catalogs);

答案 1 :(得分:0)

试试这个:

use Carbon\Carbon;

class reportController extends Controller
{
    public $response;

    public function contact_report(Request $request){
        if(0 === strpos($request->headers->get('Content-Type'), 'application/json')){

            $from = Carbon::parse($request->from)->format('Y-m-d');
            $to   = Carbon::parse($request->to)->format('Y-m-d');
            $cont_report = DB::connection('mysql_freesubs')->table('contact as c')
                            ->select("c.fname", "c.lname","c.dob", "cl.value","c.doc" )             
                            ->join("communication_link AS cl", "c.id", "=", "cl.cont_id")
                            ->join("contact_communication AS cc", "cl.coco_id", "=", "cc.id")
                            ->where("cc.type", "like","mobile%")
                            ->whereBetween("c.doc", [$from, $to])
                            ->get();

            //$this->response = $cont_report;
            return response()->json(['response' => $cont_report], 200);
        }
        else {
            return response()->json([
                'data'=> [],
                'error'=> 1,
                'success'=> 0,
                'error_msg'=>'not application/json', 
                'message'=>'Content type should be application/json'
            ], 404);        
        }
    }
}