Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 访问有说服力的关系属性_Php_Laravel_Eloquent_Models - Fatal编程技术网

Php 访问有说服力的关系属性

Php 访问有说服力的关系属性,php,laravel,eloquent,models,Php,Laravel,Eloquent,Models,我有三个模型,都是一对多关联的。类别、子类别和样式。我的关系是双向的——尽管我似乎在访问相关属性时遇到了问题 <?php namespace Paragon\Products; use Illuminate\Database\Eloquent\Model as Eloquent; class Subcategory extends Eloquent { protected $table = 'product_subcategories'; protected $f

我有三个模型,都是一对多关联的。类别、子类别和样式。我的关系是双向的——尽管我似乎在访问相关属性时遇到了问题

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
在我的查询运行之后,我将得到一个样式实例,其中“关系”是子类别的实例,子类别中的“关系”是类别的实例。这是完全正确的

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
问题是我现在似乎无法访问相关的模型实例。例如,如果我打电话:

$style->subcategory->name;
<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
我得到“尝试获取非对象的属性”。所以我试着只调用$style->subcategory,结果是'1'

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
为什么$style->subcategory不返回子类别模型的实例?是我遗漏了什么,还是我的理解不正确

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
--编辑--

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
型号

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
类别

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Category extends Eloquent {

    protected $table = 'product_categories';

    protected $fillable = [
            'name',
            'slug',
            'image'
    ];

    public function subcategories() {
            return $this->hasMany('Paragon\Products\Subcategory', 'category');
    }

}
<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
表格

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
Paragon\产品\类别

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
ID    ...
1
2
Paragon\产品\子类别

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
ID   Category    ...
1    2
2    2
Paragon\Products\Style

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
ID   Subcategory    ...
1    1
2    1

既然样式模型中的subcategory方法应该引用subcategory的单个实例,而不是它们的集合,那么我就不能按我现在(或正在尝试)的方式调用属性了吗?

我在这里是瞎猜。但我将尝试解释如何使用collection以及first和get之间的区别

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
$users= $user-> with('images') -> first(); <-- this is first  row in the table users, each user has many images.
$users-> username; //works;

$users-> images-> image_name; // wont work , model has many ,    
you get error <-- Trying to get property of non-object. 
// access the proprety image and loop through the collection of objects.
$images = $user-> images; // 

foreach ($images as $image){
  echo $image- >image_name; 
}
就你而言

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
$style -> subcategory() -> name;
通过id和子类别获取样式的步骤

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
$style -> with('subcategry') -> where('id', $styleId) -> first();

好吧,我想我现在明白了。您的雄辩模型称为子类别,但外键也是。所以当你打电话的时候

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
$style->subcategory
即返回外键而不是模型。要解决此问题,我建议将外键id的名称更改为subcategory_id。如果无法更改数据库,可以通过将方法链接到类似的内容来强制它使用模型

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
$style->subcategory()->first()->name
编辑: 另一个想法是,你可以将关系的名称改为

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
public function subcategory_item()
{
  return $this->belongsTo('Paragon\Products\Subcategory', 'subcategory');
}
那么你应该能够正确地引用它

<?php

namespace Paragon\Products;

use Illuminate\Database\Eloquent\Model as Eloquent;

class Subcategory extends Eloquent {

    protected $table = 'product_subcategories';

    protected $fillable = [
            'category',
            'name',
            'slug',
            'image'
    ];

    public function styles() {
            return $this->hasMany('Paragon\Products\Style', 'subcategory');
    }

    public function category() {
            return $this->belongsTo('Paragon\Products\Category', 'category');
    }

}
$style->subcategory_item->name

你是如何质疑这种关系的?在
样式
表中是否有
子类别
列?它可能返回的是该列的值,而不是相关的模型。您确定除了hasMany之外还设置了belongsTo关系吗?您需要向我们展示您的模型,以便我们可以帮助您。@user3158900我知道!这可能就是问题所在。@AdrianFlannery我已经设置好了,请参见上文我正在尝试访问单个模型。样式有一个子类别,但子类别有许多样式。谢谢你的回答