简体   繁体   English

我一直在尝试使用 eloquent 从我的数据库中的两个不同表中检索数据

[英]i have been trying to retrive data from two different tables in my database using eloquent

employee table i have a fully running allLeave table edited allLeave project but i want to merge a leave management system with it so i want to retrieve the full name in the table "employee" and i also want to retrieve some data from a table "allLeave".员工表我有一个完全运行的allLeave 表编辑了 allLeave项目,但我想将休假管理系统与其合并,所以我想检索表“employee”中的全名,我还想从表“allLeave”中检索一些数据”。

in my controller在我的控制器中

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\leaveType;
use App\allLeave;
use App\leaveDepartment;

class LeavesController extends Controller
{
    public function getAllLeave()
    {
        $data = App\allLeave::find(1)->full_name;
        return view('leave/allLeave',["data"=>$data]);
    }
}

in my employee model在我的员工模型中

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * Class Personnel
 * @package App
 */
class Employee extends Audit
{
    public function leave()
    {
        return $this->belongsTo('App\allLeave');
    }
}

in my allLeave model在我的 allLeave 模型中

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class allLeave extends Model
{
    public function empolyees()
    {
        return $this->hasMany('App\Employee');
    }
}

This can be done ( Based on code you have shared ).这可以完成(基于您共享的代码)。 You should be doing something like你应该做类似的事情

$allLeave = App\allLeave::find(1);

# one-to-many, employees is a collection. ["emp_01_full_name", ....]
$data = $allLeave->employees->pluck('full_name')->values();

Update更新

/* Your view will have an array like
 $data = [
    ['leave_type' => 'casual', 'leave_days' => 2, 'full_name' => 'farooq']
    ...
 ]
*/
$data = $allLeave->pluck(['leave_type', 'leave_days', 'employees.full_name'])->values();

How to use it in your view如何在您的视图中使用它

return view('leave/allLeave', compact(['data']));

OR

return view('leave/allLeave', ['data' => $data]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用连接从两个表中检索数据 - retrive data from two tables using join 我正在尝试从数据库中提取数据,该数据将从数据库中的两个不同表中提取,错误在$ returns = mysql_query中 - I'm trying to pull data from database where the data will be pulled from two different tables in the db,error is in $returns = mysql_query 如何使用Eloquent从两个不同的表中检索数据 - How to use Eloquent to retrieve data from two different tables 如何使用 eloquent 从数据库中检索 created_at 数据 - How to retrive created_at data from database with eloquent 使用SELECT查询从数据库中的两个不同表中获取数据 - Using a SELECT query to get data from two different tables in database 使用Laravel雄辩的关系从不同的表中快速检索数据 - Quick retrieve data from different tables using Laravel eloquent relationships 在两个日期之间使用口才从数据库中检索数据 - Retrieve data from the database using Eloquent between two dates 我正在尝试查询 laravel 数据库中的两个表 - I am trying to query two tables in my laravel database 如何从具有不同格式的两个不同表中将数据输入数据库 - How to input data into a database from two different tables with different forms 查看男性用户发了多少帖子(使用两个表中的数据来计算数量) - Seeing how many posts have been made from male users (Using data from two tables to calculate number)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM