我想数一下属于一个标签的帖子数量。我应该使用方法还是动态属性?
<?php
class Tag extends Eloquent {
public function posts()
{
return $this->belongsToMany('Post');
}
public function postsCount()
{
return count($this->posts);
}
public function getPostsCountAttribute()
我有两个模特。
用户:
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $table = 'users';
public function group ()
{
return $this->belongsTo('Group');
}
}
和集团:
class Group extends Eloquent {
protected $table = 'groups
我定义了与我的模型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
我有一个模型,它属于另一个模型,这个模型属于第三个模型,我想要一个雄辩的方法来将第一个模型与第三个模型联系起来。
不过,似乎没有belongsToThrough (或hasOneThrough)方法。我已经尝试过链接多个belongsTo方法,但没有成功(Call to undefined method Illuminate\Database\Query\Builder::belongsTo())。有什么想法吗?
以下是这些模型的一个示例:
// The first model
// Schema: this model has a middle_id column in the databa
我一直在用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
我的模型有什么问题:
连接表:
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
我有一个问题,读取数据与关系。返回未定义属性的一些方法: 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-&
这是我的advertisements表:
这是我的works表:
我想从advertisements表中找到数据,该表在12小时前被add_id列从works表中记录下来。
工作模式:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Work extends Model
{
}
广告模式:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Advertisement extends M
我正在测试我的应用程序的速度,并且我有多个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
下面是这些关系:
评论模型
class Comment extends Model
{
/**
*
* The comment belongs to the post
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function post()
{
return $this->belongsTo('App\Post', 'post_id')->with('
在我的Laravel应用程序中,我有一个页面,在该应用程序中,我正在获取类别,并在该类别中急切地加载事件。效果很好。我现在要做的是获取类别,但这次,根据用户选择的区域/位置获取事件。这些都是我工作的模型;
1.区域类别
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Region extends Model
{
/**
* Get events within region
*
* @return \Illuminate\Database\Eloq
我试图在Laravel4.2中加载一个动态范围。
我有以下几门雄辩的课程:
class Project extends Eloquent {
protected $table = 'projects';
public function subProjects() {
return $this->hasMany('SubProject');
}
}
在Subproject类上,我有以下动态作用域:
public function scopeForUserInPeriod($query, $user, $interv
我想创建一个简单的关系,用于查找数据库中的用户配置文件信息与User模型。
在User.php中,我有:
class User extends Eloquent implements UserInterface, RemindableInterface {
...
public function profile() {
return $this->hasOne('UserProfile');
}
...
}
在model/UserProfile.php中:
class UserProfile extends Eloquent
我所知道的:
$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
我有这三张桌子和它们的栏:
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
目前,我正试图在数据库中的两个模型之间创建一个雄辩的关系,一个在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
这里是一个初学者的问题,我怎样才能得到与信息有关系的建筑物表上的building_name列,我想在显示函数中访问它,并在show.views上显示它?我还想知道怎样才能叫它在秀中,请帮忙。
<?php
namespace App\Http\Controllers;
use App\Information;
use App\Building;
use Illuminate\Http\Request;
use Session;
class InformationController extends Controller
{
/**
* Display a listing of th
我使用的是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 = [
我所要做的就是弄清楚我到底应该在什么时候使用hasOne(),什么时候应该使用belongsTo()。两者在我看来都是一样的。例如,以下是我的模型:
use Illuminate\Database\Eloquent\Model;
use App\Categories;
use App\User;
class tickets extends Model
{
protected $table = "tickets";
public function category()
{
return $this->hasOne(Categor
TLDR:当SoftDeletes特征包含在父模型中时,我不再将父模型的软删除实例作为子模型的动态属性。这是如何做到的呢?
我定义了几个基本模型,如下所示:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Builder;
class User extends Model
{
use SoftDeletes;
public functi
我有三张桌子。
表1:posts //存储用户的twitter帖子
表2:tags //存储twitter帖子中使用的标记
表3:post_tag //枢轴表
现在,我想要的是输入一个tag,例如'yolo‘,得到所有有这个标签的帖子。这很容易,我就这样做了
职类职位:
/**
* Get the tags associated with the post
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function tags()
{
return $this->b
我很难找到我在这里做错了什么。我有两个表:表1: State (id,name)表2: City (id,name,state_id)
一个州有许多城市,一个城市属于一个州。
我宣布这些模型如下:
class City extends Model
{
protected $connection = 'pgsql';
protected $table = 'city';
public function state(){
return $this->belongsTo('App\State');
我有一个名为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\
我的应用程序构建在
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
我有属于IssueCat模型的ContactForm模型。这种关系在模型中定义如下:
class ContactForm extends \Eloquent {
// Add your validation rules here
public static $rules = [
// 'title' => 'required'
];
// Don't forget to fill this array
protected $fillable = [];
public functi
我刚接触过laravel,并做了一个基本的应用程序来掌握关系。我实现了一对一的关系,希望从基表和相关表中获得特定的列。我有两个表,即用户,identity_cards.Relationship定义如下
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*
我有以下模型:
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
下面是我设置的内容。我有两个表:users和todos。每个用户可以有多个“待办事项”。
下面是表格的样子:
用户:
待办事项:
和模型:
class User extends Eloquent
{
public function todo() {
$this->has_many('todo');
}
}
还有..。
class Todo extends Eloquent
{
public function user() {
$this->belongs_to('user');
我有三张桌子:入场券、机票回复和用户回复。我想得到票,从它的答复和有关答辩人的数据。我的桌子看起来是这样的:
用户:
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->