Siros Fakhri
Siros Fakhri

Reputation: 2323

How to Save an Array Index and Value at Separate Col With Laravel

Sorry if My English is Bad.

I Have Table Just like this,Only Two Col :

Meta-col , Value-col

Now I Get My Inputs at Array Like This:

$settings=$request->all();

So My Result is something like This :

{
"_method": "PATCH",
"_token": "Yk3l3GsacKV6plkXH0zNKLVL2HcYjZbg2kB8KeAp",
"site_title": test,
"blog_description": blog test,
"admin_email": [email protected],
"site_language": "eng",
...
}

Well,I Want Save Index in Meta-col & Values in Value-col.

My Table Should be Like This :

Meta-col           Value-col
 ----------        ----------
 site_title          value 
 blog_description  blog test
  .......           ....

I try This Codes But Not Working and Get Error :

$settings->save();

or

 foreach ($metas as $meta =>$value){
       echo "<p style='color:red;text-align: center'>". $meta ."</p>";
       echo "<p style='color:blue;text-align: center'>". $value ."</p>";
 }

Well,With This Foreach I can Get Each Meta & Value separate But Still I Don't Know How To Manage it.

And Also Tested Some other Code's But Still Not Work.

Upvotes: 0

Views: 920

Answers (1)

alegria
alegria

Reputation: 1145

If you have a Model for your table in database you can do :

Say your Model is MyModel

foreach ($metas as $meta =>$value){
        $m = new MyModel();
        $m->Meta_col = $meta;
        $m->Value_col = $value;
        $m->save();
 }

In order for your variable to be save()d to the database it has to be of type

class MyModel extends Model {} where Model being Illuminate\Database\Eloquent\Model.

I didn't catch your laravel version so here is a starter from the last version.

Upvotes: 4

Related Questions