3 回答

TA貢獻1777條經驗 獲得超3個贊
在這里,您可以創(chuàng)建一個關聯(lián)數(shù)組并通過 json 發(fā)送。
public function something()
{
$data=Model::all();
$comments=Comment::all()
return response()->json([
'data'=>$data,
'comments'=>$comments
]) //here i want to send both $data and $comments to that view
}
在 Vue 中你可以寫這樣的東西。
export default
{
data(){
return {
comments:[]
}
},
created()
{
axios.get("a url")
.then(res=>{
this.comments = [...res.data.comments] //If you are using ES6
}
}
}

TA貢獻1946條經驗 獲得超3個贊
您可以通過使用responselaravel的方法簡單地實現(xiàn)這一點
public function something()
{
$data=Model::all();
$comments=Comment::all()
return response()->json([
'data'=> $data,
'comments'=> $comments
], 200);
}
或使用相同方法的另一種方式
public function something()
{
$data=Model::all();
$comments=Comment::all()
return response([
'data'=> $data,
'comments'=> $comments
], 200);
}
在您的組件中,您可以簡單地使用
export default
{
created()
{
axios.get("a url")
.then(res=>{
console.log(res.data.data)
console.log(res.data.comments)
}
}
}
謝謝

TA貢獻1770條經驗 獲得超3個贊
你可以簡單地做到這一點
public function function()
{
$data=Model::all();
$comments=Comment::all()
return response()->json([
'data'=>$data,
'comments'=>$comments
]) //here i want to send both $data and $comments to that view
}
- 3 回答
- 0 關注
- 174 瀏覽
添加回答
舉報