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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度