【问题标题】:Laravel Object errorLaravel 对象错误
【发布时间】:2018-07-13 17:46:12
【问题描述】:

我正在尝试将具有指定角色的用户添加到数据库中,错误指示

“748460238d8f66b0fe4acb25c19f24f04b548bd9.php 第 25 行中的错误异常: 试图获取非对象的属性(视图:..\views\admin\users\index.blade.php)”。

下面是UserModel、index.blade.php 和控制器。

怎么了?

非常感谢

<?php

namespace App\Http\Controllers;

use App\Role;
use App\User;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Requests\UsersRequest;

class AdminUsersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    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::lists('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)
    {
        //
         User::create($request->all());
        //return $request->all();


      return redirect('/admin/users');
    }

    /**
     * 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)
    {
        //
    }
}
<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token'
    ];

    public function role(){

        return $this->belongsTo('App\Role');
    }
}
@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>
      </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->is_active == 1 ? 'Active' : 'No active' }}</td>
          <td>{{ $user->created_at }}</td>
          <td>{{ $user->updated_at }}</td>
      </tr>
      @endforeach
@endif
    </tbody>
  </table>


@stop

迁移架构:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRolesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('roles');
    }
}
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('role_id')->index()->unsigned()->nullabe();
            $table->integer('is_active')->default(0);
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }
}

【问题讨论】:

  • role_id 是用户表中的外键
  • 检查many to many关系
  • 您为什么没有回复任何人的答案?如果您需要免费帮助,请至少结束交易并解决您的问题。谁帮助了?谁没有?现在修好了吗?为什么?

标签: laravel laravel-5


【解决方案1】:

在你的刀片文件中,试试这个:

 @foreach($users as $user)
  <tr>
    <td>{{ $user->id }}</td>
    <td>{{ $user->name }}</td>
    <td>{{ $user->email }}</td>
    <td>
        @if($user->role)
           {{ $user->role->name }}
        @else
          No role defined
        @endif
    </td>
    <td>{{ $user->is_active == 1 ? 'Active' : 'No active' }}</td>
    <td>{{ $user->created_at }}</td>
    <td>{{ $user->updated_at }}</td>
  </tr>
  @endforeach

【讨论】:

  • 错误消失了,但要么我选择了显示在选择中的管理员,角色总是没有定义角色
【解决方案2】:

在 User 模型中更新函数 role(),如下所示:

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'name', 'email', 'password'
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token'
];

public function role(){

    return $this->belongsTo('App\Role','role_id');
}
}

【讨论】:

  • ErrorException in 748460238d8f66b0fe4acb25c19f24f04b548bd9.php line 42: Trying to get property of non-object (View: C:\wamp64\www\codehacking\resources\views\admin\users\index.blade.php)
【解决方案3】:

在$fillable 内的User 模型中添加role_id 和is_active 以将这些记录保存在用户表中。

protected $fillable = [
    'name', 'email', 'password','role_id','is_active',
];

【讨论】:

    【解决方案4】:

    为什么不检查748460238d8f66b0fe4acb25c19f24f04b548bd9.php 的第 25 行是什么?找到文件并检查它。

    我的第一个假设是您没有使用User::with('role')-&gt;get() 而不是User::all()。如果没有加载关系,您将无法访问 $user-&gt;role-&gt;name。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-05
      • 2017-01-30
      • 2017-01-22
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 2018-02-18
      • 1970-01-01
      相关资源
      最近更新 更多