samedi 25 mai 2019

Way to store a given attribute data as a json string in Laravel

for a given Model i've got the Post model a $fillable attribute where i've define a list a attibutes like this:

protected $fillable = [
        'title',
        'slug',
        'body',
        'type',
        'status',
        'inputs',
    ];

In the user interface for some reasons there will,there are some customfield that i would like to store as string in the 'inputs' attrbute.Here is my controller:

public function store(StorePost $request){
    $data=$request->validate();
    $p=Post::create($data);
    return response()->json(PostResource::collection($p)); 
}

But the thing is that,for the attribute input,would like it value to be a json string as there are some custom fields and data will also come from the form and all their names will be prefixed by input_ like for example :input_body,input_slug,input_title.But i there are not defined in the model,so what i would like to do is to create a paire-key value from them in the controller in the following structure :

[
    custom_field_name1=>value1,
    custom_field_name2=>value2
]

But has the there names will depend on the user that will created them,i dont know how to have a controller how can identifiy them automatically to create the array above.the then converts them in a json string to store them in the input attribute.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2K3FLQH
via IFTTT

Aucun commentaire:

Enregistrer un commentaire