0
クライアントの一覧と作成したそれぞれのユーザーを取得しようとしていますそれぞれのクライアント...助けてください。私は関係を成し遂げました私は何が欠けているのか分かりません...私はフレームワークを初めて学びました。App Models Clientsの定義されていない関係[ユーザー]を解決する方法クライアントとクライアントのそれぞれの作成者(ユーザー)を一覧表示しようとしています
ありがとうございます!以下は
私のクライアントモデルです:UPDATE
<?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');
}
}
?>
マイユーザーモデル:UPDATE
<?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');
}
}
?>
マイClientsController
<?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');
}
}
リカバリをやっていないので、関係にforeingキーを追加してみましょう:)このように:return $ this-> hasMany(Clients :: class、 'user_id'); 'モデル: 'return $ this-> belongsTo(Users :: class、 'user_id');' !! – Maraboc
まだ、私は同じエラーを取得... @ Maraboc –
誰かが助けることができますか? –