2016-12-09 6 views
0

をindex.blade:は非オブジェクトのlaravel 5.3のプロパティを取得しようとすると、私はLaravelでこのエラーで立ち往生しています

非オブジェクト(ビューのプロパティを取得しようとすると:C:... \リソースを\ views \ admin \ users \ index.blade.php)

私のコードに問題はありませんか?

この私のインデックス

@extends('layouts.admin') 


@section('content') 

    <h1>Users</h1> 


    <table class="table"> 
     <thead> 
     <tr> 
      <th>Id</th> 
      <th>Name</th> 
      <th>Email</th> 
      <th>Role</th> 
      <th>Status</th> 
      <th>Created</th> 
      <th>Updated</th> 
      <th>Photo</th> 
     </tr> 
     </thead> 
     <tbody> 

     @if($users) 

      @foreach($users as $user) 

     <tr> 
      <td>{{$user->id}}</td> 
      <td>{{$user->name}}</td> 
      <td>{{$user->email}}</td> 
      <td>{{$user->role->name}}</td> 
      <td>{{$user->active == 1 ? 'Active' : 'Not Active'}}</td> 
      <td>{{$user->created_at->diffForHumans()}}</td> 
      <td>{{$user->updated_at->diffForHumans()}}</td> 
      <td>{{$user->photo_id}}</td> 
     </tr> 
       @endforeach 
      @endif 
     </tbody> 
    </table> 
@stop 

この私のコントローラ

public function index() 
    { 
     // 
     $users = User::all(); 

     return view('admin.users.index', compact('users')); 
    } 

    /** 
    * Show the form for creating a new resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function create() 
    { 
     // 
     $roles = Role::pluck('name','id')->all(); 

     return view('admin.users.create', compact('roles')); 
    } 

    /** 
    * Store a newly created resource in storage. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @return \Illuminate\Http\Response 
    */ 
    public function store(UsersRequest $request) 
    { 
      //Buat bikin create dengan method ini 

     User::create($request->all()); 

     return redirect('/admin/users'); 

//  return $request->all(); 
    } 

    /** 
    * Display the specified resource. 
    * 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function show($id) 
    { 
     // 
     return view('admin.users.show'); 
    } 

    /** 
    * Show the form for editing the specified resource. 
    * 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function edit($id) 
    { 
     // 
     return view('admin.users.edit'); 
    } 

    /** 
    * Update the specified resource in storage. 
    * 
    * @param \Illuminate\Http\Request $request 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function update(Request $request, $id) 
    { 
     // 
    } 

    /** 
    * Remove the specified resource from storage. 
    * 
    * @param int $id 
    * @return \Illuminate\Http\Response 
    */ 
    public function destroy($id) 
    { 
     // 
    } 
} 

この私の要求

class UsersRequest extends FormRequest 
{ 
    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() 
    { 
     return true; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     return [ 
      // 
      'name'=>'required', 
      'password'=>'required', 
      'email'=>'required', 
      'active'=>'required', 
      'role_id'=>'required', 
      'photo_id'=>'required|image|mimes:jpeg,png,jpg,gif,svg|max:7098', 
     ]; 
    } 
} 
+1

詳細は働くべきですか?行番号は? –

答えて

0

この手段(コントローラ内部の)

$users = User::all(); 

は空のオブジェクトを返します。 dbにはユーザーがいません。おそらく(管理者/ユーザー/ index.blade.php)から来ているエラー

{{$user->id}} 

と他の線も同様に従ってください。

ユーザーをループする前に、データが空であることを確認してください。それは、ユーザーのレコードがない場合は空の配列を返し、ループに入ることはありません。この

@if($users->toArray()) 

のように見えるように、このラインに

@if($users) 

を修正します。 これにより、エラーが修正されます。

<td>{{ $user->role ? $user->role->name : '-' }}</td> 

PS: Put an else statement for the @if statement with proper message so you can know it worked.

+0

助けてくれてありがとうございます –

0

この行を変更してください

<td>{{$user->role->name}}</td> 

...今:)

+0

ありがとう –

関連する問題