dollar dollar - 2 months ago 21
PHP Question

values modified by eloquent create() in laravel 5.2

I am creating a code by which i can replicate the multiple rows of table and its related tables.

I am able to replicate parent table successfully, but while replicating the contents of child table it is modifying the values which i am unable to figure it out.

my controller code is

public function copyshowtime($cinema_id,$show_date)
{
$date=new Carbon($show_date);
$current_show_date=$date->format('Y-m-d');
$next_show_date=$date->addDay()->format('Y-m-d');

$movieshowtime=Movies_showtimes::with('showdata')->where([['cinema_id','=',$cinema_id],['show_date','=',$current_show_date]])->get();

foreach ($movieshowtime as $item)
{
$item->show_date=$next_show_date;
$item->show_id=NULL;
$newshowtime=$item->replicate();
$newshowtime->push();


foreach ($item->showdata as $sd)
{

$newshowdata = array(
'showdata_id' => NULL,
'show_id'=>$newshowtime->id,
'category_id'=>$sd->category_id,
'showdata_category'=>$sd->showdata_category,
'showdata_rate'=>$sd->showdata_rate

);

// print_r($newshowdata);
Movies_showdata::create($newshowdata);
}
}
}


When I am printing the complete array its showing proper data which i am trying to insert ,check below

enter image description here

But after inserting it into table I see this data in table, the values of show_id and showdata_rate is getting zero value and other columns getting correct data inserted.

enter image description here

I am unable to figure out this problem as I am nowhere changing the data to zero before inserting it into table.

Answer

Make sure show_id and showdata_rate are fillable in your Movies_showdata model:

class Movies_showdata extends Model {
  protected $fillable = ['show_id', 'showdata_rate', ...other fillable fields ];
}