【问题标题】:How to joining Table in Laravel 5.8如何在 Laravel 5.8 中加入表格
【发布时间】:2019-11-01 16:36:25
【问题描述】:

我有 2 个表 users 和 data 。

在用户表中有:

标识 |姓名 |电子邮件 |等等

在数据表中有:

标识 |用户 ID |无数据 |等等

我像这样创建模型数据

public function users()
{
    return $this->HasMany(\App\User::class ,'id');
}

我的控制器:

public function pns()
{
    $data = Data::get();

    //DB::table('data')
        //->join('users','data.user_id','=','users.id')
        //->where(['user' => 'something', 'otherThing' => 
         // 'otherThing'])
        //->get(); // i trying to join this table but still error 


    return view('admin.data',['data' => $data]);
}

现在我想在表格中显示视图数据:

在我的视图刀片上:

 <th>No </th>
 <th>id </th>
 <th>Name </th>
 <th>email </th>


  @php
  $no=0;
  @endphp
  @foreach ($data as $i)
  <tr>
   <td class=" ">{{ ++$no }}</td>
   <td class=" ">{{ $i->user_id}}</td>
   <td class=" ">{{ $i->users->name}}</td>
   <td class=" ">{{ $i->email}}</td>
  </tr>

这个user_id被显示了,但是这个'name'不能显示并且得到错误:

此集合实例上不存在属性 [名称]。

编辑了我的 dd($data)

#attributes: array:18 [▼
    "id" => 1
    "user_id" => 6
    "email" => 'q@gmail.com'
    //etc
    "created_at" => "2019-06-18 17:27:54"
    "updated_at" => "2019-06-18 17:27:54"
  ]

【问题讨论】:

标签: laravel


【解决方案1】:

假设,您的一个用户可以拥有多个数据。这是我的解决方案

对于用户模式

class User extends Authenticatable
{

  <YOUR OTHER CODES HERE>

  public function datas(){
     return $this->hasMany(Data::class,'user_id','id');
  }
}

对于Data.php [数据模式]

class Data extends Model{

 <YOUR OTHER CODE>

 public function userDetail(){
   return $this->belongsTo(User::class,'user_id','id');
 }
}

从您的控制器:

 $data = Data:with('userDetail')->get();
 echo '<pre>';
 print_r($data->toArray()); // This will give you an array of data with user details. 

【讨论】:

  • 你能告诉我你为什么在数据模态上使用 HasMany 吗?我真的不知道
  • 这是基于一个user 可以有多个datas 的假设。如果不是这样,那么它会改变。
【解决方案2】:

在你的用户模型中使用 hasOne

public function data()
{
return $this->hasOne('App\Data', 'user_id');
}

//控制器

return view('admin.data')->with($data);

【讨论】:

    【解决方案3】:

    你需要更新你在数据模型中的关系

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

    应该是 hasMany 而不是 HasManyuser_id 而不是 id

    然后使用这个:$data = Data::with('users')-&gt;get();

    【讨论】:

      【解决方案4】:

      使用此查询

      DB::table('data')
      ->join('users','data.user_id','=','users.id')
      ->where('user_id','=',1)
      // Here you can also add more wheres
      // But make sure where column exists in one of queried table i.e in 
      // users | data 
      ->select([
          'no',
          'user_id as id',
          'name',
          'email'   
      ])
      ->get(); // i trying to join this table but still error 
      

      【讨论】:

        【解决方案5】:

        这样做$data = Data::with('users')-&gt;get();

        【讨论】:

        • 还是错误。我使用这个 {{ $i->users->name}} 的观点
        • 执行dd($data) 并检查是否正在检索数据
        • $data = Data_pns::with('users')->get(); , 现在我 dd($data); .但未显示属性名称。我在我的线程上更新了我的 dd($data)
        猜你喜欢
        • 2021-06-14
        • 1970-01-01
        • 2020-03-08
        • 2013-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-07
        • 1970-01-01
        相关资源
        最近更新 更多