Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
    }
}
like image 266
Z0q Avatar asked Jan 25 '17 09:01

Z0q


People also ask

How can I get only one record in Laravel?

Using Laravel Eloquent you can get one row using first() method, it returns first row of table if where() condition is not found otherwise it gives the first matched row of given criteria.

How can I limit a SQL query in Laravel?

Laravel limit accepts one parameter as count( number of count). Model::limit(10); //or \DB::table('table_name')->limit(10); In the above syntax we have used 1 examples to show possibilities to use limit function in laravel. Also we can use offset function in laravel to define the starting point in query to limit.

Is Laravel eloquent slow?

Laracasts Veteran Sure it is slow, first it fetch all the record in one time, then it instantiate an eloquent object for each lines. It is not made to retrieve 10 000 objects.


2 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.

like image 107
Mjh Avatar answered Sep 20 '22 14:09

Mjh


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.

like image 23
Oluwatobi Samuel Omisakin Avatar answered Sep 19 '22 14:09

Oluwatobi Samuel Omisakin