here si my model
class Gallery extends Model{ protected $table = 'gallery'; protected $fillable = [ 'id','pages_id','title','subtitle' ]; protected $casts =[ 'photo'=>'array' ];
}
here is the controller
public function add_gallery(Request $request, $id) { $gal = Gallery::create([ 'title' => $request['titlu'], 'subtitle' => $request['autor'], 'photo' => json_encode($p), 'pages_id' => $id ]); }
the line ‘photo’ => json_encode($p) does not work, it says that column photo is empty, $p is not empty
Advertisement
Answer
$photo should be added to $fillable
protected $fillable = ['id','pages_id','title','subtitle','photo'];
Since You do using casting, You do not use json_encode/json_decode manually because Laravel is doing it for You.
$gal = Gallery::create([ 'title' => $request['titlu'], 'subtitle' => $request['autor'], 'photo' => $p, 'pages_id' => $id ]);
$p variable is not present in add_gallery() method.