You need to create Refund model which belongs to customer and than you can attach services.
Form and tables with relationships
Hi i have 4 tables Customers / Refunds / Refunds_Services / Services I cannot send correctly parameters from a form submittion. The 3 models are : Customer model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customers extends Model
{
public $primaryKey = 'id';
protected $fillable = [
'id',
'contr_nom',
'contr_cog',
'benef_nom',
'benef_cog',
'email',
'polizza',
'targa',
'data_sin',
'iban',
'int_iban',
'dossier',
'cliente',
];
public function refunds()
{
return $this->hasMany(Refunds::class);
//return $this->belongsToMany(Services::class)->withPivot(['services_id','importo','data_ric']);
}
}
Refunds model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Refunds extends Model
{
public $primaryKey = 'id';
protected $fillable = [
'id',
'date_ref',
'status_ref',
];
public function services()
{
return $this->belongsToMany(Services::class)->withPivot(['services_id','services_amount','services_status']);
}
public function customers()
{
return $this->belongsTo(Customers::class);
}
Services model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Services extends Model
{
public $primaryKey = 'id';
protected $fillable = [
'code',
'name',
];
public function refunds()
{
return $this->belongsToMany(Refunds::class);
}
}
Customer table store customer datas and each customer can do various refunds requests (with own date and status) . Any Refunds request can have multiple services with own import, date and so on. All these i put in a form. There are customers datas text boxes and a button that can add services .
Then each time u press all form Save button u made one Refund request. in my controller i did this method trying to store datas in all different tables
$customers = Customers::create($request->all());
$tipo = $request->input('tipo', []);
$importo = $request->input('importo', []);
for ($i=0; $i < count($tipo); $i++) {
if ($tipo[$i] != '') {
$test = $customers->refunds();
$test->services()->attach($tipo[$i], [
'services_id' => $tipo[$i],
'importo' => $importo[$i]
]);
}
}
return redirect("/");
Customers data are saved but i cannot access to bridge table refunds_services to store the services of the refunds request. In effect I also have to store refund request datas in refunds table. How can i do? Thx a lot
@niloleon Why do you create refunds again?
$refunds = Refunds::create($request->all());
public function storeRefunds(RefundsPost $request) {
$customers = Customers::create($request->all());
$refunds = $customers->refunds()->create([
'date_ref' => request('date_ref'),
'status_ref' => request('stato'),
]);
$tipo = $request->input('tipo', []);
$importo = $request->input('importo', []);
for ($i = 0; $i < count($tipo); $i++) {
if ($tipo[$i] != '') {
$refunds->services()->attach($tipo[$i], [
'services_id' => $tipo[$i],
'services_amount' => $importo[$i]
]);
}
}
return redirect('/'');
}
Please or to participate in this conversation.