1

I'm trying to render out multiple returns, what is the best way of rendering both returns.

One of them returns which is the deleteable collect, the updatable doesn't return.

public function getPosts()
{

    $posts = Post::with('user')->get();
    $response = new Response(json_encode($posts));
    $response->headers->set('Content-Type', 'application/json'); 
    return response()->json(Post::with('user')->get()->map(function(Post $post){ 
        return collect($post->toArray())->put('deletable', auth()->user()->can('delete', $post)); 
        return collect($post->toArray())->put('update', auth()->user()->can('update', $post));
    })); 
}

updated, posts don't appear doing the following:

public function getPosts()
{
    $posts = collect(Post::with('user')->get());
    $response = new Response(json_encode($posts));
    $response->headers->set('Content-Type', 'application/json'); 

    $data = $posts->map(function(Post $post)
    { 
        $post->toArray()->put('deletable', auth()->user()->can('delete', $post)); 
        $post->toArray()->put('update', auth()->user()->can('update', $post));

        return $post;
    });

    return response()->json($data); 
}
15
  • 6
    You can't return more than once per function call. The function exits as soon as you return. The first return will run, the function will exit, and the second will never been seen. A good IDE would have told you that the second return is dead code. Commented Nov 24, 2017 at 22:32
  • i use sublime text, what will be the best way to tackle this ? Commented Nov 24, 2017 at 22:33
  • For that reason conditional control structures like if is found. Commented Nov 24, 2017 at 22:34
  • 1
    Return an array of your objects. Then deconstruct it as required in your calling code. Commented Nov 24, 2017 at 22:36
  • 2
    @BARNOWL If you need to return multiple values, you need to stick everything in a structure like an array, then return that structure. An array will work. If you can't get it to work, you need to give specific information so we have a hope of helping you. Commented Nov 24, 2017 at 22:39

1 Answer 1

2

Try something along the lines of :

public function getPosts()
{
    $posts = Post::with('user')->get();
    $response = new Response(json_encode($posts));
    $response->headers->set('Content-Type', 'application/json'); 

    $data = $posts->map(function(Post $post)
    { 
        $post->toArray())->put('deletable', auth()->user()->can('delete', $post); 
        $post->toArray())->put('update', auth()->user()->can('update', $post);

        return $post;
    });

    return response()->json($data); 
}

Whats going on here:

  1. Use the $posts variable in the array map instead of making the call again.
  2. Do the actions to each $post in the array map.
  3. Return the $post to the array of $data.
  4. Return the $data.

Here are the docs on array maps and return.

You CANNOT return more than once.

Read these:

https://laravel.com/docs/5.5/collections#method-map

http://php.net/manual/en/function.return.php

Update:

Try this:

public function getPosts()
{
    $posts = Post::with('user')->get();
    $response = new Response(json_encode($posts));
    $response->headers->set('Content-Type', 'application/json'); 


    $data = $posts->map(function(Post $post)
    { 
        $user = auth()->user();

        if($user->can('delete', $post)) {
            $post['deletable'] = true;
        }

        if($user->can('update', $post)) {
            $post['update'] = true;
        }
        return $post;
    })

    return response()->json($data); 
}
Sign up to request clarification or add additional context in comments.

9 Comments

this looks sort of "hacky", is there a way i can pass the two collect returns into an array, to make it look more php 7 appropriate. I understand whats going on being that you explained it, but the format is quite "hacky"
your a life saver, been looking for the that sneaky collections documentation thanks
tried your code worked out the syntax errors now my posts don't appear.
yeah, posts don't appear ill edit the thread to show you what i got.
You made me a happy owl , thank you :) hoo hooo hoot
|

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.