我在获取数据库时遇到问题。 我有两个型号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');
}
}
数据库&#39;粉丝&#39;
<?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');
}
}
我通过我的FollowerController
获取数据<?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]);
}
}
在这里,我得到了这样的错误 enter image description here
我不知道为什么,但它在SQL请求中添加了&#39;用户。&#39; 如果你帮助我,我会非常愉快的
答案 0 :(得分:0)
你这很复杂。您只需要一个数据透视表来保存关注者信息。
添加follows
数据透视表
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
模型。
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项目中实现的以下功能的代码。您可以查看它以了解更多信息。
注意:您无需为数据透视表定义模型。您将从用户模型关系中获得关注者和以下信息。