1

Below code works adding single single entry i want to store multiple parent_id and user_id

public function test($p_id, $pl_id)
    {
        $CheckRelationship = UsersRelationship::where('parent_user_id',  $p_id )->where('child_user_id', $pl_id )->first();

        if( $CheckRelationship )
        {
            return Response::json( [
            'ok'=> false,
            'message'=> 'The profiles are currently linked '
            ] ,422);
        }

        $user = User::find( $pl_id );
        $user->p_id = $p_id;
        $user->updated_by = $p_id;
        //$user->status = 1;
        $user->save();

        $UsersRelationship = new UsersRelationship;
        $UsersRelationship->parent_user_id = $parent_id;
        $UsersRelationship->child_user_id = $player_id;


        $UsersRelationship->save();



        return Response::json( [
        'ok'=> true,
        'message'=> 'Linked',
        ] ,200);
    }

I want to pass multiple value

$UsersRelationship = new UsersRelationship;
        $UsersRelationship->parent_user_id = $parent_id; //single value passing
        $UsersRelationship->child_user_id = $player_id; //single value passing


            $UsersRelationship->save();

  foreach($UsersRelationship as $k=>$values){
            $UsersRelationship['parent_user_id'] = $values;
            $UsersRelationship['child_user_id'] = $values;
             $UsersRelationship->save();

  }

doesn't work for me please suggest where i am mistaking early reply highly appreciated thanks in advance

7
  • foreach($UsersRelationship as $key=>$values){ $multiRelationship[parent_user_id] = $values; $multiRelationship[child_user_id] = $values; } $multiRelationship->save(); doesn't work Commented Feb 19, 2016 at 5:08
  • insert ->save() inside foreach Commented Feb 19, 2016 at 5:21
  • $multiRelationship->save(); if i entering says Call to a member function save() on a non-object Commented Feb 19, 2016 at 5:38
  • foreach($UsersRelationship as $key=>$values){ $multiRelationship['parent_user_id'] = $values; $multiRelationship['child_user_id'] = $values; $UsersRelationship->save(); } says Call to a member function load() on a non-object Commented Feb 19, 2016 at 5:40
  • url hitting localhost:9000/v1/xyz/4444/myplayer/12693,16054/multiple want to store 12693,16054 values Commented Feb 19, 2016 at 5:41

1 Answer 1

1

Parameters in URLs will work only for a singular user relationship. If you wish to pass multiple relationships, JSON is your friend.

JSON Sample:

[
    {
    'parent_user_id': 1,
    'child_user_id': 2,
    },
    {
    'parent_user_id': 2,
    'child_user_id': 2,
    }    
]

PHP controller function:

public function test(\Illuminate\Http\Request $request) {
    //Pass your information through a GET parameter or POST it through a form
    // 'user_relationship' is the name of the field, expecting data in JSON format
    $user_relationships = json_decode($request->get('user_relationship'));

    //array to store all relationships that were not linked
    $relationshipsNotLinked= [];

    //Loop through each relationship, attach where possible
    foreach($user_relationships as $user_relationship_row) {
        $CheckRelationship = UsersRelationship::where('parent_user_id',  $user_relationship_row['parent_user_id'] )->where('child_user_id', $user_relationship_row['child_user_id'] )->first();

        //If exists, we don't save.
        if( $CheckRelationship ) {
            $relationshipsNotLinked[] =  $user_relationship_row['child_user_id'];
        } else {
            //Else we link relationship

            //Not sure what the links below does..
            //users cannot have `p_id` attribute, given that they may have multiple parents. Same goes for 'updated_by' attribute
            $user = User::find( $user_relationship_row['parent_user_id'] );
            $user->updated_by = $user_relationship_row['parent_user_id'];
            //$user->status = 1;
            $user->save();

            //Save relationship
            $UsersRelationship = new UsersRelationship();
            $UsersRelationship->parent_user_id = $user_relationship_row['parent_user_id'];
            $UsersRelationship->child_user_id = $user_relationship_row['child_user_id'];
            $UsersRelationship->save();


        }

    }


    if(count($relationshipsNotlinked)) {
        //Some relationships were not linked, we display an error message
        return Response::json( [
        'ok'=> false,
        'message'=> 'The following profiles are already linked: '.implode(',',relationshipsNotLinked),
        ] ,422);    
    } else {
        //Display success message
        return Response::json( [
        'ok'=> true,
        'message'=> 'Linked',
        ] ,200);     
    }

}
Sign up to request clarification or add additional context in comments.

8 Comments

in that you function you pass Illuminate\Http\Request, string can i pass $parent_id, $player_id values because i m getting error in url string given also my hitting url is localhost:9000/v1/xyz/4444/myplayer/12693,16054/multiple Array[12693,16054] getting from the js
just changes in your code public function multiplelinkPlayerWithFamily($parent_id, $player_id) { $user_relationships = ''; $user_relationships .= json_decode($parent_id->get('user_relationship')); $user_relationships .= json_decode($player_id->get('user_relationship')); got error (Call to a member function get() on a non-object)
You cannot use the url localhost:9000/v1/xyz/4444/myplayer/12693,16054/multiple Array[12693,16054]. Instead, create an ajax (POST or GET) function which sends the data in JSON format with the parameter user_relationship. Add your current JS in your question, and i'll modify it accordingly.
$http.post("v1/xyz/"+$scope.user.user_id+"/multiple", {"player_id":$scope.selection}) .success(function (response){ //code }
Argument 1 passed to ApiUserController::test() must be an instance of Illuminate\Http\Request, string given error showing
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.