kokleng's avatar

Laravel Inertia alway reload page

I use laravel inertia and vue js in my project. everything is working fine exclude for when i try to write file. when i write file it always reload pg. i already dd() to see where the place that make it happen. it is "File::put()" or "file_put_contents" .

if i use javascript fetch() or ajax for test it. it still make my page redirect infinitely. i think it have problem with middleware of inertia in kernel setup it in $middlewareGroups of "web" (i just guess) because this code is work fine in my other project (laravel only) i use ajax to update something in my file with laravel ( File::put() or file_put_contents in php).

here is my route: Route::get('/translate/kh', 'TranslateController@translateKh')->name('admin.translate.kh')->middleware('permission:translate_khmer,view');

here is my controller: public function updateNewWord(){ $file_path_en = resource_path('lang/en.json'); $dataEn = (array) json_decode(File::get($file_path_en));

    $file_path_kh = resource_path('lang/kh.json');
    $dataKh =  (array) json_decode(File::get($file_path_kh));

    $newDataKh = [];
    foreach($dataEn as $en_key => $en){
        if(array_key_exists($en_key, $dataKh)){
            $newDataKh[$en_key] = $dataKh[$en_key];
        } else {
            $newDataKh[$en_key] = '';
        }
    }

    $newDataKh = json_encode($newDataKh, JSON_UNESCAPED_UNICODE);
    return File::put($file_path_kh, $newDataKh);
}
public function translateKh(Request $r){
    if($this->ajaxRequest($r)){
        $file_path = resource_path('lang/kh.json');
        $data = json_decode(File::get($file_path));

        $dataTable = collect([]);
        foreach($data as $k => $d){
            $dataTable->push([
                'key' => $k,
                'word' => $d
            ]);
        }

        return DataTables::of($dataTable)->make(true);
    }
    if($this->updateNewWord()){
        return Inertia::render('backends/settings/translates/kh');
    };
}

please help me. thanks

0 likes
1 reply
kokleng's avatar
kokleng
OP
Best Answer
Level 1

Hello everyone, Now i solve it. The problem is when i run "npm run dev" and it is watch file project every second. so when i try to update something or try to write into file. it's know that file is change, then it will reload page. however we try to preventDefault via fetch or ajax or axios. but in production it work fine after i build file via "npm run build". thank you.

Please or to participate in this conversation.