Advertisement
deyanivanov966

Упражнение MVC Потреб. интерфейс.Запълване на БД със случайни записи.Страниране. 02.12.2021 (инструк

Feb 21st, 2022
974
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.53 KB | None | 0 0
  1. Стъпка 0. Подготовка
  2.  
  3. Използваме подход “Learning by Doing”.
  4.  
  5.  
  6. Стартираме Apache & MySQL & cmd
  7.  
  8. Ако XAMPP е инсталиран на D:, то първо сменяме устройството, а после директорията:
  9.  
  10. D:
  11.  
  12. cd \xampp\htdocs\laravel8
  13.  
  14. PATH=%PATH%;C:\xampp\php
  15.  
  16. PATH=%PATH%;D:\xampp\php
  17.  
  18.  
  19. Периодично архивираме работещ проект, с цел бързо да възстановим работа от определен момент нататък.
  20.  
  21.  
  22. Задача 1 - View - подобряване на потребителския интерфейс
  23. При добавяне:
  24.  
  25. 1. Цена - поле с бутони нагоре/надолу.  
  26.  
  27. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/number
  28.  
  29.  
  30. 2. Рейтинг - с плъзгач
  31.  
  32. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/range
  33.  
  34. https://getbootstrap.com/docs/5.0/forms/range/
  35.  
  36.  
  37. При разглеждане на списъка:
  38.  
  39. 3. Рейтингът да се показва със звездички
  40.  
  41. a) Server side - само Blade
  42.  
  43. https://laravel.com/docs/8.x/blade#loops
  44.  
  45.                         <td>
  46.  
  47. @for ($i = 0; $i < $game->rating; $i++)
  48.  
  49.     <img src="https://iheartcraftythings.com/wp-content/uploads/2021/05/How-to-draw-star-7.jpg" width="25px">
  50.  
  51. @endfor                        
  52.  
  53.                         </td>
  54.  
  55.  
  56.  
  57. b) Client side - само JS
  58.  
  59. <script>
  60.  
  61. function f(x) {
  62.  
  63.         for(i = 0; i < x; i++)
  64.  
  65.                 document.write('<img src="https://iheartcraftythings.com/wp-content/uploads/2021/05/How-to-draw-star-7.jpg" width="40px"> ');
  66.  
  67. }
  68.  
  69. </script>
  70.  
  71.  
  72.                         <td>
  73.  
  74. <script>f( {{$game->rating}} );</script>                                
  75.  
  76.                         </td>
  77.  
  78.  
  79.  
  80. Задача 2 - Model - Запълване на таблица с примерни данни
  81. За повече информация: https://laravel.com/docs/8.x/seeding
  82.  
  83. Отваряме:
  84.  
  85. \xampp\htdocs\laravel8\database\seeds\DatabaseSeeder.php
  86.  
  87. Добавяме:
  88.  
  89. use Illuminate\Database\Seeder;
  90.  
  91. use Faker\Factory as Faker;
  92.  
  93. .
  94.  
  95.     public function run()
  96.  
  97.     {
  98.  
  99.         $faker = Faker::create();
  100.  
  101.         foreach (range(1,100) as $index) {
  102.  
  103.             \DB::table('games')->insert([
  104.  
  105.                 'name' => $faker->catchPhrase,
  106.  
  107.                 'price' => rand(1, 100),
  108.  
  109.                 'rating' => rand(1, 5),
  110.  
  111.             ]);
  112.  
  113.         }
  114.  
  115.     }
  116.  
  117.  
  118. Информация за методите тук: https://github.com/fzaninotto/Faker
  119.  
  120. Добавяне на 100 генерирани записа:
  121.  
  122. php artisan db:seed
  123.  
  124. и на още 100:
  125.  
  126. php artisan db:seed
  127.  
  128.  
  129. Тестване:
  130.  
  131. http://127.0.0.1/laravel8/public/games
  132.  
  133. Задача 3 - Controller - Страниране
  134. Добавяне на страниране на списъка със записите.
  135.  
  136. За повече информация: https://laravel.com/docs/8.x/pagination
  137.  
  138. Отваряме:
  139.  
  140. \xampp\htdocs\laravel8\app\Http\Controllers\GameController.php
  141.  
  142. Заместваме:
  143.  
  144.     public function index()
  145.  
  146.     {
  147.  
  148.         $games = Game::all();
  149.  
  150.         $games = Game::paginate(15);
  151.  
  152.         return view('index', compact('games'));
  153.  
  154.     }
  155.  
  156.  
  157. Отваряме:
  158.  
  159. \xampp\htdocs\laravel8\resources\views\index.blade.php
  160.  
  161. Добавяме:
  162.  
  163.   </table>
  164.  
  165.   {{ $games->links() }}
  166.  
  167. <div>
  168.  
  169. @endsection
  170.  
  171.  
  172. Отваряме: (съгласно https://laravel.com/docs/8.x/pagination#using-bootstrap )
  173.  
  174. \xampp\htdocs\laravel8\app\Providers\AppServiceProvider.php
  175.  
  176. Добавяме:
  177.  
  178. use Illuminate\Support\ServiceProvider;
  179.  
  180. use Illuminate\Pagination\Paginator;
  181.  
  182. ..
  183.  
  184. public function boot()
  185.  
  186. {
  187.  
  188.     Paginator::useBootstrap();
  189.  
  190. }
  191.  
  192.  
  193.  
  194. Тестване:
  195.  
  196. http://127.0.0.1/laravel8/public/games
  197.  
  198.  
  199. КРАЙ
  200.  
  201.  
  202. Типове данни на полета в таблица от БД: https://laravel.com/docs/8.x/migrations#creating-columns
  203.  
  204.  
  205. Елементи на уеб форма:
  206.  
  207. Падащ списък: https://www.w3schools.com/html/html_form_elements.asp
  208.  
  209. <input type="checkbox">, <input type="radio"> и др. -
  210.  
  211. https://www.w3schools.com/html/html_form_input_types.asp
  212.  
  213.  
  214. Качване на файлове:
  215.  
  216. VIEW:
  217.  
  218. <form action="..." method="POST" enctype="multipart/form-data">
  219.  
  220. File: <input type="file" name="myFile">
  221.  
  222. КОНТРОЛЕР:
  223.  
  224. https://laravel.com/docs/8.x/requests#files
  225.  
  226. https://laravel.com/docs/8.x/filesystem
  227.  
  228.  
  229.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement