[英]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();
/* 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();
return view('leave/allLeave', compact(['data']));
OR
return view('leave/allLeave', ['data' => $data]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.