helper.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shehbaz
  5. * Date: 1/21/19
  6. * Time: 12:19 PM
  7. */
  8. use Illuminate\Support\Facades\Validator;
  9. if (!function_exists("helper_test")) {
  10. function helper_test()
  11. {
  12. echo "it is working";
  13. }
  14. }
  15. if (!function_exists("populate_breadcumb")) {
  16. /**
  17. * popular data to layouts.admin.app when send from controller
  18. *
  19. *<h1> controller example </h1>
  20. * <pre>
  21. * $data = [
  22. * ["name" => "Dashboard1", "url" => route("admin.dashboard")],
  23. * ["name" => "Products1", "url" => request()->fullUrl()]
  24. * ];
  25. *
  26. * populate_breadcumb($data)
  27. * </pre>
  28. *
  29. * @param $data
  30. * @return void
  31. */
  32. function populate_breadcumb($data)
  33. {
  34. $validated = validate_breadcumb($data);
  35. if ($validated["valid"] === true) {
  36. view()->composer([
  37. "layouts.admin.app"
  38. ], function ($view) use ($data) {
  39. $view->with(
  40. [
  41. "breadcumbs" => $data
  42. ]
  43. );
  44. });
  45. }
  46. }
  47. }
  48. if (!function_exists('validate_breadcumb')) {
  49. /**
  50. * validate breadcumb data
  51. * @param $data
  52. * @return array
  53. */
  54. function validate_breadcumb($data)
  55. {
  56. $validated = false;
  57. $errors = [];
  58. foreach ($data as $key => $item) {
  59. $messages = [
  60. 'required' => "The :attribute field is required at index: $key.",
  61. "url" => "The :attribute format is invalid at index: $key"
  62. ];
  63. $validator = Validator::make($item, [
  64. 'name' => 'required',
  65. 'url' => "required|url",
  66. // "icon" => ""
  67. ], $messages);
  68. if ($validator->fails()) {
  69. $validated = false;
  70. $errors[] = $validator->errors();
  71. } else {
  72. $validated = true;
  73. }
  74. }
  75. return ["errors" => $errors, "valid" => $validated];
  76. }
  77. }