0
public function changeData(Request $request)
{
     $product = Product::where('_id', '=', $request->product['_id'])->first()->toArray();

        //result product : 
        // array(4) {
        //     ["_id"]=>
        //     string(24) "5fffc00df116005c724859f2"
        //     ["name"]=>
        //     string(6) "iphone"
        //     ["slug"]=>
        //     string(14) "product-iphone"
        //     ["status"]=>
        //     string(14) "1"
        // }


        // result $request->product :
        // array(4) {
        //     ["_id"]=>
        //     string(24) "5fffc00df116005c724859f2"
        //     ["name"]=>
        //     string(7) "samsung"
        //     ["slug"]=>
        //     string(15) "product-samsung"
        //     ["status"]=>
        //     string(14) "1"
        // }

        $diff = array_diff($request->product, $product);
        var_dump($diff);
}

ERROR: 'Array to string conversion'

If I use the :

$diff = array_diff(array_map('serialize',$request->product), array_map('serialize',$product));

Then it will return the following result:

 array(2) {
        ["name"]=>
        string(15) "s:6:"samsung";"
        ["slug"]=>
        string(25) "s:15:"product-samsung";"
      }

The results above are correct for what I need. But it shows "s:6: and "s:15: inside the result. Is there a way for these two to not appear. I just want to get the correct result eg name is samsung. Thank you.

3
  • 1
    Do you want to check which fields were updated? Commented Dec 27, 2021 at 11:01
  • @mohammadmahdibaleghsefat Yes, i want that Commented Dec 27, 2021 at 11:04
  • are u sure that both $request->product and $product are array ? maybe one of them is object ? specially $request->product . use $diff = array_diff(((array)$request->product), ((array)$product )); and check for error ? Commented Dec 27, 2021 at 11:22

2 Answers 2

1

try this

$diff = array_diff(array_map('serialize', $request->product), array_map('serialize', $product));

$multidimensional_diff = array_map('unserialize', $diff);

print_r($multidimensional_diff);
Sign up to request clarification or add additional context in comments.

2 Comments

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
This is the answer I was expecting.Thank you for your help with my work
0

If you want to check which fields were updated, you can use isDirsty method. For example, you want to check if the user has changed the email, send an email verification:

if ($user->isDirty('email'))
{
    // Do something
}

Laravel document

1 Comment

If you use this method, for example, if there are 10 fields, check if 10 times or not

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.