Skip to content

UniversiteLimoges/GiphyApp

Repository files navigation

Gify

Introduction

Gify is an test project where we will test SPA architecture based on Laravel and Vue.

Through Gify, users can to meet each others according to some attributes :

  • Geolocalisation.
  • Tags (that represent their personals traits).
$ composer create-project --prefer-dist laravel/laravel Gify
$ cd Gify
$ composer require laravel/ui
$ php artisan ui vue --auth
$ npm install
$ npm run dev

Database

We use SQLite database support.

DB_CONNECTION=sqlite
#DB_HOST=127.0.0.1
#DB_PORT=3306
#DB_DATABASE=laravel
#DB_USERNAME=root
#DB_PASSWORD=
DB_FOREIGN_KEYS=true
$ touch database/database.sqlite
$ php artisan migrate

Need to modify webpack.mix.js file:

const mix = require('laravel-mix');
require('mix-tailwindcss');  // <=====

/*
 |--------------------------------------------------------------------------
 | Mix Asset Management
 |--------------------------------------------------------------------------
 |
 | Mix provides a clean, fluent API for defining some Webpack build steps
 | for your Laravel application. By default, we are compiling the Sass
 | file for the application as well as bundling up all the JS files.
 |
 */

mix.js('resources/js/app.js', 'public/js')
   .sass('resources/sass/app.scss', 'public/css').tailwind(); // <======

npm run dev


Models Structure

Tag

$table->enum('type', ['fav','trait'])->nullable();

Seeding

$ php make:seeder TagsSeeder $ composer dump-autoload $ php artisan db:seed

Eloquent Relationships

Gify app needs to some relationships between their tables <=> Models

Tag - User : Many To Many

App/User

public function tag()
{
    return $this->belongsToMany('App\Tag');
}

App/Tag

public function user()
{
	return $this->belongsToMany('App\User');
}

database/database.sqlite : tag_user : tag_id , user_id ;

Retrieve

$user = App\User::find(1);

foreach ($user->tag as $t) {
    //
}

Attaching / Detaching

$user = App\User::find(1);

$user->tag()->attach($tagId);
    //
}
$user->tag()->detach($tagId);

// Detach all roles from the user...
$user->tag()->detach();
}

User - Location : One To One

In App\User

public function location() 
{
    return $this->hasOne('App\Location');
}

In Location

public function user()
{
	return $this->belongsTo('App\User');
}

Geolocation

Used to get location information from IP visitor thanks to global $_SERVER['REMOTE_ADDR'] variable

  • New table locations
 public function up()
    {
        Schema::create('locations', function (Blueprint $table) {
            $table->id();
            $table->foreignId('user_id')->nullable();
            $table->string('ip')->nullable();
            $table->string('country')->nullable();
            $table->string('city')->nullable();
            $table->float('lat', 8, 3)->nullable();
            $table->float('lon', 8, 3)->nullable();
            $table->timestamps();
        });
    }
  • New Middleware 'geoIp'
namespace App\Http\Middleware;

use Closure;
use App\Location;
use App\User;
use Illuminate\Support\Facades\Auth;

class GeoIp
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        // Get current User
        $current_user = Auth::user();

        // Get instance of \Torann\GeoIP\Facades\GeoIP
        $actual_location = geoip()->getLocation($_SERVER['REMOTE_ADDR']);

        if( 
            $current_user->location
            //!empty($current_user->location_ip)
            //$current_user->has('location')
            ) {

            $location = $current_user->location;

            $location->user_id = $current_user->id;
            $location->ip = $_SERVER['REMOTE_ADDR'];
            $location->country = $actual_location->country;
            $location->city = $actual_location->city;
            $location->lat = $actual_location->lat;
            $location->lon = $actual_location->lon;

            $location->save();

        }else{

            $location = New Location();

            $location->user_id = $current_user->id;
            $location->ip = $_SERVER['REMOTE_ADDR'];
            $location->country = $actual_location->country;
            $location->city = $actual_location->city;
            $location->lat = $actual_location->lat;
            $location->lon = $actual_location->lon;

            $location->save();  
        }

        return $next($request);
    }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published