0

I need to seed Reg-Ex patterns in SqLite database. I'm using Laravel 7. Here is my seeder:

<?php
use App\PhoneValidetionRules;
use Illuminate\Database\Seeder;

class PhoneValidetionRulesSeeder extends Seeder
{
public function run()
{
    $rules = '
    {
        "iPhone": "\\biPhone\\b|\\biPod\\b",
        "BlackBerry": "BlackBerry|\\bBB10\\b|rim[0-9]+|\\b(BBA100|BBB100|BBD100|BBE100|BBF100|STH100)\\b-[0-9]+",
        "HTC": "HTC|HTC.*(Sensation|Evo|Vision|Explorer|6800|8100|8900|A7272|S510e|C110e|Legend|Desire|T8282)|APX515CKT|Qtek9090|APA9292KT|HD_mini|Sensation.*Z710e|PG86100|Z715e|Desire.*(A8181|HD)|ADR6200|ADR6400L|ADR6425|001HT|Inspire 4G|Android.*\\bEVO\\b|T-Mobile G1|Z520m|Android [0-9.]+; Pixel",
        "Nexus": "Nexus One|Nexus S|Galaxy.*Nexus|Android.*Nexus.*Mobile|Nexus 4|Nexus 5|Nexus 6"
    }';
    $data = [];
    foreach (json_decode($rules, true) as $key=>$value){
        $data[] = [
            'name' => $key,
            'regex' => $value
        ];
    }
    PhoneValidetionRules::insert($data);
}
}

When I seed it, all \bare converted to \x08. Here is database:

enter image description here

Here is table blueprint:

Schema::create('phone_validetion_rules', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('regex');
    });

If I update regex from PhpStorm manually, It insert correctly. How can I insert Reg-Ex to database using Laravel Seeder?

1 Answer 1

1

Why not inserting them with json_encode() like so

'regex' => json_encode($value)

the data will inserted like "\biPhone\b|\biPod\b" if that's what you are looking for.

Sign up to request clarification or add additional context in comments.

4 Comments

Upvote for a nice trick. But it is not the answer i need. why it converts \b are converted to \x08 ? And what is the exact method to upload Reg-Ex to database using larael Model? Raw Sql can do this also. But I don't want to write raw Sql in my project.
This doesn't work. for json_decode(PhoneValidetionRules::find(5)->regex) output is iPhone|iPod with print_f or "\x08iPhone\x08|\x08iPod\x08" with dd
First, you don't need json_decode(PhoneValidetionRules::find(5)->regex) to get regex value you can do PhoneValidetionRules::find(5)->regex the output will be "\biPhone\b|\biPod\b" to remove extra " you can do str_replace("\"", "", PhoneValidetionRules::find(5)->regex) and you will get \biPhone\b|\biPod\b".
Second, why \b converted to \x08 because this is the json_decode() behavior it converts some special characters to unicode and you have to avoid those special character in your string, you read more here json.org/json-en.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.