Home » Mysql » How to simplify this Laravel PHP code to one Eloquent query?

How to simplify this Laravel PHP code to one Eloquent query?

Posted by: admin November 29, 2017 Leave a comment

Questions:

I assume that this should all be in one query in order to prevent duplicate data in the database. Is this correct?

How do I simplify this code into one Eloquent query?

$user = User::where( 'id', '=', $otherID )->first();

if( $user != null )
{
    if( $user->requestReceived() )
        accept_friend( $otherID );
    else if( !$user->requestSent() )
    {
        $friend = new Friend;
        $friend->user_1= $myID;
        $friend->user_2 = $otherID;
        $friend->accepted = 0;
        $friend->save();
    }
}
Answers:

I would say if there is a relationship between User and Friend you can simply employ Laravel’s model relationship, such as:

$status = User::find($id)->friends()->updateOrCreate(['user_id' => $id], $attributes_to_update));

Thats what I would do to ensure that the new data is updated or a new one is created.

PS: I have used updateOrCreate() on Laravel 5.2.* only. And also it would be nice to actually do some check on user existence before updating else some errors might be thrown for null.

UPDATE

I’m not sure what to do. Could you explain a bit more what I should do? What about $attributes_to_update ?

Okay. Depending on what fields in the friends table marks the two friends, now using your example user_1 and user_2. By the example I gave, the $attributes_to_update would be (assuming otherID is the new friend’s id):

$attributes_to_update = ['user_2' => otherID, 'accepted' => 0 ];

If your relationship between User and Friend is set properly, then the user_1 would already included in the insertion.

Furthermore,on this updateOrCreate function:

updateOrCreate($attributes_to_check, $attributes_to_update);

$attributes_to_check would mean those fields you want to check if they already exists before you create/update new one so if I want to ensure, the check is made when accepted is 0 then I can pass both say `[‘user_1’ => 1, ‘accepted’ => 0]

Hope this is clearer now.

Questions:
Answers:

I assume that this should all be in one query in order to prevent
duplicate data in the database. Is this correct?

It’s not correct. You prevent duplication by placing unique constraints on database level.

There’s literally nothing you can do in php or any other language for that matter, that will prevent duplicates, if you don’t have unique keys on your table(s). That’s a simple fact, and if anyone tells you anything different – that person is blatantly wrong. I can explain why, but the explanation would be a lengthy one so I’ll skip it.

Your code should be quite simple – just insert the data. Since it’s not exactly clear how uniqueness is handled (it appears to be user_2, accepted, but there’s an edge case), without a bit more data form you – it’s not possible to suggest a complete solution.

You can always disregard what I wrote and try to go with suggested solutions, but they will fail miserably and you’ll end up with duplicates.

Questions:
Answers:

I’m assuming “friends” here represents a many-to-many relation between users. Apparently friend requests from one user (myID) to another (otherId).

You can represent that with Eloquent as:

class User extends Model
{
    //...

    public function friends()
    {
        return $this->belongsToMany(User::class, 'friends', 'myId', 'otherId')->withPivot('accepted');
    }
}

That is, no need for Friend model.

Then, I think this is equivalent to what you want to accomplish (if not, please update with clarification):

$me = User::find($myId);

$me->friends()->syncWithoutDetaching([$otherId => ['accepted' => 0]]);

(accepted 0 or 1, according to your business logic).

This sync method prevents duplicate inserts, and updates or creates any row for the given pair of “myId – otherId”. You can set any number of additional fields in the pivot table with this method.

However, I agree with @Mjh about setting unique constraints at database level as well.

Questions:
Answers:

For this kind of issue, First of all, you have to enjoy the code and database if you are working in laravel. For this first you create realtionship between both table friend and user in database as well as in Models . Also you have to use unique in database .

$data= array('accepted' => 0);
User::find($otherID)->friends()->updateOrCreate(['user_id', $otherID], $data));

This is query you can work with this . Also you can pass multiple condition here. Thanks

Questions:
Answers:

You can use firstOrCreate/ firstOrNew methods (https://laravel.com/docs/5.3/eloquent)

Example (from docs) :

// Retrieve the flight by the attributes, or create it if it doesn't exist...
$flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);

// Retrieve the flight by the attributes, or instantiate a new instance...
$flight = App\Flight::firstOrNew(['name' => 'Flight 10']);

Questions:
Answers:

use `firstOrCreate’ it will do same as you did manually.

Definition of FirstOrCreate copied from the Laravel Manual.

The firstOrCreate method will attempt to locate a database record using the given column / value pairs. If the model can not be found in the database, a record will be inserted with the given attributes.

So according to that you should try :

$user = User::where( 'id', '=', $otherID )->first();
$friend=Friend::firstOrCreate(['user_id' => $myId], ['user_2' => $otherId]);    

It will check with both IDs if not exists then create record in friends table.