Я делаю многоуровневую систему, и мы используем реферальную ссылку, но имя пользователя спонсора не отображается в html страницы регистрации, и для доступа необходимо выйти из текущего сеанса.
Моя миграция:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddReferrerToUsersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table)
{
$table->integer('referrer_id')->unsigned()->default(1)->after('id');
$table->foreign('referrer_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table)
{
$table->dropForeign(['referrer_id']);
$table->dropColumn( 'referrer_id' );
});
}
}
Мой контроллер: refController.php
<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
class RefController extends Controller
{
public function index( $username )
{
$user = User::where( 'username', $username )->first();
return ( is_null( $user ) )
? redirect( '/' )
: redirect( '/register' )->withCookie( cookie()->forever( 'referrer_id', $user->id ) );
}
}
Мои маршруты:
Route::get('/user', function () {
return redirect('/');
});
Route::get('/user/{username}', 'RefController@index' );
Мой HTML:
<?php if (isset($referrer_id->id)) { ?>
<p class="login-box-msg" style="margin-top: 20px; margin-bottom: 30px; color: #373737;">You have been nominated for:<br><b>{{$referrer_id->name. ' - '. $referrer_id->email}}</b></p>
<?php } ?>
Моя структура "referrer_id" находится внутри таблицы "users".