我正在 Laravel 中開發 rest api,在這里我試圖在資料庫中發布陣列資料,但我不知道該怎么做,有人可以指導我嗎?我是 Laravel 的新手
ProductdetaisController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\productdetails;
class ProductdetailsController extends Controller{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return productdetails::all();
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'productname' => 'required',
'productid' => 'required',
'productdescription' => 'required',
'productimage' => 'required',
'productinvoice' => 'required',
]);
return productdetails::create($request->all());
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
return productdetails::find($id);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$productdetails = productdetails::find($id);
return $productdetails->update($request->all());
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
return productdetails::find($id)->delete();
}
}
產品詳情.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class productdetails extends Model
{
use HasFactory;
protected $fillable = ['productname', 'productid', 'productdescription', 'productimage', 'productinvoice'];
}
2021_09_25_075455_create_productdetails_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductdetailsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('productdetails', function (Blueprint $table) {
$table->id();
$table->string('productname');
$table->string('productid');
$table->string('productdescription');
$table->array('productinvoice'); <----- here i want to store array of data ------>
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('productdetails');
}
}
資料陣列示例
[{productquantity:'5',productprice:'5',productgst:'5',productname:'xyz'},{productquantity:'5',productprice:'5',productgst:'5',productname:'ABC '}]
如何在資料庫中發布此類上述資料?
uj5u.com熱心網友回復:
Model::create()不能那樣作業。您應該遍歷陣列并單獨創建行。
關于此方法的 Laravel 檔案
此外,您的驗證將不起作用。陣列驗證檔案
uj5u.com熱心網友回復:
試試這個:(保存到資料庫)
$productinvoice =json_encode( $request->your_array_json_data );
...保存到資料庫。當您想在客戶端使用它時,您應該將它決議為 json 資料陣列。像這樣:(在客戶端部分使用它)
JSON.parse($productinvoice)
當您想在從資料庫中檢索它后在后端部分使用它時。這就是您可以將其轉換為 php 陣列的方法:(在后端部分中使用它)
$productinvoice = json_decode($productionDetails->productinvoice, true);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/351804.html
