当我尝试按用户ID查找数据时,dd()返回null

时间:2017-06-12 11:34:46

标签: mysql laravel-5.3

我正在尝试通过user_id从数据库中检索数据。我的视图显示了这个错误:试图获取非对象的属性。 dd()返回null。

这是我的控制器:

AbstractItemCountingItemStreamItemReader

这是我的用户模型:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\Complain;
use App\Feedback;
use App\Item;
use Illuminate\Support\Facades\Redirect;
use Session;
use Auth;

class ProfileController extends Controller
{
    public function profile($id){

      $complain = Complain::find($id);
      dd($complain);
      return view('user.profile')->with(['complains' => User::find($id)->complains]);

    }
} 

这是我的投诉模型。

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'surname', 'regnumber', 'course', 'department', 'email', 'password',
    ];

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

    public function complains()
    {
        return $this->hasMany('App\Complain');
    }
}

这是我的观点:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Complain extends Model
{
    protected $fillable = [
      'user_id', 'title', 'body', 'name', 'regnumber'
    ];

    public function user(){
        return $this->belongsTo('App\User');
    }
}

user_id是外键。如何根据user_id成功显示投诉(数据)?

这是我的用户表:

@foreach($complains as $complain)
   <div>
    <h3 class="operator-complain-title">Title: </h3>
    <p>{{ $complain->title }}</p>
    <h3 class="operator-complain-title">Complain:</h3>
    <p>{{ $complain->body }}</p>
   </div>
    <hr>
@endforeach

这是我的投诉表:

id | name | email | password
7    John   email   password

有想法的人请分享。

3 个答案:

答案 0 :(得分:2)

据我所知,您希望用户$id投诉。

这样做Complain::find($id)不会返回用户的抱怨,因为id字段可以找到。

但你想通过user_id字段抱怨。

因此对于这种情况:Complain::whereUserId($id)->get()



检查一下(建议在您显示用户的数据及其投诉时):

public function profile(Request $request, $id){
  $User = User::with(['complains'])->find($id);
  if(!$User) return abort(404, 'User with id:'.$id.' not found');

  return view('user.profile')->with(['user' => $User, 'complains' => $User->complains]);
}

或者这个(如果你只是出现抱怨):

public function profile(Request $request, $id){
  $complains = Complain::whereUserId($id)->get();
  if(!$complains) return abort(404, 'No complains for user (id:'.$id.') found');

  return view('user.profile')->with(['complains' => $complains]);
}



如果$id是用户名:

public function profile(Request $request, $name){
  $User = User::with(['complains'])->whereName($name)->first();
  if(!$User) return abort(404, 'User with name:'.$name.' not found');

  return view('user.profile')->with(['user' => $User, 'complains' => $User->complains]);
}

答案 1 :(得分:0)

在尝试使用

之前,您会先尝试检查对象是空还是空
<?php
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}
?>

答案 2 :(得分:0)

public function profile($id)

应该是

public function profile(Request $request)

和Eloquent

$complain = Complain::find($request->id);

或从段获取查询。示例域/ id / =来自段号1的id

$complain = Complain::find($request->segment(1));