I'm developing rest api in laravel, here im trying to post array data in database, but i could not figure out how to do it, can some one guide me in this ? i'm new to 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();
}
}
productdetails.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');
}
}
example for array of data
[{productquantity: '5', productprice: '5', productgst: '5', productname: 'xyz'}, {productquantity: '5', productprice: '5', productgst: '5', productname: 'ABC'}]
How to post such kind of above data in the data base ?
CodePudding user response:
Model::create()
doesn't work like that. You should loop over the array and create rows individually.
Also, your validation won't work. Docs on array validation
CodePudding user response:
Try this: (save it to database)
$productinvoice =json_encode( $request->your_array_json_data );
...save it to database. When you want to use It inside client side You should parse It to array of json data. Like this: (use it inside client side part)
JSON.parse($productinvoice)
When You want to use it inside backend part after retrieve it from database. This is how You could convert it to php array : (use it inside backend part)
$productinvoice = json_decode($productionDetails->productinvoice, true);