通过外键检索数据

时间:2017-06-09 10:24:39

标签: mysql laravel-5.3

我的数据库用户和投诉中有两个表。

用户表:

id |名字|密码

投诉表

id | user_id |标题|体

user_id是外键。如何根据user_id?

从投诉表中检索标题和正文

这是我到目前为止,但我无法检索特定于user_id的数据

我的用户模型:

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'surname', 'regnumber', 'course', 'department', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
}

我的个人资料控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Complain;
use App\Feedback;
use App\Item;
use Illuminate\Support\Facades\Redirect;
use Session;
use Auth;

class ProfileController extends Controller
{
    public function profile($user_id){

        $user = User::whereId($user_id)->first();

        $complains = Complain::paginate(2);

        return view('user.profile', compact('user', 'complains'));
    }

}

我的观点:

@foreach($complains as $complain)
   <div>
       <h3 class="operator-complain-title">Title: </h3>
       <p>{{ $complain->title }}</p>
       <h3 class="operator-complain-title">Complain:</h3>
       <p>{{ $complain->body }}</p>
   </div>
   <hr>
@endforeach

有任何想法的人请分享。

2 个答案:

答案 0 :(得分:0)

您希望设置如此处所述的一对多关系:https://laravel.com/docs/5.4/eloquent-relationships#one-to-many

一旦你掌握了正确的方法,当你做$user->complains之类的事情时,Eloquent会找到关系。

class User extends Model
{
    /**
     * Get the complains for the User.
     */
    public function complains()
    {
        return $this->hasMany('App\Complain');
    }
}

class Complain extends Model
{
    /**
     * Get the User for the Complain.
     */
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

答案 1 :(得分:0)

这是我的用户表迁移:

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

这是我的抱怨表迁移:

<?php

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

class CreateComplainsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('complains', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->text('body');
            $table->timestamps();
        });
    }

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

这是我用来向抱怨表添加外键的迁移:

<?php

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

class AddForeignKeyToComplains extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('complains', function (Blueprint $table) {
            $table->integer('user_id')->after('id')->nullable();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('complains', function (Blueprint $table) {
            $table->dropColumn('user_id');
        });
    }
}