如何将此Laravel集合数组转换为json格式,如下所示.
//All records from users table.
$users = DB::table('users')->get();
// Required json format.
return '{
"data": [
{
"DT_RowId": "row_1",
"id": "Tiger",
"clear": "Nixon",
"fsssf": "System Architect",
"tex": "t.nixon@datatables.net",
"created_at": "Edinburgh",
"updated_at": "Edinburgh"
},
{
"DT_RowId": "row_2",
"id": "Tiger",
"clear": "Nixon",
"fsssf": "System Architect",
"tex": "t.nixon@datatables.net",
"created_at": "Edinburgh",
"updated_at": "Edinburgh"
}
],
"options": [],
"files": []
}';
Run Code Online (Sandbox Code Playgroud)
对不起基本问题,但我无法将此转换为此jso.
我正在使用 Laravel 的 API 资源将资源转换为 API 调用的数组,并且它工作正常,我是否可以在一次调用中检索多个模型的数据?至于获取用户的 JSON 数据和 Pages JSON ?或者我需要一个单独的电话。
这是我迄今为止尝试过的
//Controller
public function index(Request $request)
{
$users = User::all();
$pages = Page::all();
return new UserCollection($users);
}
//API Resource
public function toArray($request)
{
return [
'name' => $this->name,
'username' => $this->username,
'bitcoin' => $this->bitcoin,
];
}
Run Code Online (Sandbox Code Playgroud)
任何帮助都将受到高度赞赏
如何将订阅计划设置为特定价格?我想提供试用期,但我不希望它们100%免费。我想说1美元,这样他们就会觉得自己在做出财务承诺。免费订一个不好的期望。从外观上看,我只能选择免费。如何更改?
如何使用Laravel插入多个文本字段数据.我有两个领域.
<input type="text" name="title[]" />
<input type="text" name="email[]" />
Run Code Online (Sandbox Code Playgroud)
现在在控制器中我可以得到如下的这些值.
$name = $request->title;
$description = $request->email;
Run Code Online (Sandbox Code Playgroud)
这是我打印这些值时得到的.
Array
(
[0] => title1
[1] => title2
)
Array
(
[0] => desc1
[1] => desc2
)
Run Code Online (Sandbox Code Playgroud)
如何用雄辩的方式添加它.
使用 Laravel 从数据透视表中搜索。
这是我的表结构:
Product
id
name
Categories
id
name
product_category (Pivot table)
id
category_id
product_id
//products can have multiple categories
Run Code Online (Sandbox Code Playgroud)
产品型号:
public function categories(){
return $this->belongsToMany(Category::class, 'product_category');
}
Run Code Online (Sandbox Code Playgroud)
按类别 ID 搜索所有产品的最佳方法是什么?目前我正在这样做,这似乎不是一种有效的方式:
//Controller
$categories = product_category::where('category_id',1)->get();
Run Code Online (Sandbox Code Playgroud)
现在我必须遍历类别,然后获取产品并将其传递给视图?知道如何以有效的方式做到这一点吗?