2017-05-29 6 views
0

私のデータベースのデータを取得する際に問題があります。 私は2つのモデルのUser、Followerを持っています。ラーベル関係のエラー

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

    class Follower extends Model 

     { 
      public function mentor() 
      { 
       return $this->belongsTo('App\User','id','mentor_id'); 
      } 
      public function follower() 
      { 
       return $this->belongsTo('App\User','id','user_id'); 
      } 
     } 

<?php 

namespace App; 

use Illuminate\Contracts\Auth\Authenticatable; 
use Illuminate\Database\Eloquent\Model; 
use Illuminate\Support\Facades\App; 
use Illuminate\Support\Facades\Cache; 
use Illuminate\Support\Facades\DB; 
use App\Friend; 
use App\Follower; 

class User extends Model implements Authenticatable 
{ 
public function followers() 
    { 
     return $this->hasMany('App\Follower','mentor_id','id'); 
    } 

    public function follows() 
    { 
     return $this->hasMany('App\Follower','user_id','id'); 
    } 
} 

データベース '信者'

<?php 

use Illuminate\Support\Facades\Schema; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateFollowerTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('followers', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->integer('user_id'); 
      $table->integer('mentor_id'); 
      $table->rememberToken(); 
      $table->timestamps(); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::dropIfExists('followers'); 

    } 
} 

私は

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 
use App\Follower; 
use Illuminate\Support\Facades\Auth; 
use App\User; 
class FollowerController extends Controller 
{ 
    public function getFollows(Request $request) 
    { 
     $follows=Auth::user()->follows()->get(); 
     return view('follows',['follows' => $follows]); 
    } 
    public function getFollowers(Request $request) 
    { 
     $followers=Auth::user()->followers()->get(); 
     return view('followers',['followers' => $followers]); 
    } 
} 

そして、ここでは私が取得しています私のFollowerControllerを介してデータを取得していますこのようなエラー enter image description here

私はなぜbがわからないそれはSQLリクエストに 'users'というものを追加します。 私が助けてくれればとても楽しいです

+0

ユーザテーブルに 'mentor_id'(外部キー)がありません。最初に定義する –

+1

'スキーマ:: create( 'following.blade.php'、function(Blueprint $ table)'何? – Sandeesh

+0

ok失敗するとエラーが発生する – OrestKhomitskyi

答えて

0

あなたはこれを複雑にしています。あなたが必要とするのは、フォロワーの情報を保持するピボットテーブルだけです。

Schema::create('follows', function (Blueprint $table) { 
    $table->unsignedInteger('follower_id'); 
    $table->unsignedInteger('followed_id'); 
    $table->timestamps(); 

    $table->primary(['follower_id', 'followed_id']); 

    $table->foreign('follower_id') 
     ->references('id') 
     ->on('users') 
     ->onDelete('cascade'); 

    $table->foreign('followed_id') 
     ->references('id') 
     ->on('users') 
     ->onDelete('cascade'); 
}); 

Userモデルにこれを追加followsピボットテーブルを追加します。

public function following() 
{ 
    return $this->belongsToMany(User::class, 'follows', 'follower_id', 'followed_id')->withTimestamps(); 
} 

public function followers() 
{ 
    return $this->belongsToMany(User::class, 'follows', 'followed_id', 'follower_id')->withTimestamps(); 
} 

コントローラでこれを行います。

public function getFollows() 
{ 
    $follows = auth()->user()->following; 
    return view('follows', ['follows' => $follows]); 
} 

public function getFollowers() 
{ 
    $followers = auth()->user()->followers; 
    return view('followers', ['followers' => $followers]); 
} 

私のOSSプロジェクトの1つに実装されているフォロー機能のコードに基づいています。詳細はこちらをご覧ください。

https://github.com/gothinkster/laravel-realworld-example-app/blob/master/database/migrations/2017_04_27_200850_create_follows_table.php

https://github.com/gothinkster/laravel-realworld-example-app/blob/master/app/RealWorld/Follow/Followable.php

注:ピボットテーブルのためのモデルを定義する必要はありません。フォロワーとフォローの両方の情報をUserモデルの関係から取得しています。