如何连接这两个表以获得结果作为 api 资源?
How to join these two tables to get results as api resource?
我有两个table,一个是主客户端table,一个是子客户端table,子客户端是主客户端的一个客户端这是主客户端的主键在子客户端 table 我如何加入这两个 tables 并在控制器中获取输出以将资源作为 JSON 传递给 API ??
这是我的主要客户端模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Clients extends Model
{
//
}
这是我的主客户端控制器:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Clients;
use App\Http\Resources\Client as ClientResource;
// use Illuminate\Http\Response;
class Clients_controller extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//get clients
$clients = Clients::paginate(15);
//Return collection of clients as a resource
return ClientResource::collection($clients);
}
}
这是我的子客户端模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Sub_clients extends Model
{
//
}
这是我的子客户端控制器:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Sub_clients;
use App\Http\Resources\Sub_client as SubclientResource;
class Sub_client extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//get Sub_clients
$subclients = Sub_clients::paginate(15);
//Return collection of sub clients as a resource
return SubclientResource::collection($subclients);
}
}
谁能帮忙,因为我是 laravel
的新手
我使用查询生成器加入
$client = DB::table('clients')
->join('sub_clients', 'clients.c_id', '=', 'sub_clients.c_id')
->select('clients.*', 'sub_clients.password', 'sub_clients.username as name')
->where('sub_clients.c_id','=',$c_id)
->get();
我有两个table,一个是主客户端table,一个是子客户端table,子客户端是主客户端的一个客户端这是主客户端的主键在子客户端 table 我如何加入这两个 tables 并在控制器中获取输出以将资源作为 JSON 传递给 API ??
这是我的主要客户端模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Clients extends Model
{
//
}
这是我的主客户端控制器:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Clients;
use App\Http\Resources\Client as ClientResource;
// use Illuminate\Http\Response;
class Clients_controller extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//get clients
$clients = Clients::paginate(15);
//Return collection of clients as a resource
return ClientResource::collection($clients);
}
}
这是我的子客户端模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Sub_clients extends Model
{
//
}
这是我的子客户端控制器:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Sub_clients;
use App\Http\Resources\Sub_client as SubclientResource;
class Sub_client extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//get Sub_clients
$subclients = Sub_clients::paginate(15);
//Return collection of sub clients as a resource
return SubclientResource::collection($subclients);
}
}
谁能帮忙,因为我是 laravel
的新手我使用查询生成器加入
$client = DB::table('clients')
->join('sub_clients', 'clients.c_id', '=', 'sub_clients.c_id')
->select('clients.*', 'sub_clients.password', 'sub_clients.username as name')
->where('sub_clients.c_id','=',$c_id)
->get();