В этой записи вы узнаете как прочитать XML файл в Laravel. Давайте посмотрим на приведенный ниже пример преобразования XML файла в php массив laravel.
Этот метод чтения легко использовать в версиях laravel 6, laravel 7, laravel 8, laravel 9 и laravel 10.
Вы можете создать или загрузить простой XML файл, как показано ниже.

Давайте добавим метод чтения xml файла. Код представлен ниже:
<?php
  
namespace App\Http\Controllers;
   
use Illuminate\Http\Request;
  
class XMLController extends Controller
{
    /**
     *
     * @return response()
     */
    public function index()
    {
        $xmlString = file_get_contents(public_path('sample.xml'));
        $xmlObject = simplexml_load_string($xmlString);
                   
        $json = json_encode($xmlObject);
        $phpArray = json_decode($json, true); 
   
        dd($phpArray);
    }
}Здесь мы читаем содержимое файле в строку и сохраняем ее в переменную $xmlString с помощью функций file_get_contents. Далее используя функцию simplexml_load_string мы переводим строку  в объект XML и сохраняем ее в переменную $xmlObject. И на заключительных этапе нужно перевести объект XML в json с помощью json_encode, и затем с помощью json_decode переведем строку json в массив.
Вывод этого метода показан ниже:
Array
(
    [food] => Array
        (
            [0] => Array
                (
                    [name] => Belgian Waffles
                    [price] => $5.95
                    [description] => Two of our famous Belgian Waffles with plenty of real maple syrup
                    [calories] => 650
                )
            [1] => Array
                (
                    [name] => Strawberry Belgian Waffles
                    [price] => $7.95
                    [description] => Light Belgian waffles covered with strawberries and whipped cream
                    [calories] => 900
                )
            [2] => Array
                (
                    [name] => Berry-Berry Belgian Waffles
                    [price] => $8.95
                    [description] => Light Belgian waffles covered with an assortment of fresh berries and whipped cream
                    [calories] => 900
                )
            [3] => Array
                (
                    [name] => French Toast
                    [price] => $4.50
                    [description] => Thick slices made from our homemade sourdough bread
                    [calories] => 600
                )
            [4] => Array
                (
                    [name] => Homestyle Breakfast
                    [price] => $6.95
                    [description] => Two eggs, bacon or sausage, toast, and our ever-popular hash browns
                    [calories] => 950
                )
        )
)