在 laravel 如何在没有 where 条件的情况下获取同一数组中的连接 table 数据

In laravel how to get the join table data in same array without where condition

我已经用数据

创建了 employes 和 employes_detail 表格

i have created model for both of the table that is given below:

emloye model:

<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;
use App\Http\Model\EmployeDetail;
class Employe extends Model
{
    public function employes_detail()
    {
        return $this->hasOne(EmployeDetail::class);
    }
}

和雇员详细模型:

<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;

class EmployeDetail extends Model
{
   public function employe()
    {
        public function employe()
    {
        return $this->belongsTo(Employe::class);
    }
    }
}

在控制器中,我使用的是:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Model\Employe;
use App\Http\Model\EmployeDetail;
class EmployeController extends Controller
{
    public function index(Request $request)
    {
        $Employe=Employe::all();
        $convert=$Employe->toArray();
        echo "<pre>";print_r($convert);exit;
        //return view('employe.employe');
    }
}

it showing only employe table data how can i show the data for the employes_detail as well as .still i am not able to understand it on laravel documentation can anyone please help me related this. how can i get the all data from employes and employes_details table for all the records

但是当我在控制器中使用这段代码时:

public function index(Request $request)
    {
        $Employe=Employe::where('id',1)->first();
        //$convert=$Employe->toArray();
        echo "<pre>";print_r($Employe->employes_detail);exit;
        //return view('employe.employe');
    }

its shows me the employe_detail table data but i want both of the table data in a same array and i dont want to use where condition here.

模型中的函数 employes_detailemploye 只声明了模型之间的关系,但如果你想加载关系,你可以试试这个:

Employe::with('employes_detail')->get();

$employees = Employe::all(); $employees->load('employes_detail');

然后您可以像这样访问每个员工的关系属性:

foreach($employees as $employe) {
    $employe->employes_detail->id;
}

希望对您有所帮助。