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中通过id选择不同表上的关系数据_Laravel_Model_Controller_Relation - Fatal编程技术网

如何在laravel中通过id选择不同表上的关系数据

如何在laravel中通过id选择不同表上的关系数据,laravel,model,controller,relation,Laravel,Model,Controller,Relation,我有3个表,名称是:用户、机器、维护。这个模式是这样的 *使用者 id |姓名|电子邮件|密码|创建|更新|在 1 |约翰|A@mail|*******等 *机器 id |名称|机器|创建|更新|在 1 |冲击|等 *维护 id |机器| id |用户| id |问题|状态|已创建|| 更新地址 1.......................1。。。。。。。1……|等等 现在,我想通过id显示数据“name”(用户表)和name_machine(机器表),在表“Maintance”上显示数据

我有3个表,名称是:用户、机器、维护。这个模式是这样的

*使用者

id |姓名|电子邮件|密码|创建|更新|在

1 |约翰|A@mail|*******等

*机器

id |名称|机器|创建|更新|在

1 |冲击|等

*维护

id |机器| id |用户| id |问题|状态|已创建|| 更新地址

1.......................1。。。。。。。1……|等等

现在,我想通过id显示数据“name”(用户表)和name_machine(机器表),在表“Maintance”上显示数据

这是我的控制器和我的视图

 public function show($id)
{
    $maintance = Maintance::all();
    return view('users.view',['maintance' => $maintance]);
}
我的看法

<table class="table table-hover">
              <tbody><tr>
                <th>Machine</th>
                <th>Question</th>
                <th>User Input</th>
                <th>Action</th>
                <th>Tanggal</th>
              </tr>
              @foreach ($maintance as $i)
              <tr>
                <td>{{ $i->machine_id}} </td>// i want to show name machine here
                <td>{{ $i->Question}}</td>
                <td>{{ $i->user_id}}</td> // name users to 
                <td><span class="label label-primary">Lihat Data</span></td>
                <td>{{ $i->created_at}}</td>
              </tr>
              @endforeach
            </tbody></table>

机器
问题:
用户输入
行动
唐加尔
@foreach(维护为$i)
{{$i->machine\u id}}//我想在这里显示名称machine
{{$i->问题}
{{$i->user_id}}//指定要访问的用户
Lihat数据
{{$i->created_at}
@endforeach

此视图数据没有错误,但我不知道如何从不同的表中显示此数据。有人能帮忙吗?

假设您的
维护模式如下

public function user(){
  return $this->belongsTo(User::class);
}
public function machine(){
  return $this->belongsTo(Machine::class);
}
假设您的模型具有上述关系

然后执行以下操作以获取您的数据

$maintance = Maintance::with(['user','machine'])->get();
在你看来

<table class="table table-hover">
          <tbody><tr>
            <th>Machine</th>
            <th>Question</th>
            <th>User Input</th>
            <th>Action</th>
            <th>Tanggal</th>
          </tr>
          @foreach ($maintance as $i)
          <tr>
            <td>{{ $i->machine->machine_name}} </td>// i want to show name machine here
            <td>{{ $i->Question}}</td>
            <td>{{ $i->user->name}}</td> // name users to 
            <td><span class="label label-primary">Lihat Data</span></td>
            <td>{{ $i->created_at}}</td>
          </tr>
          @endforeach
        </tbody></table>

机器
问题:
用户输入
行动
唐加尔
@foreach(维护为$i)
{{$i->machine->machine\u name}}//我想在这里显示名称machine
{{$i->问题}
{{$i->user->name}}//指定用户
Lihat数据
{{$i->created_at}
@endforeach

先生,您能告诉/解释一下为什么在模型上使用belongsTo吗?我使用belongsTo在您的表之间建立关系。查看您的表
maintance
。它有两个外键
user\u id
machine\u id
,外键使用
belongsTo
/
belongstomy
关系引用其父表。这里的
user
表和
machine
表将
id
作为主键,并假定它是
自动递增的。显然,
maintance
表中的一个条目在每个父表中都有一个条目。