Using Laravel 5.7.
Designing an app with many views containing forms.
In this view:
- there is one (only one) client_id field. This is hidden and the value is retrieved from an url segment.
- other fields are dynamic (date, type, comments) and refers to one or more products bought by this client.
I cannot figure out how to save input values from the dynamic fieds to the database (sorry, I am quite new to coding) See below my current code and attempts. I found related answers in stack overflow could not make them fit to my case. I would appreciate help
STORE() FUNCTION IN CONTROLLER
public function storeDYNarray(Request $request)
{
$client = $request->input('client_id');
$all = $request->input();
$products = $request->input('product');
//dd($all); SEE BELOW
//THIS CREATES AS MANY NEW ENTRIES IN PRODUCTS TABLE AS NEEDED WITH THE CORRESPONDING client_ID
foreach ($products as $product){
$dia = new Product;
//client_id is retrieved from an URL segment
$dia->client_id = $request->input('client_id');
//DON'T KNOW HOW TO SAVE VALUES FROM THE DYNAMIC FIELDS
$dia->save();
}
return dd($all); OUTPUT
array:3 [▼
"_token" => "uvtLaCiuAueBIuyWkoCoOTdQzYB1paxhnLw0lbyO"
"client_id" => "898"
"product" => array:2 [▼
1 => array:3 [▼
"date" => "2019-03-13"
"type" => "new"
"comment" => "surplus"
]
2 => array:3 [▼
"date" => "2019-03-28"
"type" => "used"
"comment" => "good condition"
]
]
]
I tried strategies along those lines...I kind of see what does not make sense but cannot do better:
foreach ($products as $product){
$dia = new product;
$dia->client_id = $request->input('client_id');
foreach ($product as $key=>$value){
$dia->product_date = $request->input('date'); //or input('*.date');
$dia->product_type = $request->input('type'); //or input('*.type');
$dia->product_comment = $request->input('comment'); //or input('*.comment');
}
$dia->save();
}
//for the second loop, I also tried using a $i variable and to retrieve values with $product[$i][date]...
$client_products = array();
foreach ($products as $product)
{
$client_products[] = new product(array(
'client_id' => $client,
'product_date' => $product['date'],
'product_type' => $product['type'],
'comment_about_product' => $product['comment'],
));
}
$client->products()->saveMany($client_products);
PRODUCTS TABLE
class CreateProductsTable extends Migration
{
public function up()
{
//KEPT VALIDATION PARAMETERS SIMPLE FOR KNOW
Schema::create('products', function (Blueprint $table) {
$table->increments('product_id');
$table->string('client_id')->nullable($value = true);
$table->date('product_date')->nullable($value = true);
$table->string('product_type')->nullable($value = true);
$table->longText('comment_about_product')->nullable($value = true);
$table->timestamps();
});
}
PRODUCT MODEL
class product extends Model
{
//GIVEN THE STRUCTURE OF ARRAYS WHEN dd($all); I DONT THINK I NEED THAT
/*
public $fillable = [
'client_id',
'product_date',
'product_type',
'comment_about_product_type',
];
*/
public function client(){
return $this->belongsTo('App\client');
}
}
CLIENT TABLE
<?php
//use Illuminate...
class CreateclientsTable extends Migration
{
public function up()
{
Schema::create('clients', function (Blueprint $table) {
$table->string('client_id');
$table->primary('client_id');
//other columns...
$table->integer('user_id')->nullable($value = true);
$table->timestamps();
});
}
CLIENT MODEL
class client extends Model
{
protected $primaryKey = 'client_id';
public $incrementing = false;
protected $keyType = 'string';
public function user(){
return $this->belongsTo('App\User');
}
public function products(){
return $this->hasMany('App\product');
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire