Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Laravel 5.8中连接表格_Laravel - Fatal编程技术网

如何在Laravel 5.8中连接表格

如何在Laravel 5.8中连接表格,laravel,Laravel,我有2个表用户和数据 在用户表中,用户有: id |姓名|电子邮件|等 在数据表中有: id |用户id |无数据|等 我像这样创建模型数据 public function users() { return $this->HasMany(\App\User::class ,'id'); } 我的控制器: public function pns() { $data = Data::get(); //DB::table('data') //->

我有2个表用户和数据

在用户表中,用户有:

id |姓名|电子邮件|等

在数据表中有:

id |用户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>

这样做
$data=data::with('users')->get()

使用此查询

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 

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

public function users()
{
    return $this->hasMany(\App\User::class ,'user_id');
}
它应该是hasManynothasManyuser\u idnotid


然后使用以下命令:
$data=data::with('users')->get()

在您的用户模型中使用hasOne

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

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

假设一个用户可以有多个数据。这是我的解决办法

对于用户模式

class User extends Authenticatable
{

  <YOUR OTHER CODES HERE>

  public function datas(){
     return $this->hasMany(Data::class,'user_id','id');
  }
}
类用户扩展可验证性
{
公共函数数据(){
返回$this->hasMany(数据::类,'user_id','id');
}
}
对于Data.php[数据模式]

class Data extends Model{

 <YOUR OTHER CODE>

 public function userDetail(){
   return $this->belongsTo(User::class,'user_id','id');
 }
}
类数据扩展模型{
公共函数userDetail(){
返回$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. 
$data=data:with('userDetail')->get();
回声';
打印($data->toArray());//这将为您提供一个包含用户详细信息的数据数组。

仍然存在错误。我的视图使用此{{$i->users->name}执行
dd($data)
并检查是否正在检索数据$data=data_pns::with('users')->get(),现在我dd($data)。但没有显示出房东的名字。我在我的线程上更新我的dd($data)你可以为你想要的字段添加别名,或者你可以通过使用有说服力的关系来简化它你能告诉我为什么在数据上使用HasMany吗?我真的不知道这是基于一个
用户可以拥有许多
数据的假设。如果不是这样的话,情况就会改变。
 $data = Data:with('userDetail')->get();
 echo '<pre>';
 print_r($data->toArray()); // This will give you an array of data with user details.