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....
- Data passed through as an Eloquent object
- 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.
- Convert my foo Eloquent object to an array?
- Convert my foo array to an object?
- Other
If using option 2, should the object be the Foo model? Or another Foo class?