dongxuanchao1425 2018-02-27 12:25 采纳率: 0%
浏览 84
已采纳

Laravel Eloquent显示多个表格的结果

I am trying to create a basic logs program, each log entry is associated to a user using his id, I can't display the whole log entries on a table and use the relationship between the models to display the user name instead of his id in the table, any help is appreciated to explain the syntax and why is the relation not showing.

my controller:

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

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

log model:

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

user model:

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

the view: the field users-> is appearing empty with nothing in it.

@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>

and here is my logs table

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

what am i doing wrong?

EDIT:output of 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条回答 默认 最新

  • dqenv99518 2018-02-27 12:45
    关注

    You are trying to get property of $post like array but $post is a object. Change your foreach to:

    @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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?