0

I'm trying to merge eloquent result and array because I need to add all possible filters that user can use for this model. If anyone have any other idea how to make it I really would be very thankful. Here is an example code:

<?php

class School extends Eloquent {
    protected $table = 'schools';

    public function listSchoolsEndUser()
    {
        $schools_data = new School;

        $schools_data = $schools_data->paginate(12);

        $filters = array( 
            'filters' => array(
                'name' => 'Neshtoto'
            )
        );

        $schools_data = (object) array_merge(get_object_vars($schools_data), $filters);

        echo '<pre>';
        print_r( $schools_data );
        exit;

        return $schools_data;
    }

And the result is very interesting:

stdClass Object
(
    [filters] => Array
        (
            [name] => Neshtoto
        )

)
1
  • Please provide an example including the expected result Commented Feb 4, 2015 at 21:18

1 Answer 1

1

If you just want to send both, filters and the school_data back in a JSON response you can do it this way:

return Response::json(array(
   'filters' => array(
        'name' => 'Neshtoto'
    ),
    'data' => $school_data->toArray()
));

Or if you want to use array_merge:

$school_data = array_merge($filters, array('data' => $school_data->toArray()));
return $school_data;

Edit

If you are just injecting the data into a view I see no reason at all to merge the data, just pass two variables!

return View::make('view', array('schools' => $school_data, 'filters' => $filters));

(Here $filters would obviously only be array('name' => 'Neshtoto') and not the full thing including 'filters' => ...)

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

4 Comments

Yes it's return array but then I can't use pagination and laravel throw Trying to get property of non-object in blade file. I'm using the simple foreach @foreach($schools->data as $school) .... @endForeach. Is there any reason to keep laravel's pagination and add filters?
Ah you're passing it to a view? Didn't look like that in your question. Editing now...
Yes, I can be more descriptive. Sorry
Thank you dude :) I'm just beginner in laravel and sorry if I'm asking a bit silly questions.

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.