0

私は自分のウェブサイトのようなシステムを作成していました。これで、1人のユーザーが投稿に対して1回だけいいねできるようにしたかったのです。そして、投稿は多くのユーザーに好かれます。また、多くのユーザーが多くの投稿に「いいね!」をすることができます。

したがって、私が正しく推測すると、それは多対多の関係です。

このコンテキストでは、次のテーブルを作成します

... ユーザー テーブル:

id

name

.... 投稿テーブル:

id

post

...post_likes テーブル

ID

user id
poost_id

今、私は次のモデルを持っています

ユーザー:

class User extends SentryUserModel {
    public function post_likes()
    {
        return $this->has_many('Post_like', 'id');
    }
}

役職 :

class Post extends Eloquent {

    public function post_likes()
    {
        return $this->has_many('Post_like', 'id');
    }
}

post_like :

class Post_like extends Eloquent {

    public function posts()
    {
        return $this->belongs_to('Post', 'post_id');
    }
    public function users()
    {
        return $this->belongs_to('User', 'user_id');
    }

}

データベースに挿入しようとすると(post_likesテーブル用)、というエラーが発生します

Illuminate \ Database \ Eloquent \ MassAssignmentException
user_id

また、次のようなデータベースに挿入する方法があることを知りたい

$user->like()->save($user); ?????

前もって感謝します。ハッピーコーディング。\m/

4

2 に答える 2

2

基本的な問題から始めます。まず、すべてのテーブルが小文字であることを確認したい場合があります (スネーク ケースも同様です)。必須ではありませんが、最終的には Laravel で期待される方法であるため、生活が楽になります。それを維持するために。また、クラス名と同様に、データベーステーブルは通常単数形であるため、users ではなく user です。

次に、$user->post_likes()->save($debate); で挿入できます。ユーザー クラスの post_likes メソッドが has_many を返すためです。

第三に、Post_like クラスの設計が少しずれています。次のようにしたほうがよいでしょう。

class PostLike extends Eloquent { // note that PostLikes is a more standard naming for a class, they should ideally be camel case names but with all capitals for words

    protected $table = 'post_like'; // specifies the table the model uses

    public function post() // this should be singular, the naming of a belngs_to method is important as Laravel will do some of the work for you if let it
    {
        return $this->belongs_to('Post'); // by naming the method 'post' you no longer need to specify the id, Laravel will automatically know from the method name and just adding '_id' to it.
    }
    public function users()
    {
        return $this->belongs_to('User');
    }

}

第 4 に、他のクラスは次のように優れている可能性があります。

class Post extends Eloquent {

    public function post_likes()
    {
        return $this->has_many('PostLike'); // doesn't require you to specify an id at all
    }
}

大量割り当てエラーが発生した理由を正確に伝えることはできません。投稿が少し文字化けしており、実際に例外を引き起こすコードが含まれているようには見えませんか? 一度に複数のデータベース行の挿入を試みているが、ここのような PostLike の入力可能な配列を定義していないという気がします: http://four.laravel.com/docs/eloquent #質量割り当て

于 2013-11-05T08:15:54.187 に答える
0

Laravel 5によると:

ユーザー モデル:

namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model {
    public function post_likes()
    {
        return $this->hasMany('App\PostLike');
    }
}

ポストモデル:

namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model {

    public function post_likes()
    {
        return $this->hasMany('App\PostLike');
    }
}

PostLike モデル:

namespace App;
use Illuminate\Database\Eloquent\Model;
class PostLike  extends Model {

    protected $table = 'post_like';

    public function posts()
    {
        return $this->belongsTo('App\Post');
    }
    public function users()
    {
        return $this->belongsTo('App\User');
    }

}

post_like データを保存する場合は、次のようにします。

$inputs['post_id'] = 1;
$inputs['user_id'] = 4;

$post_like = PostLike::create($inputs);

お役に立てれば!!

于 2016-11-03T06:28:27.910 に答える