Php Laravel雄辩的关系不检索结果
我正试图使与拉雷维尔(5.6)的桌上关系更具说服力。我无法从模型中检索关系表结果。下面我提到了这种关系 关系:项目有一个类别 这是我的商品模型Php Laravel雄辩的关系不检索结果,php,mysql,laravel-5,Php,Mysql,Laravel 5,我正试图使与拉雷维尔(5.6)的桌上关系更具说服力。我无法从模型中检索关系表结果。下面我提到了这种关系 关系:项目有一个类别 这是我的商品模型 class InventoryItems extends Model { /** * table row delete */ use SoftDeletes; /** * The table associated with the model. * * @var string
class InventoryItems extends Model
{
/**
* table row delete
*/
use SoftDeletes;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'inventory_items';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $guarded = ['id'];
protected $fillable = ['name','type_id','category_id','sub_category_id','description','cost_price','sale_price','image_path','image','store_id','status'];
public function category()
{
return $this->belongsTo('App\ItemCategories', 'id', 'category_id');
}
}
这是我的分类模型
class ItemCategories extends Model
{
/**
* table row delete
*/
use SoftDeletes;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'item_categories';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $guarded = ['id'];
protected $fillable = ['name'];
}
这是我的控制器
public function index(Request $request)
{
$items = InventoryItems::all();
dd($items);
}
这就是结果
Collection {#722 ▼
#items: array:2 [▼
0 => InventoryItems {#719 ▼
#table: "inventory_items"
#guarded: array:1 [▶]
#fillable: array:11 [▼
0 => "name"
1 => "type_id"
2 => "category_id"
3 => "sub_category_id"
4 => "description"
5 => "cost_price"
6 => "sale_price"
7 => "image_path"
8 => "image"
9 => "store_id"
10 => "status"
]
#connection: "mysql"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:15 [▼
"id" => 1
"name" => "Dell Keyboard"
"type_id" => 1
"category_id" => 1
"sub_category_id" => 1
"description" => "<p>test key</p>"
"cost_price" => 100.0
"sale_price" => 500.0
"image_path" => null
"image" => null
"store_id" => 1
"status" => 1
"created_at" => "2018-06-02 14:31:32"
"updated_at" => "2018-06-02 14:31:32"
"deleted_at" => null
]
#original: array:15 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#forceDeleting: false
}
Collection{#722▼
#项目:阵列:2[▼
0=>InventoryItems{719▼
#表:“库存商品”
#防护:阵列:1[▶]
#可填充:数组:11[▼
0=>“名称”
1=>“类型\u id”
2=>“类别id”
3=>“子类别id”
4=>“说明”
5=>“成本价格”
6=>“销售价格”
7=>“图像路径”
8=>“图像”
9=>“门店标识”
10=>“状态”
]
#连接:“mysql”
#主密钥:“id”
#键类型:“int”
+递增:真
#带:[]
#withCount:[]
#每页:15
+存在:正确
+最近被创建:false
#属性:数组:15[▼
“id”=>1
“名称”=>“戴尔键盘”
“type_id”=>1
“类别id”=>1
“子类别id”=>1
“说明”=>“测试密钥”
“成本价格”=>100.0
“销售价格”=>500.0
“图像路径”=>null
“图像”=>null
“门店标识”=>1
“状态”=>1
“创建于”=>“2018-06-02 14:31:32”
“更新时间:2018-06-02 14:31:32”
“已删除”=>空
]
#原件:阵列:15[▶]
#更改:[]
#演员:[]
#日期:[]
#日期格式:空
#附件:[]
#调度事件:[]
#可观测值:[]
#关系:[]
#触摸:[]
+时间戳:真
#隐藏:[]
#可见:[]
#强制删除:false
}
我不熟悉雄辩的关系,我参考了laravel文档和其他一些教程,但没有奏效。这肯定是一个愚蠢的错误想法。首先,尝试使用('category')添加到控制器
,这样雄辩者知道你正在调用关系。因此,你的控制器应该看起来像
public function index(Request $request)
{
$items = InventoryItems::with('category')->get();
dd($items);
}
第二,我认为键的顺序是错误的,应该是错误的
return $this->belongsTo('YourModel','foreing_key','other_model_key')
所以你们的关系应该是这样的
return $this->belongsTo('App\ItemCategories', 'category_id', 'id');
有一些事情正在发生
首先,Laravel在处理模型关系时使用“急切加载”
从文档中删除。()
这意味着在您首次访问属性之前,不会实际加载关系数据。但是,Eloquent可以在您查询父模型时“急切加载”关系
这意味着,在你尝试访问它之前,Laravel不会加载你的关系
例如,下面的代码将延迟加载该类别
foreach (InventoryItems::all() as $inventoryItem) {
dump($inventoryItem->category); // Laravel loads the category here and not before
}
还可以在检索模型本身时加载关系
$items = InventoryItems::all()->load('category'); // Laravel loads the category here.
dd($items);
这里唯一需要注意的是,当您急于加载时,您应该使用关系函数的名称,而不是您检索的模型的名称
如果您的类别关系定义为
public function itemCategory()
你会用
InventoryItems::all()->load('itemCategory');
需要提及的一点是->load()只对集合有效,而对构建器无效
对于构建器,您需要将->与()一起使用
e、 g
您的解决方案是正确的。但是我必须在控制器中使用get()而不是all()。
InventoryItems::all()->load('category'); // all() has already returned a collection so we have to 'load' additional data
InventoryItems::where('condition','=','something')->with('category')->get(); // This is a builder (until we call get() or first()) so we can ask for the data to be delivered 'with' the category to the collection