我的控制器是这样的:
<?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生成器源:
http://labs.infyom.com/laravelgenerator/
https://github.com/InfyOmLabs/adminlte-generator/tree/5.3
最佳答案
解决方案A:从infyom编辑生成的代码以实现关系,或者仅在laravel中实现原始数据库查询联接表:
Relationship。您可以创建类似
```
/**
* Get the user that owns the phone.
*/
public function user()
{
return $this->belongsTo('App\User', 'foreign_key', 'other_key');
}
有很多可能性:
定义关系
一对一
一对多
一对多(逆向)
多对多有许多
通过多态关系
多对多态关系
Query builder - joins
这样的东西可以使用
```
$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。
关于php - 如何加入Laravel Infyom Generator?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41113811/