2016-11-17 155 views
0

我想運行查詢,將獲取給定日期之間的數據,但我沒有得到所需的結果。我有一個空白數組作爲輸出。Laravel數據庫查詢問題?

這是我在控制器代碼:

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; 
} 
} 

回答

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);   
     } 
    } 
} 
+0

謝謝@Rimon – SaMeEr

0

你試過if語句中返回從$response,像這樣?

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; 

} 
} 
+0

由於一噸@Luuk – SaMeEr