Я пытаюсь получить список клиентов и их соответствующих пользователей, которые создали соответствующий клиент... Пожалуйста, помогите. Я делал отношения, я не знаю, чего не хватает... Я новичок в laravel framework.
Заранее спасибо!
Ниже приведена модель моих клиентов: ОБНОВЛЕНИЕ
<?php
namespace app\Models;
use App\Models\Users;
use Illuminate\Database\Eloquent\Model;
class Clients extends Model {
protected $table = 'Clients';
protected $fillable = ['name','email'];
protected $hidden = [];
public function users()
{
return $this->belongsTo(Users::class,'user_id');
}
}
?>
Моя модель пользователей: ОБНОВЛЕНИЕ
<?php
namespace App\Models;
use App\Models\Clients;
use Illuminate\Database\Eloquent\Model;
use Illminate\Database\Auth\Authenticatable;
class Users extends Model implement Authenticatable{
protected $table = 'Users';
protected $fillable = ['name','email','password'];
protected $hidden = [];
public function clients()
{
return $this->hasMany(Clients::class, 'user_id');
}
}
?>
Мои клиентыКонтроллер
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Clients;
use App\Models\Users;
class ClientsController extends Controller
{
//
public function index(){
$clients = Clients::with('users')->get();
return response()->json([
'msg'=>'successfully connected API to clients',
'client'=>$clients->toArray()
],200
);
моя миграция клиентов:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateClientTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
Schema::create('clients', function (Blueprint $table)
{
$table->increments('id');
$table->string('email');
$table->string('name');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
Schema::create('clients');
}
}
Я решил это несколько недель назад @Joe & @Maraboc благодарит за ваши входы... Я изменил клиентов клиенту, тогда он отлично работает.