dongsi7067 2017-09-14 03:11
浏览 16
已采纳

使用相同的验证函数php laravel验证所有对象值

enter image description here

Above is the result of dd() one of the object , right now what i did is

//$car is a object variable
$car->corporate_id = some_function($car->corporate_id);
$car->corporate_name = some_function($car->corporate_name);
$car->member_id = some_function($car->member_id);

Instead doing the above ways , how can i achieve something like

 $data = some_function($car);
//it will go through all $car properties and run the same validation

can someone tell me how to achieve this?

  • 写回答

2条回答 默认 最新

  • dsf4s5787 2017-12-13 07:35
    关注

    I found it , first foreach the object , for example $cars is object ,

    foreach($cars as $car)
    {
         $carr = $car->map(function ($item, $key)
                 {
    
                   //code logic goes here
                   // $key variable is the index key of the object
                   // $item variable is value of the object
                    $data = $item + 1;        
                   return $data ;
                 });
    
             //after this you can simply access the object attribute like usual
             $carr->car_number;
             $carr->car_name;
    
    
    }
    

    but this is solution is using laravel Collection and you also can turn an array into collection by simply call

      $collection = collect($anyArray); // make sure to include Illuminate\Support\Collection this class 
                                       //in order for to use the collection functionality
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?