Asked  2 Years ago    Answers:  5   Viewed   270 times

I want to check if the model has been changed with isDirty method, but always returns false.

This is my code :

 if (!is_null($partnersData)) {
        foreach ($partnersData as $partnerData) {
            $partner = Partner::find($partnerData['partner_id']);
            $partner->update($partnerData);

            if($partner->isDirty()){
                dd('true');
            }
        }
    }

 Answers

1

$model->update() updates and saves the model. Therefore, $model->isDirty() equals false as the model has not been changed since the last executed query (which queries the database to save the model).

Try updating the model like this:

$partner = Partner::find($id);

foreach ($partnerData as $column => $value) {
    if ($column === 'id') continue;

    $partner->$column = $value;
}

if ($partner->isDirty()) {
    // should be dirty now
}

$partner->save(); // $partner will be not-dirty from here
Wednesday, August 3, 2022
1

The main reason upon your issue is that you are caching your configuration. When running php artisan config:cache you're storing your configuration in your cache, and the next time Laravel will boot up it won't read the .env file because it detects that the configuration has been stored in the cache. Environment file should be used only to setup configuration files and then to access the value you're looking for you should use ONLY the config method.

Let's assume that you have the file config/stripe.php that consists of this content:

<?php

return [
    'secret' => env('STRIPE_SECRET', '')
];

Once you run php artisan config:cache access this value using ONLY the syntax config('stripe.secret') through your application code. Every time you update your config files and your .env you need to run php artisan config:cache again.

Friday, December 23, 2022
 
2

Check the primary key of your users database. If you set your table's primary key other than id, you need to set it in your User model.

The Laravel Documentation states the following:

Eloquent will also assume that each table has a primary key column named id. You may define a protected $primaryKey property to override this convention.

For example, if you have a user_id column set as your primary key in your users database, you need to place the code below in the User model:

protected $primaryKey = 'user_id';

Sunday, October 16, 2022
 
gregl
 
3

This can be done in (at least) 2 ways.

Using pure Eloquent model logic:

class Buy extends Model
{
  public function getTotalPrice() {
    return $this->buyDetails->sum(function($buyDetail) {
      return $buyDetail->quantity * $buyDetail->price;
    });
  }
}

The only issue here is that it needs to fetch all buy details from the database but this is something you need to fetch anyway to display details in the view.

If you wanted to avoid fetching the relation from the database you could build the query manually:

class Buy extends Model
{
  public function getTotalPrice() {
    return $this->buyDetails()->sum(DB::raw('quantity * price'));
  }
}
Thursday, September 1, 2022
 
2

I got the problem solve turns out that my factory method in my test is using

factory(User::class)->make() 

it should be

factory(User::class)->create()
Friday, November 25, 2022
 
tletnes
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages