1

First, yes This question is very similar to Arrays vs Objects in view template but my question sort of expands on this...

When deciding on whether to use an object or array to pass data to your view. Which would be a better approach when you're looking for a view that you can re-use for both....

  1. Data passed through as an Eloquent object
  2. Data that is manually created in your controller that does NOT originate from an Eloquent or any other object...

For example let's say I have a piece of code in my view that is simply...

{!! $foo->bar !!}

I have my Foo Model..

class Foo extends Model
{
    protected $fillable =[
      'bar',
    ];

and MyController controller...

class MyController extends Controller
{
    public function fromDB()
    {
        $foo = Foo:all();
    }

    public function manually()
    {
        $foo = array(
            'bar' => 'Hello World',
        );
    }

To re-use my view, should I.

  1. Convert my foo Eloquent object to an array?
  2. Convert my foo array to an object?
  3. Other

If using option 2, should the object be the Foo model? Or another Foo class?

1

This is one of the reasons why view models were created. Define a class that holds the necessary data for the view and this question becomes moot. The source of the data could be an Eloquent object or array, but since the view is rendering its own model the original source and format of the data is irrelevant.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.