php - 如何加入 laravel infyom 生成器?

标签 php mysql laravel generator laravel-5.3

我的 Controller 是这样的:

<?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>

我的模型是从用户表中获取的。我想加入另一张 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 中实现关系或仅实现原始数据库查询连接表:

  1. 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 生成器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41113811/

相关文章:

php - 授予 Laravel 5 中新角色的权限

PHP查询打印特定的列选择

mysql - 如何使用 Ebean 和 mysql 获取超过 100000 行

php - 使用日期函数从数据库中提取记录

mysql - 事务中回滚后剩余的查询正在运行事件

php - 如何对 Laravel 5.3 数据库通知进行分页

php - 如果缺少 URL 段,则使用 Codeigniter

php - 带有月份列和多个字段行的mysql交叉表

laravel - 如何在 Laravel 5 中按语言环境切换 View ?

mysql - 在查询生成器中查询不同的行