2016-05-25 2 views
0

2つのテーブルのデータを表示するAPIを作成しようとしていますが、この時点で固まっています。Api Laravelでのクエリ

これは私のApiControllerです:

public function postDetaillog(Request $request) 
{ 
    $response = array(); 
    $validator = Validator::make(
     $request->all(),    
     [ 
      'id'=> 'required', 
     ] 
    ); 

    if ($validator->fails()) { 
     $message = $validator->errors()->all();  

     $result['api_status'] = 0; 
     $result['api_message'] = implode(', ',$message);  

     $res = response()->json($result); 
     $res->send(); 
     exit; 
    } 

    $data = DB::table('log_patrols') 
     ->where('id', $request->input('id')) 
     ->first(); 

    $site = asset("uploads").'/'; 

    $result = DB::table('log_patrol_details') 
     ->select("*",DB::raw("concat('$site',photo1) as photo1"),DB::raw("concat('$site',photo2) as photo2"),DB::raw("concat('$site',photo3) as photo3")) 
     ->where('id', $request->input('id')) 
     ->first(); 

    if (count($result) == 0) { 
     $response['api_status'] = count($result); 
     $response['api_message'] = "No data"; 
    } else { 
     $response['api_status'] = 1; 
     $response['api_message'] = "success"; 
     $response['data']  = $data; 
     $response['result']  = $result; 
    } 
    return response()->json($response); 
} 

最初のテーブル this the table 二台 second table

私は結果を取得しようとするたびに、それは常に私に0 = no data

を与えます

私を助けてもらえますか?

+0

あなたはmysqlの上で、そのクエリを試してみましたか?なぜあなたは 'CONCAT()'を使っていますか? – phaberest

+0

私はconcatを使って私の写真にURLを与えます –

+0

またはあなたにはsoutionがありますか?私のために ? –

答えて

0

あなたが入手したものがコレクションの場合は、if ($result->count() == 0)を使用して空であるかどうかを確認することができます。

変更し、次のように:

if ($result->count() == 0) { 
    $response['api_status'] = 0; // Don't use useless logic 
    $response['api_message'] = "No data"; 
} else { 
    $response['api_status'] = 1; 
    $response['api_message'] = "success"; 
    $response['data']  = $data; 
    $response['result']  = $result; 
} 
+0

私はそれを使用しようとしましたが、その結果のエラー "他のオブジェクトのメンバー関数count()を呼び出す"他の? –

+0

Laravelのどのバージョンを使用していますか? – phaberest

+0

iamを使用する5.1。遅い情報を申し訳ありません^^ –