mardi 22 novembre 2016

How do I move this exploded data into another table? Laravel 5.3

I'm doing a database refactor at the moment to move data into pivot tables and create proper relationships. It's an old database, so there's some interesting choices of storage... Something like this:

enter image description here

Yeah it's good fun. Anyway, I'm still a bit shakey on how laravel does it's objects and arrays, which still seem like madness to me. So I have this query here, which is one half:

public function move() {

        $prefs = DB::table('users')->select('id', 'Preferences')->where('Preferences', '!=', '')->get();

            foreach ($prefs as $pref) {
                 $tags = $pref->Preferences;
                 $tag = explode(',', $tags);
                 print_r('$tag');
             }

Right now print_r just prints $tag$tag$tag$tag. I know it's got something to do with me messing up my understanding of how laravel works with arrays. So how to do I do this properly?

My goal is to go through each user, grab their tags, explode those tags, then print them out to the pivot table (tag_user) beside the user_id.

Note: The tags are contained in the preferences collumn, which is shown in the above image.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire