如何从博客中获取用户名 Laravel 5.2
How to get username from a blog Laravel 5.2
我在 Laravel 5.2 中创建了博客。我无法显示与该博客关联的用户名。我的传单与 user_id 链接,但我无法获得创建的传单的实际用户名。
这是我与 Flyer 的用户关系:
/**
* One user can have many Travel Flyers.
*/
public function travelFlyers() {
return $this->hasMany(TravelFlyers::class);
}
这是我关联用户的 Flyer 模型:
/**
* A Travel Flyer belongs to a user.
*/
public function user() {
return $this->belongsTo('App/User', 'user_id');
}
下面是我展示传单的方法:
/**
* Show a Travel Flyer in detail.
* -- with title being the url
*/
public function show($title) {
//
$travelFlyer = TravelFlyers::TravelFlyerLocatedAt($title);
// return a view with the travel Flyer.
return view('travelflyers.show', compact('travelFlyer'));
}
这是我的 flyer_table:
public function up()
{
Schema::create('travel_flyers', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('title');
$table->string('excerpt');
$table->text('description');
$table->string('country');
$table->string('state');
$table->string('city');
$table->double('lat', 20, 10);
$table->double('lng', 20, 10);
$table->timestamps();
$table->timestamp('published_at');
});
}
我需要做这样的事情:
@foreach($travelFlyers as $flyer)
<div class="col-md-12">
<p><b>Username:</b> {{ $flyer->username }}</p>
</div>
@endforeach
This works fine, but I need username, not ID
@foreach($travelFlyers as $flyer)
<div class="col-md-12">
<p><b>Username:</b> {{ $flyer->user_id }}</p>
</div>
@endforeach
{{ $flyer->username }}
传单没有username
,user
有username
。但是flyer有一个user
,所以你需要用
{{ $flyer->user->username }}
我在 Laravel 5.2 中创建了博客。我无法显示与该博客关联的用户名。我的传单与 user_id 链接,但我无法获得创建的传单的实际用户名。
这是我与 Flyer 的用户关系:
/**
* One user can have many Travel Flyers.
*/
public function travelFlyers() {
return $this->hasMany(TravelFlyers::class);
}
这是我关联用户的 Flyer 模型:
/**
* A Travel Flyer belongs to a user.
*/
public function user() {
return $this->belongsTo('App/User', 'user_id');
}
下面是我展示传单的方法:
/**
* Show a Travel Flyer in detail.
* -- with title being the url
*/
public function show($title) {
//
$travelFlyer = TravelFlyers::TravelFlyerLocatedAt($title);
// return a view with the travel Flyer.
return view('travelflyers.show', compact('travelFlyer'));
}
这是我的 flyer_table:
public function up()
{
Schema::create('travel_flyers', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('title');
$table->string('excerpt');
$table->text('description');
$table->string('country');
$table->string('state');
$table->string('city');
$table->double('lat', 20, 10);
$table->double('lng', 20, 10);
$table->timestamps();
$table->timestamp('published_at');
});
}
我需要做这样的事情:
@foreach($travelFlyers as $flyer)
<div class="col-md-12">
<p><b>Username:</b> {{ $flyer->username }}</p>
</div>
@endforeach
This works fine, but I need username, not ID
@foreach($travelFlyers as $flyer)
<div class="col-md-12">
<p><b>Username:</b> {{ $flyer->user_id }}</p>
</div>
@endforeach
{{ $flyer->username }}
传单没有username
,user
有username
。但是flyer有一个user
,所以你需要用
{{ $flyer->user->username }}