Laravel 5.2有很多关系 - 无法访问字段

时间:2016-03-16 03:54:45

标签: laravel-5 eloquent relational-database

我已经阅读了有关雄辩关系的文档以及有关它的不同文章。但似乎我很难进入我的桌子关系的字段。请指教。

User->hasMany BuyRequest

用户表列:

id,
userid (int),
name

BuyRequest表列:

id,
buyrequest_id,
userid (int)

这是我的用户模型的代码,

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\BuyRequest;
class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','userid',
    ];

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

    public function buyRequest(){
        return $this->hasMany(BuyRequest::class,'userid','userid');
    }

}

我的buyRequest模型的代码

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use App\User;
class BuyRequest extends Model
{
    //
    protected $table = 'buyrequest';

    protected $fillable = [
        'buyrequest_id'
    ];

}

我正在访问变量的控制器类

<?php

namespace App\Http\Controllers;

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

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\ProductPostRequest;
use App\Products;
use Illuminate\Support\Facades\DB;
use App\BuyRequest;
use App\Http\Requests\RFQRequest;
use App\ProductCategory;

class TaskController extends Controller
{

    public function show(Request $request){

        $user = new User();
        $buyrequest = $user->buyRequest();

        dump($buyrequest->getResults());
    }

}

输出:

Collection {#241 ▼
  #items: []
}

我的收藏品中没有任何商品,我是否正确访问了buyrequest?请帮忙。

我已经尝试了laravel文档中的那个,请参阅下文。

$buyrequest1 = User::find(1)->buyrequest;

        foreach ($buyrequest1 as $br) {
            dump($br->title);
        }

我得到了这个输出:

尝试获取非对象的属性

提前谢谢。

2 个答案:

答案 0 :(得分:1)

在您的BuyRequest模型中添加列名userid,如下所示:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\BuyRequest;
class User extends Authenticatable
{
   /**
   * The attributes that are mass assignable.
   *
   * @var array
   */
   protected $fillable = [
       'name', 'email', 'password','userid',
   ];

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

    public function buyRequest(){
        return $this->hasMany(BuyRequest::class,'userid','userid');
    }

}

并按以下方式访问它:

class TaskController extends Controller
{

    public function show(Request $request){

        $user = User::with('buyRequest')->where('userid','1')->firstOrFail();

        dump($user[0]->buyRequest[0]->title);
    }

}

答案 1 :(得分:1)

好的,过了一天。我想我找到了我正在寻找的东西。感谢@Nadimul De Cj帮助我尝试其他选项并帮助我解决它。

基本上,在您可以访问购买请求的用户之前,您应首先过滤您想要输出的购买请求访问权限。

好的另一种解释是,首先选择要使用where语句访问buyrequest的用户.....所以,id等于1的用户然后访问他的购买请求。我们不能只使用用户模型然后直接访问buyrequest模型,或者我们可以说这不可行。

这里是代码。

$user = User::where('userid','1')->firstOrFail();
        dump($user->buyrequest['0']->title);

输出: &#34; Cold Pyro Indoor Show Crackers&#34;

这是我的关系代码。

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\BuyRequest;
class User extends Authenticatable
{

 public function buyRequest(){
        return $this->hasMany(BuyRequest::class,'userid','userid');
    }

}