我是Laravel的學習者。我現在試圖學習的是將JSON
文件從後端發送到前端,以便我使用這個JSON
文件繪製圖形。從PHP轉換爲JSON文件
在我的模型中,我寫了一個函數,它將提取值和時間戳created_at
。這裏是一段代碼,它將返回與單個網站相關聯的谷歌網頁速度值和時間戳。然後,我想使用JS
繪製圖表,其中垂直值是谷歌頁面速度,水平是時間戳created at
。
任何人可以幫助我嗎?我是否需要將返回值更改爲數組?
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Notification;
use App\Status;
class ChartController extends Controller
{
public function speedHistory(){
$o_status = Status::where('name','speed')->first();
$o_response = $this->statuses()->where('status_id', $o_status->id)
->select('values AS value', 'created_at AS timestamp')->orderBy('created_at','DESC')->get();
if($o_response){
return response()->json($o_response);
}
// return an empty json array instead of false
//return false;
return response()->json(array());
}
}
和trhe路線
路線::得到( 'JSON', 'ChartController @ speedHistory');
不斷抱怨方法沒有定義。這是我的模型 lass Notification擴展模型 protected $ fillable = ['id','website_url','email','slack_channel','check_frequency','alert_frequency','speed_frequency','active'];
public function statuses(){
return $this->belongsToMany('App\Status')->withPivot('values')->withTimestamps();
}
L4'返回響應:: json($ data_array,$ status_code);'或L5'return response() - > json($ data_array,$ status_code);' – Peon
您能解釋一下'$ o_response->的值s'部分?如果我沒有弄錯,$ o_reponse應該是一個數組,你不能在數組上調用對象屬性。嘗試將$ o_reponse查詢從' - > get()'更改爲' - > first()' – devk
@ devk:$ o_response-> values,它是一個數組。一個網站可能有不同的谷歌頁面速度,例如70,80,100等,它存儲在數據透視表中的值,我提取它作爲$ o_response->值。 –