我的控制器是这样的:
<?php
namespace App\Http\Controllers;
...
class UserController extends AppBaseController
{
/** @var UserRepository */
private $userRepository;
public function __construct(UserRepository $userRepo)
{
$this->userRepository = $userRepo;
}
public function index(Request $request)
{
$this->userRepository->pushCriteria(new RequestCriteria($request));
$users = $this->userRepository->all();
return view('users.index')
->with('users', $users);
}
}
..
我的存储库是这样的:
<?php
namespace App\Repositories;
use App\Models\User;
use InfyOm\Generator\Common\BaseRepository;
class UserRepository extends BaseRepository
{
protected $fieldSearchable = [
'username',
'email',
'password',
'kdkotama',
'kdsatker',
'nama_lengkap',
'telp',
'level',
'kdtingkat'
];
public function model()
{
return User::class;
}
}
我的模型是这样的:
<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
public $table = 'users';
protected $dates = ['deleted_at'];
public $fillable = [
'username',
'email',
'password',
'kdkotama',
'kdsatker',
'nama_lengkap',
'telp',
'level',
'kdtingkat'
];
protected $casts = [
'username' => 'string',
'email' => 'string',
'password' => 'string',
'kdkotama' => 'string',
'kdsatker' => 'string',
'nama_lengkap' => 'string',
'telp' => 'string',
'level' => 'string',
'kdtingkat' => 'string'
];
}
我的观点是这样的:
@extends('layouts.app')
@section('content')
<section class="content-header">
<h1 class="pull-left">Users</h1>
<h1 class="pull-right">
<a class="btn btn-primary pull-right" style="margin-top: -10px;margin-bottom: 5px" href="{!! route('users.create') !!}">Add New</a>
</h1>
</section>
<div class="content">
<div class="clearfix"></div>
@include('flash::message')
<div class="clearfix"></div>
<div class="box box-primary">
<div class="box-body">
@include('users.table')
</div>
</div>
</div>
@endsection
<table class="table table-responsive" id="users-table">
<thead>
<th>No</th>
<th>Username</th>
<th>Email</th>
<th>Kotama</th>
<th>Satker</th>
<th>Nama Lengkap</th>
<th>Telp / No HP</th>
<th>Level</th>
<th>Tingkat</th>
<th colspan="3">Action</th>
</thead>
<tbody>
@foreach($users as $key => $user)
<tr>
<td>{!! ++$key !!}</td>
<td>{!! $user->username !!}</td>
<td>{!! $user->email !!}</td>
<td>{!! $user->kdkotama !!}</td>
<td>{!! $user->kdsatker !!}</td>
<td>{!! $user->nama_lengkap !!}</td>
<td>{!! $user->telp !!}</td>
<td>{!! $user->level !!}</td>
<td>{!! $user->kdtingkat !!}</td>
<td>
{!! Form::open(['route' => ['users.destroy', $user->id], 'method' => 'delete']) !!}
<div class='btn-group'>
<a href="{!! route('users.edit', [$user->id]) !!}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-edit"></i></a>
{!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!}
</div>
{!! Form::close() !!}
</td>
</tr>
@endforeach
</tbody>
</table>
我的模型取自表用户。我想加入另一张桌子。 kdkotama字段和kdsatker字段如:kotama代码和satker代码。我想加入表t_kotams和表t_satkers来获取kotama名称和satker名称。
使用infyom laravel发生器,我很困惑,如何实施。是否有人可以帮助我?
Laravel infyom generator source:
答案 0 :(得分:0)
解决方案A :从infyom编辑生成的代码以实现关系,或者只是在laravel中编辑原始数据库查询连接表:
```
/**
* Get the user that owns the phone.
*/
public function user()
{
return $this->belongsTo('App\User', 'foreign_key', 'other_key');
}
有很多可能性: 定义关系
多对多的多态关系
```
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();
解决方案B :使用fields.json在infyom中生成模型。按照文档here定义* .json中的关系。
注意:infyom是一个源生成器,一切都基于模型,如果你的其他表没有模型,则采用解决方案A.