Giter VIP home page Giter VIP logo

weather_api's Introduction

Weather Api Project

  1. Для установки laravel проекта использовал консольную команду (при установленном composer)
    composer create-project laravel/laravel weather_api --prefer-dist
      Для развертывание этого проекта на другой машине следует склонировать файлы с этого репозитория:
    • прописать в консоли команду
      composer install;
    • переименовать .env.example в .env;
    • заменить путь и доступы к вашей базе данных;
  2. Для удаление ненужной в данном проекте frontend части использовал команду
    php artisan preset none
  3. Создал базу данных weather_api, прописал подключение к бд в файле .env
          
            DB_CONNECTION=mysql
            DB_HOST=127.0.0.1
            DB_PORT=3306
            DB_DATABASE=weather_db
            DB_USERNAME=root
            DB_PASSWORD=
          
        
  4. Создал модель Weather и миграцию для нее командой
    php artisan make:model Weather -m
  5. Заполнил поля в миграции таким образом:
          
            Schema::create('weathers', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->string('city');
                $table->integer('temperature');
                $table->integer('humidity');
                $table->integer('wind_speed');
                $table->timestamps();
            });
          
        
    и мигрировал командой
    php artisan migrate
  6. Добавил атрибут fillable для модели Weather
    
            class Weather extends Model
            {
                protected $fillable = ['city', 'temperature', 'humidity', 'wind_speed'];
            }
        
  7. Добавил два роута в api.php для получения всех данных о погоде
            
            Route::get('weather', function() {
                return Weather::all();
            });
            
        
    и для получения погоды по конкретному городу
    
            Route::get('weather/{city}', function($city) {
                return Weather::where('city', $city) -> first();
            });
        
  8. Создал контроллер командой
    
            php artisan make:controller WeatherController
        
  9. В контроллере прописал два метода index и show для получения всей погоды и по городу соответственно
    
            class WeatherController extends Controller
            {
                public function index()
                {
                    return Weather::all();
                }
                public function show($city)
                {
                    return Weather::where('city', $city) -> first();
                }
            }
        
  10. После этого убрал логику из роута в файле api.php
    
            Route::get('weather', 'WeatherController@index');
            Route::get('weather/{city}', 'WeatherController@show');
        

weather_api's People

Contributors

alexzakablukov avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.