2017-12-11 19 views
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'); 
    } 
} 
+0

다음과 같이 권장 사항을 수행하지 않았으므로 관계에 foreing 키를 추가하십시오. return $ this-> hasMany (Clients :: class, 'user_id'); ' 모델 :'return $ this-> belongsTo (Users :: class, 'user_id');'!! – Maraboc

+0

그래도 같은 오류가납니다 ... @ Maraboc –

+0

누군가 도울 수 있습니까? –

답변

0

몇 주 전에 @Joe & @ Maraboc 님의 의견을 보내 주셔서 감사합니다. 클라이언트를 클라이언트로 변경했는데 정상적으로 작동합니다.