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 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法