我想数一下属于一个标签的帖子数量。我应该使用方法还是动态属性?
<?php
class Tag extends Eloquent {
public function posts()
{
return $this->belongsToMany('Post');
}
public function postsCount()
{
return count($this->posts);
}
public function getPostsCountAttribute()
在我的Laravel应用程序中,我有一个页面,在该应用程序中,我正在获取类别,并在该类别中急切地加载事件。效果很好。我现在要做的是获取类别,但这次,根据用户选择的区域/位置获取事件。这些都是我工作的模型;
1.区域类别
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Region extends Model
{
/**
* Get events within region
*
* @return \Illuminate\Database\Eloq
我的模型有什么问题:
连接表:
id,parent_sid,connection_id (可空)
连接步骤
id,sid,date_started,date_ended,connection_id
步骤
id,名字,url
class Step extends Eloquent {
public function connections() {
return $this->hasMany("Connection", "parent_sid");
}
...}
class Connection ex
我需要获得基于belongsToMany关系的所有记录的计数。通常,我可以在模型内部的函数中使用groupBy()。但是如果我在模型函数中使用count()或withCount(),我会得到如下错误: 功能代码: public function TaskCount(){
return $this->belongsToMany(User::class)->count();
} 错误消息: Symfony\Component\Debug\Exception\FatalThrowableError: Call to a member function addEagerConst
我正在查询一个关系,以便与getNameAttribute的访问器一起使用--这是我的代码
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Conference extends Model{
public function getNameAttribute() {
return $this->getRelation('event')->name;
}
public function event() {
r
Edit:
I dont think its the same issue as:
https://stackoverflow.com/questions/40022929/laravel-relationship-error-undefined-property-illuminate-database-eloquent-col
because in that issue hasMany relationship is used which returns an array but i used belongsTo which should return a certain object.
我
我有两个模特。
用户:
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $table = 'users';
public function group ()
{
return $this->belongsTo('Group');
}
}
和集团:
class Group extends Eloquent {
protected $table = 'groups
这对我来说有点复杂,所以我决定寻求一些帮助。
分类法有:
public function products()
{
return $this->belongsToMany(
Product::class,
'term_relationships',
'term_taxonomy_id',
'object_id'
);
}
而Product拥有:
public function taxonomies(
我的应用程序构建在
Laravel: 5.6.35
PHP 7.2.4
Entrust: 1.9
我的榜样
class Role extends EntrustRole
{
public function permissions()
{
return $this->belongsToMany(Permission::class);
}
public function users()
{
return $this->hasMany(User::class);
}
}
我的用户模型是
class User
我有一个问题,读取数据与关系。返回未定义属性的一些方法: Illuminate\Database\Eloquent\Relations\HasMany::$id.我不知道我做错了什么,因为我刚刚开始使用拉拉。
模型1项目:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
protected $table = "project";
public function projectitem()
{
return $this-&
我有一个名为users的表,还有一个名为user_stats的表,stats表保存该用户的在线时间。我试图通过在user_stats中按online_time列排序来创建前10名最多在线用户的集合,但我需要传递一个users表模型的集合,以便可以调用username和motto等泛型列
用户
<?php
namespace App\Database\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Database\
目前,我正试图在数据库中的两个模型之间创建一个雄辩的关系,一个在sql中,一个在mongodb中:
<?php
namespace App\Models\Trend;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\HybridRelations;
class Location extends Model
{
use HasFactory, HybridRelations
我的数据库里有一张书的清单,每本书都有章节。我的模特是
Books.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
protected $guarded = [];
public function chapters()
{
return $this->hasMany(Chapter::class);
}
}
Chapter.php
<?php
namespace App;
use I
假设我有这样一个Conversation模型:
class Conversation extends Model
{
public function questions (){
return $this->hasMany('App\Question','conversation_id','conversation_id');
}
public function category ()
{
return $this->belongsTo('App\Category&
我一直在用Laravel框架编写一个web应用程序,并且遇到了一个问题,我正在为用户获取特定的数据。我的web应用程序的用户结构是基于一个主用户表,然后我有一个RP表为所有的用户统计数据,如健康,能源,他们拥有的项目,等等。
访问页面时,我试图在上面显示此数据,收到以下错误
Trying to get property of non-object (View: C:\workspace\mainwebsite\resources\views\frontend\home.blade.php)
O只是尝试为用户打印角色扮演表的user_id列(是的,我尝试了其他列,它们输出的结果相同)
<d
如何从两个模型连接这两个表:
Student.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $table = 'students';
}
和StudentDetails.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ProductDetails extends Model
{
p
我有以下模型:
namespace App;
use Illuminate\Database\Eloquent\Model;
class forum_category extends Model
{
//
protected $table = 'forum_category';
public function posts()
{
$this->hasMany('App\forum_post');
}
}
和
namespace App;
use Illuminate\Database
我会尽可能地怀疑托马斯。抱歉密码太长了。
这个问题将使用系列、书籍、作者、authorPicture来呈现。
系列型
/**
* The Series Model
*/
namespace My\Project\Series\Eloquent;
use Illuminate\Database\Eloquent\Model;
class Series Extends Model
{
protected $table = "series";
protected static $bookModel = "My\Project\Books\Eloqu
我有4张表,其结构和流程如下:
User
Accounts
Contacts
Orders
这种关系如下:
$user->hasMany('accounts')->hasMany('contacts')->hasMany('orders');
/** User Model **/
class User extend Eloquent {
public function accounts(){
return $this->hasMany('Accounts');
}
嗨,我正在试图查询我的数据库中表之间的关系。快速概述我的应用程序,我有users,tasks,projects,tasks,statuses和优先级。目前,我希望返回具有客户端名称和状态名称的auth用户项目,可以使用db表中的相关id检索它们。
我几乎正确地返回了所有这些信息,例如,如果我使用项目和客户端查询Auth用户,返回的数据也是正确的,那么我将在回购中进行查询,并让我的控制器使用以下内容:
public function getUserProjectswithClients()
{
return \User::with(array('projects&
我定义了与我的模型A&B的下列关系:
模型A
namespace App;
use Illuminate\Database\Eloquent\Model;
class A extends Model {
protected $table = 'list';
public function b(){
return $this->hasMany('App\B', 'id', 'id');
}
}
B型
namespace App;
use Illuminate\Database\Eloque
我的桌子
Modelos
id
鼻涕虫
marca_id
Pecas
id
编迪戈
modelo_peca
modelo_id
peca_id
produtos
id
categoria_id
编迪戈
诺姆
范畴
id
诺姆
弹状物
我有这些模型
class Modelo extends Eloquent
{
public function pecas()
{
return $this->belongsToMany('Peca');
}
}
class Peca extends Eloquent {
public funct
我正在测试我的应用程序的速度,并且我有多个hasOne关系。例如,订单有一种状态(订单待定,订单已发运等)
序模型
<?php
namespace App\Models;
use App\Models\OrderStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
protected $primaryKey = 'order_id';
publi
我有两张桌子:建筑物:
id
名字
地址
公寓:
id
building_id
unit_number
other_data
我还有两个模特:
建筑模型
class Building extends Eloquent{
protected $table = 'buildings';
public function apartments()
{
return $this->hasMany('Apartment');
}
}
公寓模式
class Apartment extends Eloquent{
我有三张桌子:入场券、机票回复和用户回复。我想得到票,从它的答复和有关答辩人的数据。我的桌子看起来是这样的:
用户:
ID | Username | Email | ...
门票:
ID | title | user_id | active | ...
Tickets_Replies:
ID | tickets_id | message | user_id | ...
它应该如何在雄辩的ORM?在这一刻我
用户:
<?php
class User extends Eloquent {
public function replies() {
return $this->
我在数据库中有四个表: groups,specialties,lessons,group_lesson。它的结构:
groups
id
specialty_id
name
specialties
id
name
lessons
id
specialty_id
group_lesson (UNIQUE INDEX lesson_id, group_id)
lesson_id
group_id
date
我的模型现在看起来是这样的:
class Group extends Eloquent {
public f
我所知道的:
$this->$parent->childs(); //we get childs data
我想知道的是:
$this->child->find($id)->parent(); //how to get childs parent without including model in controller | by just using eloquent
下面是我的employee和employeeDependent模型的示例代码:
trait EmployeeRelationships{
public function dependen
我有一个导航模型,它可以有许多与其相关的项目:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use JetBrains\PhpStorm\ArrayShape;
use Laravel\Scout\Searchable;
class Navigation extends Model
我试图将一个雄辩的关系分成这样的页码:
$query = Product::find(1)->options()->paginate();
但我得到以下错误:
Fatal error: Call to a member function getCurrentPage() on a non-object
我已经确认代码$query = Product::find(1)->options()返回一个选项集合。$query对象似乎属于hasMany类型。下面是我正在使用的模型类。
class Product extends Eloquent
{
protected $t
我有这三张桌子和它们的栏:
users
id first_name ...
reports
id user_id title ...
entries
id report_id value
这个查询提供了我想要的内容,它是特定用户的所有条目:
SELECT entries.value from entries JOIN
reports ON (entries.report_id = reports.id) where reports.user_id = 2
我怎么能用Laravel的雄辩的ORM呢?
编辑:
我有这样的模型:
cla
我正试图为我们正在开发的新API建立雄辩的基础。我在一个模型中使用关系。
有些关系很复杂,并不适合快速链式查询生成器语句。例如,我试图返回度量,其中一些度量是复杂的。例如,计算用户生成的总点击量(它不仅仅是一个简单的COUNT(*))。下面是我现在的代码:
<?php
namespace App\Models\Eloquent;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class Af
我使用的是Laravel5.3,这个模型:
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Storage;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use SoftDeletes;
protected $table = 'categories';
protected $fillable = [