简体   繁体   中英

Cant store simple array in laravel database

I want to store user ID's in a column in my User row, as people can "follow" other people.

So if I am a user, I can follow multiple users. This i want to store in a super simple format, like [1, 5, 6] in the database.

The Following column is a string format:

$table->string('following');

I am trying to do this by running the following:

$user = Auth::user(); 
$data = $request->all(); 
$follow = $data['follow']; 

if(!empty($user->following)) { 
    $user->following = array_merge($user->following, array($follow)); 
} else { 
    $user->following = array($follow); 
}

However it troughs a Parameter mismatch, pattern is a string while replacement is an array

I have done this exactly the same way on another system also build upon laravel, and there everything works like a charm, so I simply dont get why this isn't working. In that database to the other system, the data is stored like mentioned earlier.

I dont want to serialize alot, as this has to be simple, so I can do a

$following = User::whereIn('id', $user->following)->get();

foreach($follwing as $follow) {
...
}

I dont think this is the best way to do it, however this is the only way I can wrap my head around with my skills. Other suggestions are mostly welcome!

This not the Laravel way to store data. You should create a new table to store the follower's data.

This can be achieved by creating a many-to-many relationship between user table itself.

Create a followers_following table with columns as:

id | follower_id | following_id

Create two relation function in User model as:

public function followers()
{
    return $this->belongsToMany('App\User', 'followers_following', 'following_id', 'follower_id');
}

public function following()
{
    return $this->belongsToMany('App\User', 'followers_following', 'follower_id', 'following_id');
}

Then you can query the user as:

$user = User::find(1);

$user->followers;

$user->following;

To save the data in DB you can do as:

$user->following()->attach($follow);

Docs

First off, if you want to store array data into the database, the best column type is probably $table->json('following') . Coupled with a cast of that attribute to JSON in your model , this will let Laravel save/retrieve array data automatically.

That said, you really don't want to be doing that for something like follower data. While some database engines can index JSON data, others can't, and storing it as a string is even worse. SQL is relational, and user-->follower associations are relations .

Instead, you'll want a separate table, something like user_follows , that stores the user_id and the ID of the user they're following as something like follow_id .

Then, use a many-to-many relationship to link them:

public function followers() {
    return $this->belongsToMany('App\User', 'user_follows', 'user_id', 'follow_id');
}

public function following() {
    return $this->belongsToMany('App\User', 'user_follows', 'follow_id', 'user_id');
}

In your User model define mutators .

public function setFollowingAttribute(array $following) {
    $this->attributes['following'] = implode(',', $following);
}
public function getFollowingAttribute(string $following) : array {
    return explode(',', $following);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM