如何获得非对象的属性?

时间:2019-09-28 00:07:32

标签: php html laravel

我的推荐系统运行良好,但是我想在html Ex:{$用户名}页面中放置推荐的名称,用户名或电子邮件地址。在我的控制器上,我当前正在使用“重定向”,但是如果我将{$ username}放回“ undefined variable”,但是如果将其更改为“ View”,{$ username}可以工作,但是注册是通过“ admin”完成的默认情况下。

我的控制器:

    <?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/{username}', 'RefController@index' );

已更新

我的RegisterController:

 protected function create(array $data) {
    if($data['gender']=='male'){

        $avatar_path = 'boy.png';
    }
    else
        { 
            $avatar_path = 'girl.png';
        }
        $referrer = User::find( Cookie::get( 'referrer_id' ) );

        return User::create([
            'name' => $data['name'],
            'username' => $data['username'],
            'avatar' => $avatar_path,
            'gender' => $data['gender'],
            'email' => $data['email'],
            'passaport' => $data['passaport'],
            'country' => $data['country'],
            'city' => $data['city'],
            'state' => $data['state'],
            'address' => $data['address'],
            'postcode' => $data['postcode'],
            'password' => Hash::make($data['password']),
            'referrer_id' => (is_null($referrer )) ? 1 : $referrer->id,
        ]);

已更新

我的迁移add_Ref_to_user_table:

    <?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' );
        });
    }
}

返回错误的代码是这样的:

{{ Auth::user()->name }}

已更新

@Caddy DZ将return view ('auth.register') -> with更改为

,我得到了帮助和耐心
return view ('/auth/register')

2 个答案:

答案 0 :(得分:2)

如果只有经过身份验证的用户才能访问此路由,请尝试向其添加身份验证中间件。

Route::get('/user/{username}', 'RefController@index' )->middleware('auth');

尝试将控制器中的代码更改为以下内容:

return ( is_null( $user ) )
        ? redirect( '/' )
        : view( 'auth.register', compact( 'referrer_id', 'user' ) );

auth.register视图中,尝试获取{{ $user->name }}而不是{{ Auth::user()->name }}

答案 1 :(得分:1)

由于这是一个引荐系统,因此用户应获得404,以便在网址中插入不存在的引荐

使用firstOrFail

public function index($username)
{
    $user = User::where('username', $username)->firstOrFail();
    return view('auth.register')->with([
        'referrer_id' => $user->id,
        'username' => $username
    ]);
}

然后在这样的视图中显示用户名

{{ $username }}

假设您的数据库表中确实有一个name列

Schema::create('users', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name'); // Must have this
    $table->string('username')->unique(); // Must have this unique
    $table->string('email')->unique();
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
});

更新

替换此代码

'referrer_id' => (is_null($referrer )) ? 1 : $referrer->id,

通过存储在Cookie中的实际整数

'referrer_id' => $referrer,