Я сделал блог в 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 для показа флаеров:
/**
* 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