简体   繁体   中英

How to join in laravel infyom generator?

My controller is like this :

<?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);
    }
}

..

My repository is like this :

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

My model is like this :

<?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'
    ];
}

My view is like this :

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

My model was taken from the table users. I want to join with another table. kdkotama field and kdsatker field such : kotama code and satker code. I would like to join with table t_kotams and table t_satkers to get kotama name and satker name.

Using infyom laravel generator, I was so confused, how to implement. is there anyone who can help me?

Laravel infyom generator source :

http://labs.infyom.com/laravelgenerator/

https://github.com/InfyOmLabs/adminlte-generator/tree/5.3

Solution A : Edit generated code from infyom to implement relationship or just raw db query join tables in laravel:

  1. Relationship . You can create something like

```

/**
 * Get the user that owns the phone.
 */
public function user()
{
    return $this->belongsTo('App\User', 'foreign_key', 'other_key');
}

There are many possibilities : Defining Relationships

  • One To One
  • One To Many
  • One To Many (Inverse)
  • Many To Many Has Many
  • Through Polymorphic Relations
  • Many To Many Polymorphic Relations

    1. Query builder - joins something like this can be used

```

$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();

Solution B : Use fields.json to generate your Model in infyom. Define relationships inside your *.json as documented here .

Note : infyom is a source generator, and everything is based on models, if your other tables don't have models, adopt Solution A.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM