我正在創建一個電影 api,可以在其中顯示電影。
為此,我有兩張表,一張是以cat_id作為外鍵的電影,第二張是以cat_id作為主鍵的類別。具有以下欄位

我已經回傳了像這樣的電影 API
此外,您可以訪問此鏈接https://backend.hac-inc.org/api/movies
但想要轉換它喜歡在其類別的物件中獲取電影,如下所示
在此處輸入影像描述
{
"movies":[
"Action":[{
"id": 1,
"title": "Attack",
"description": "Attack _ Official Trailer _ John A, Jacqueline F, Rakul Preet S _ Lakshya Raj Anand_ April 1st, 2022",
"cast": "John A, Jacqueline F, Rakul Preet S _ Lakshya Raj Anand",
"language": "Hindi",
"category": "Action",
"movie_path": "https://backend.hac-inc.org/images/movies/1647589765.jpg",
"movie_url": "https://backend.hac-inc.org/movies/1647589765.mp4",
"movie_image_full_name": "1647589765.jpg",
"movie_file_full_name": "1647589765.mp4",
"created_at": "2022-03-17T22:49:25.000000Z",
"updated_at": "2022-03-17T22:49:25.000000Z"
},
{
"id": 2,
"title": "Bachchhan Paandey",
"description": "Bachchhan Paandey _ Official Trailer _ Akshay Kriti Jacqueline Arshad _ Sajid N _Farhad S_18th March",
"cast": "Akshay Kriti Jacqueline Arshad",
"language": "Hindi",
"category": "Action",
"movie_path": "https://backend.hac-inc.org/images/movies/1647596446.jpg",
"movie_url": "https://backend.hac-inc.org/movies/1647596446.mp4",
"movie_image_full_name": "1647596446.jpg",
"movie_file_full_name": "1647596446.mp4",
"created_at": "2022-03-18T00:40:46.000000Z",
"updated_at": "2022-03-18T00:40:46.000000Z"
},{
"id": 5,
"title": "Rudra",
"description": "Rudra _ Official Trailer _ Coming Soon _ DisneyPlus Hotstar",
"cast": "Ajay Devgan",
"language": "Hindi",
"category": "Action",
"movie_path": "https://backend.hac-inc.org/images/movies/1647596619.jpg",
"movie_url": "https://backend.hac-inc.org/movies/1647596619.mp4",
"movie_image_full_name": "1647596619.jpg",
"movie_file_full_name": "1647596619.mp4",
"created_at": "2022-03-18T00:43:39.000000Z",
"updated_at": "2022-03-18T00:43:39.000000Z"
}
],
"Romantic":[{
"id": 4,
"title": "October",
"description": "October _ Official Trailer _ Varun Dhawan _ Banita Sandhu _ Shoojit Sircar",
"cast": "Varun Dhawan _ Banita Sandhu _ Shoojit Sircar",
"language": "Hindi",
"category": "Romantic",
"movie_path": "https://backend.hac-inc.org/images/movies/1647596567.jpg",
"movie_url": "https://backend.hac-inc.org/movies/1647596567.mp4",
"movie_image_full_name": "1647596567.jpg",
"movie_file_full_name": "1647596567.mp4",
"created_at": "2022-03-18T00:42:47.000000Z",
"updated_at": "2022-03-18T00:42:47.000000Z"
}]
}
我用過的集合中有很多功能,但沒有幫助。請有人幫助我,我被嚴重卡住了。
嘗試轉換我的簡單電影表 API 以將它們顯示在一組類別中。
uj5u.com熱心網友回復:
您可以在 Category 模型中建立關系以獲取類別串列,并且在每個類別中,都會有 movies 屬性,其中包含與該類別相關的電影。
將此添加到您的類別模型中:
public function movies()
{
return $this->hasMany(Movie::class, 'cat_id', 'id');
}
然后要獲取電影屬性,您可以使用以下命令:
return $categories->with(['movies']);
uj5u.com熱心網友回復:
$movies = Movie::all();
return $movies->groupBy('category');
我剛寫了這個,它奏效了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/448884.html
上一篇:是否可以使用非機器人令牌從不和諧公會中獲取所有成員的串列?
下一篇:印資企業初印象
