Blade view not printing data from array

天大地大妈咪最大 提交于 2019-12-25 08:30:44

问题


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 = [];

}

回答1:


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.




回答2:


Try to change your controller like this. This should pass an object to blade:

return view('UserEdit')->with(compact('object'));

Then you should be able to use it your blade like

@if ($object)
   @foreach($object as $val)
      {{ $val->name }}
   @endforeach
@endif

Update: Also check this answer




回答3:


if you have array in $val. so you cant do this:-

@foreach ($who as $val)

      {{ print_r($val) }}

 @endforeach


来源:https://stackoverflow.com/questions/39990911/blade-view-not-printing-data-from-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!