Tuesday, February 7, 2017

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

Leave a Comment

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();     } } 

6 Answers

Answers 1

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.

Answers 2

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.

Answers 3

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.

Answers 4

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

Answers 5

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']); 

Answers 6

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.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment