dongxuanchao1425 2018-02-27 12:25
浏览 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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信夜间被转走了1w对,当天手机剪切板里就出现了这个乱码,有铁子可以看看是啥吗可以
  • ¥50 跑通github上的代码 深度学习 pytorch
  • ¥50 求写,批处理调用分区助手分区脚本
  • ¥15 求购HI3519AV100开发板
  • ¥15 请问1553 RT怎么测试,没有BC有方法吗
  • ¥100 业务编程如何选择学习方向和内容?
  • ¥15 wamp3.3.5安装完成后图标正常显示绿色,鼠标左右键点击图标均无反应。求解决方法。
  • ¥15 鼠标点击的这条记录了什么?
  • ¥15 在写pid调速的程序时,电机始终维持最大速度
  • ¥15 【有偿】调用DXGI截图初始化失败,提示0xf 887a0004