Laravel Eloquent отображает результаты из нескольких таблиц

Я пытаюсь создать базовую программу журналов, каждая запись журнала связана с пользователем, использующим его идентификатор, я не могу отобразить все записи журнала в таблице и использовать отношения между моделями для отображения имени пользователя вместо его идентификатора. в таблице, любая помощь приветствуется, чтобы объяснить синтаксис и почему связь не отображается.

мой контроллер:

public function index()
{
$dcmlogs = log::with('users')->get();

return view('dcmlog.index', compact('dcmlogs'));
}

модель журнала:

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

модель пользователя:

public function logs()
{
return $this->hasMany('App\Logs');
}

вид:
поле users-> выглядит пустым, в нем ничего нет.

@foreach($dcmlogs as $post)
<tr>
<td>{{$post['users->name']}}</td>
<td>{{$post['description']}}</td>
<td>{{$post['action']}}</td>
<td>{{$post['type']}}</td>
<td>{{$post['comment']}}</td>
<td>{{$post['created_at']}}</td>

и вот моя таблица журналов

$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');

Что я делаю неправильно?

РЕДАКТИРОВАТЬ: вывод dd (dcmlogs)

Collection {#220 ▼
#items: array:3 [▼
0 => log {#226 ▼
#table: "logs"#fillable: array:4 [▼
0 => "action"1 => "description"2 => "comment"3 => "type"]
#connection: "mysql"#primaryKey: "id"#keyType: "int"+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:8 [▶]
#original: array:8 [▼
"id" => 1
"type" => "volvo""action" => "okoko""description" => "okok""comment" => "kokok""user_id" => 1
"created_at" => "2018-02-27 07:57:19""updated_at" => "2018-02-27 07:57:19"]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: array:1 [▼
"users" => User {#231 ▼
#table: "users"#fillable: array:3 [▶]
#hidden: array:2 [▶]
#connection: "mysql"#primaryKey: "id"#keyType: "int"+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:7 [▼
"id" => 1
"name" => "alex""email" => "ijijij@gmail.com""password" => "$2y$10$fSk7n1JqNTp2bQL/iBfXfOIetXOfpPhLBhu/S3mwp3CeLJi""remember_token" => "lCx9FtWOHEGQmpP1bX9XtxFGdN3vCS6OAxkrO2AAei4ARtlrUa6KpmNG""created_at" => "2018-02-27 07:56:45""updated_at" => "2018-02-27 07:56:45"]

1

Решение

Вы пытаетесь получить собственность $post как массив, но $post это object, Измени свой foreach чтобы:

@foreach($dcmlogs as $post)
<tr>
<td>{{$post->users->name}}</td>
<td>{{$post->description}}</td>
<td>{{$post->action}}</td>
<td>{{$post->type}}</td>
<td>{{$post->comment}}</td>
<td>{{$post->created_at}}</td>
@endforeach
0

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector