Ok, so this is a question I hope can help other newbies as I'm running into errors pulling information from arrays in Blade and I'm unfamiliar with it's syntax to properly debug.
I understand how to send array data to the view normally, for instance:
public function index() {
$variable = DB::table('tablename')->where('ID', '535');
return view('viewname', compact('variable'));
}
This will send everything attached to the ID of 535 to the view. The title can then be printed out like so:
@foreach ($variable as $foo)
{{ $foo->title }}
@endforeach
Or if you want to print everything (I Think this is right?):
@foreach ($variable as $foo)
@foreach ($foo as $name)
{{ $name }}
@endforeach
@endforeach
That process I kind of understand.
But where I'm getting stuck is with models.
Let's say I set up some routes:
Route::get('User', 'UserEntryController@index');
route::get('User/{id}', 'UserEntryController@show');
And in the controller one that grabs the show route:
<?php
namespace App\Http\Controllers;
use App\UserEdit;
use Illuminate\Http\Request;
use App\Http\Requests;
class UserEntryController extends Controller
{
public function show(UserEdit $id) {
return $id;
}
}
This will return everything attached to the model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserEdit extends Model {
protected $table = 'users';
protected $fillable = ['ID', various', 'pieces', 'of', 'table', 'data'];
protected $hidden = [];
protected $casts = [];
protected $dates = [];
}
However if I change a line on the controller:
<?php
namespace App\Http\Controllers;
use App\UserEdit;
use Illuminate\Http\Request;
use App\Http\Requests;
class UserEntryController extends Controller
{
public function show(UserEdit $id) {
return view('UserEdit', compact('id'));
//return $id;
}
}
The aforementioned Blade code will not run. In fact, I can't parse the array sent out to the the view at all. Of course a straight {{ id }} will give me the actual contents of the array.
{"ID":535,"various":"Del","pieces":"22","of":"32","table":"54","data":"John"}
So I guess my question is. If I'm getting data that's in the form of an array like this. How do iterate through it to apply formatting, put it in tables, or put it in a form, etc?
You're naming convention actually confuses you. As long as you're returning and using a Object, rename that $id variable to something more obvious. like:
For the case when your framework is not set to bind directly to the object and your show method receives the $id, than you should return the object by querying the DB.
... and nou in your view blade file you can use it like in your above examples.