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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。