我想知道是否可以从与相关表相关的表中选择数据。我将添加一张图片以使其更清晰: ? 我目前拥有的是:Sensor::all()->with('sensor_types', 'sensor_triggers')->get(); 这将返回sensors、sensor_types和sensor_triggers表。我还希望尽可能多地将value_types与sensor_types结合起来,因为我只能从直接相关的数据库中选择数据,而不能像sensors with sensor_types with value_types那样使用'double
我试图使用Laravel 5的(L5)模型::with ()方法,获得具有给定类别的产品的简单列表。但是L5似乎忽略了where子句的类别。
我的产品模型中的关系:
public function categories(){
return $this->belongsToMany('App\Category', 'categories_products');
}
在我的主计长中:
public function getByCategory($slug){
$return = Product::with(array('categori
正常的关系方法通常没有条件,通常如下所示:
class StripeCustomer extends Model
{
public function user()
{
return $this->belongsTo(User::class, 'stripe_customer_id');
}
}
在我的模型中,关系方法有一个条件,如下所示:
class StripeCustomer extends Model
{
public function user()
{
if ($this->type
我正在使用Laravel5.2开发项目管理应用程序,在我的应用程序中,用户可以创建项目,每个项目都有许多任务,每个任务也有许多子任务。在我的表中,项目和任务表有以下关系。任务模型与项目模型的关系是
public function project()
{
return $this->belongsTo('App\Project');
}
项目模型与任务表的关系
public function tasks(){
return $this->hasMany('App\Task');
}
我需要子任务表
好的,我正在做一个laravel 5.5项目。
我们有一个用户表,并且在这个用户表中有一个父字段。父项拒绝此对象的父项关系。
因此,每个用户可以有一个父级,但一个用户可以有多个子级。
public function parent()
{
return $this->belongsTo(User::class, 'parent_id', 'id');
}
public function children()
{
return $this->hasMany(User::class, 'parent_id',
我有以下关系定义:
邮报:
class Post extends Model
{
use SoftDeletes;
protected $fillable = ['title','content','category_id','featured_image','slug'];
protected $dates = ['deleted_at'];
public function category()
{
return $this->belongsTo('A
我正在使用Laravel和它提供的雄辩的ORM,但我正在努力选择我需要的数据。我有两个模型 Company
Member 我该怎么做呢? SELECT * FROM members m, companies c
WHERE (m.main_type = "2" AND m.main_id = c.id AND m.status = "1")
AND ((m.username LIKE "%keywords%") OR (c.name LIKE "%keywords%"))
我想使用Laravel Eloquent从数据库中检索一个连接表。我能够获得这种关系,但是如何获得属于联接表的详细信息?例如,我有一个applicants和applicant_children表。applicant_children表是applicants表中父级和子级的连接表。我得到的细节只是父母的细节。
Applicant.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\ApplicantChildren;
class Applicant extends Model {
p
我有一张HouseholdProfile hasMany HouseholdMembers.桌我想用一个附加字段来查询所有的HouseholdProfile,其中包括一个特定配置文件的成员数量。
我想要的:
ID Profile No of Dependents
1 Greg 3 <--- 3 is Number of child record in member table
2 Roger 2
我试过下面的查询,但我认为它缺少一些东西。
$query = Househo
我有一个简单的查询,如下所示:
$category = Category::with('translation')
->with(['childCategories' => function ($query) {
$query->active();
}])
->where('id', $id)->first();
范围和关系:
public function scopeActive($query)
{
re
我正在使用Laravel5.1作为我的新项目智能搜索。我的问题是使用like查询从外键获取数据以搜索该表。我的数据库表是:
category->id, name
search->id, category_id (foreign_key), question, answer, tags
我的模型代码是:类别模型
public function helpcenter() {
return $this->belongsTo('App\HelpCenter');
}
帮助中心模型
public function category() {
return
假设我有parent->child one下的产品和parent->child one->child one child下的另一个产品,我所能得到的只有最后一个类别名称,比如:child one和child one child,但是前面的父级名称我无法得到。
代码
类别模型:
public function categories()
{
return $this->hasMany(Category::class);
}
public function childs() {
return $this->hasMany(Category::
我刚接触过laravel,并做了一个基本的应用程序来掌握关系。我实现了一对一的关系,希望从基表和相关表中获得特定的列。我有两个表,即用户,identity_cards.Relationship定义如下
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*
我想做以下几点:
我有两张桌子:
1) Content
id,
section_id
parent_id,
sequence,
2) Sections
id,
title,
description,
date_entered
每个内容都必须有一个节,该节由外键定义,内容可以有一个子节,如果内容具有相同的parent_id,则将其归类为子节。例如:
1. My first section
1.1. My first sub section
2. My second section
3. My third section
在拉威尔面临一个奇怪的问题。一切正常,数据库对于正常的读/写操作和常规的雄辩查询都工作得很好。但是当我使用 $query->whereHas('some_related_model',function($q){
$q->whereIn('column_on_related_model_table',[array_values]);
}) 它给出连接被拒绝的错误,如标题I am using laravel 6.x,mySql 8.0 and php v7.4中所示 我已经在上面写了一个通用的代码。每个whereHas都会发生这种情
我真的需要在以下方面的帮助!我正在努力理解hasOneThrough是如何工作的。
用户表:
id = belongs_to (公司id)
公司表:
id = country_id
国家表:
id
所以我想知道通过公司用户的国家ID .在用户模型中,我正在尝试以下内容:
public function country() {
return $this->hasOneThrough(
'App\Countries',
'App\Companies',
'count
这里是我的控制器代码,以获得在联接表中应用条件的订单列表
$orders = Order::whereHas('order_carpets', function($q){
$q->where('carpent_no', '1345');
})->get();
模型代码如下所示
class Order extends Model
{
protected $table = 'orders';
public function order_carpets()
{
我有三个模特。Sponsor,Optin和Participant。Sponsor和Optin有一对多的关系,Optin有一对一的Participant.
这使得模型看起来像这样:
赞助模式
class Sponsor extends Model
{
public function optins()
{
return $this->hasMany(Optin::class);
}
}
Optin模型
class Optin extends Model
{
public function sponsor()
{
我正在做一个使用的项目,在尝试对数据进行排序时遇到了麻烦。首先,我需要按名称排序,该名称是来自'users‘表的联接列。
我的代码:
$Records = Tickets::with('User', 'Session', 'Session.Course');
$Records = $Records->orderBy('first_name', 'desc'); //generates an error as no first_name column exists in
我正在建立一个有用户的系统。用户有角色,角色有操作。我的模型设置如下:
用户
/**
* The roles that belong to the user.
*
* @return Object
*/
public function roles()
{
return $this->belongsToMany('App\Models\User\Role')->withTimestamps();
}
角色
/**
* The actions that belong to the role.
*
* @return Object
*/
publi