laravel simplexml load file databese curd

I want to save some of the data that comes with “simplexml_load_file” to the database, but it doesn’t save, I wonder where is the problem?

what i want to save
“US DOLLAR,EUR,POUND STERLING”

"message": "Object of class Illuminate\Support\Facades\Date could not be converted to string",

Route::post('/exchange_rates',[ToolsController::class, 'kurlar']);

Schema::create('exchanges', function (Blueprint $table) {
            $table->id();
            $table->boolean('buying_rate');
            $table->boolean('selling_rate');
            $table->date('date');
            $table->string('currency');
            $table->timestamps();
        })

public function kurlar()
    {
        $products = simplexml_load_file('https://www.tcmb.gov.tr/kurlar/today.xml');

        $pred = [0, 3, 4];
        print_r($products);


        foreach ($products as $product) {
            $exchange = new Exchange;
            $exchange->buying_rate = $product->ForexBuying;
            $exchange->selling_rate = $product->ForexSelling;
            $exchange->date = new Date();$product->attributes['Date'];
            $exchange->currency = $product->attributes['CurrencyCode'];
            $exchange->save();
        }
    }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

First, the error: Object of class Illuminate\Support\Facades\Date could not be converted to string means that you could not save a date attribute in model by passing a new Date() object in it, for simple Laravel allows you to just pass the date as a string to the required attribute.

After implementing and modifying your code I reached this working version from it:

 public function kurlar(){
    $products = simplexml_load_file('https://www.tcmb.gov.tr/kurlar/today.xml');

    $exchanges = [];

    foreach ($products as $product) {
        $exchange = new Exchange;
        $exchange->buying_rate = get_object_vars($product->ForexBuying)[0];
        $exchange->selling_rate = get_object_vars($product->ForexSelling)[0] ?? 0; //0 as default value when returning null from ForexSelling

        // '2021-09-19' as a default date because all the products returns null on data
        $exchange->date = '2021-09-19';
        $exchange->currency = get_object_vars($product->attributes()['CurrencyCode'])[0];
        
        if(in_array($exchange->currency, ['USD', 'EUR', 'GBP'])){
            $exchange->save();
            array_push($exchanges, $exchange);
        }


    }

    return $exchanges;
}

Or, you could just check the indexes by using for loop:
for ($i=0; $i < count($products->Currency) ; $i++) { 
    $product = $products->Currency[$i];
    # code...
    $exchange = new Exchange;
    $exchange->buying_rate = get_object_vars($product->ForexBuying)[0];
    $exchange->selling_rate = get_object_vars($product->ForexSelling)[0] ?? 0; //0 as default value when returning null from ForexSelling

    // '2021-09-19' as a default date because all the products returns null on data
    $exchange->date = '2021-09-19';
    $exchange->currency = get_object_vars($product->attributes()['CurrencyCode'])[0];
    
    if(in_array($i, [0, 3, 4])){
        $exchange->save();
        array_push($exchanges, $exchange);
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x