Mugluck Mugluck - 1 month ago 8
PHP Question

Blade view not printing data from array

I've got a simple enough array of user information I'm trying to print.

I can send the information to the view fine. And a command like:

{{ $who->name }} will give me the name.

However when I do a foreach loop to print all the data in the array I get a bunch of numbers. 1 1 1 and blank spaces.

@foreach ($who as $val)

{{ $val }}

@endforeach


What's going on?

Also, as the array has the titles of each value: ie "Name": "John Doe", is there a way to print the titles separately?

This is the controller:

public function show(UserEdit $object) {
return view('UserEdit', compact('object'));
}


Note the controller is loading the model UserEdit, which has the user's data, and the id is generated from the route. Which I've confirmed works.

Edit: Updating files:

UserEdit.blade:

@extends('layout')


@section('content')
<h1>User Profile Data {{ $object->First_Name }} {{ $object->Last_Name }}</h1>
<br><br>

@foreach ($object as $key=>$value)
{{ $key }} {{ $value }}
@endforeach


@stop


Gives error: Trying to get property of non-object

UserEntryController:


namespace App\Http\Controllers;
use App\UserEdit;
use Illuminate\Http\Request;
use DB;
use App\Http\Requests;

class UserEntryController extends Controller
{
public function index(){
$columns = DB::getSchemaBuilder()->getColumnListing('users');
$profile = UserEdit::all()->where('ID', '530');
return view('UserEntry', compact('profile', 'columns'));
}

public function show(UserEdit $object) {
//$columns = DB::getSchemaBuilder()->getColumnListing('users');
// $profile = UserEdit::all()->where('ID', '530');
// $who = UserEdit::find($ID);
$object = $object->toArray();
return view('UserEdit', compact('object'));
//return $object;
}

}


Routes:

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of the routes that are handled
| by your application. Just tell Laravel the URIs it should respond
| to using a Closure or controller method. Build something great!
|
*/

Route::get('/', function () {
return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index');

Route::get('DB', 'DBController@index');
//$tables = DB::select('SHOW TABLES');
//$titles = DB::select("SELECT * FROM topneeds(Name, Abbrev, jobtitle, region, detail)");

//return view('DB', compact('titles'));
Route::get('Sort', 'SortController@index');
Route::get('User', 'UserEntryController@index');
route::get('User/{object}', 'UserEntryController@show');


UserEdit:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class UserEdit extends Model {



/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';

/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['ID', <--67 values-->, 'remember_token'];

/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = [];

/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [];

/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [];

}

Answer

Your are sending object only to view. So lopping though that object giving you 1 1 1.

That's why this is working for you {{ $obj->name }}

Update

Convert your object to array

public function show(UserEdit $object) {
   $object = $object->toArray();
   return view('UserEdit', compact('object'));
}

Loop though that array

@foreach($object as $key=>$value)
   {{ $key }} - {{ $value }}
@endforeach

Edit from @Mugluck: A quick note, for people who get "Undefined variable" when using the model type hint in this scenario. I solved it with this ($who is the route, which in this case is the ID):

   public function show($who) {
       $array = UserEdit::find($who)->toArray();
       return view('UserEdit', compact('array'));
   }

Apologies for mangled code. But this should give you the result.