我有一个名为'posts'的表,其中包含以下列:'post_id int primary incrementments','poster_id int'和'status text'以及名为friends的数组,其中包含以下列:'user_id int primary'和'friend_ids text'.
我需要获取朋友文本列中的所有ID,这很容易使用:
$friends = explode(',', \Friend::where('user_id', \Sentry::getUser()->id)->first()->friend_ids);
Run Code Online (Sandbox Code Playgroud)
文本列中的数据看起来像'1,2,3'等.
然后我创建一个Eloquent Collection对象,也可以通过以下方式轻松完成:
$posts = new \Illuminate\Database\Eloquent\Collection();
Run Code Online (Sandbox Code Playgroud)
但问题是我无法弄清楚如何填充集合并通过Post对象的'created_at'列对其内容进行排序.
这就是我现在所拥有的:
foreach ($friends as $id) {
$posts_ = \Post::where('poster_id', $id)->getQuery()
->orderBy('created_at', 'desc')
->get();
foreach($posts_ as $post) {
$posts->add($post);
}
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚这个代码是否适用于通过'created_at'列对整个帖子集合进行排序.我还需要能够轻松地对整个集合进行分页.
对集合进行排序的推荐方法是什么?
我正在尝试获取所有模型的关联数组.我有以下型号:
class Article extends Eloquent
{
protected $guarded = array();
public static $rules = array();
public function author()
{
return $this->belongsTo('Author');
}
public function category()
{
return $this->belongsTo('Category');
}
}
Run Code Online (Sandbox Code Playgroud)
从这个模型,我试图获得以下关系数组:
array(
'author',
'category'
)
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种方法从模型中自动拉出这个数组.
我发现这对雄辩的模型,这似乎回归模型的关系阵列的relationsToArray方法的定义.它似乎使用了Eloquent模型的$ this-> relations属性.但是,此方法返回一个空数组,而relations属性是一个空数组,尽管我的关系设置正确.
什么是$ this->如果不存储模型关系所使用的关系?有什么方法可以自动获取模型关系的数组吗?
我有以下Eloquent模型与关系:
class Lead extends Model
{
public function contacts()
{
return $this->belongsToMany('App\Contact')
->withPivot('is_primary');
}
}
class Contact extends Model
{
public function leads()
{
return $this->belongsToMany('App\Lead')
->withPivot('is_primary');
}
}
Run Code Online (Sandbox Code Playgroud)
数据透视表包含一个额外的param(is_primary),用于将关系标记为主要关系.目前,当我查询联系人时,我看到这样的返回:
{
"id": 565,
"leads": [
{
"id": 349,
"pivot": {
"contact_id": "565",
"lead_id": "349",
"is_primary": "0"
}
}
]
}
Run Code Online (Sandbox Code Playgroud)
有没有办法将is_primary其转换为布尔值?我已经尝试将它添加到$casts两个模型的数组中,但这并没有改变任何东西.
我刚刚开始使用Laravel所以请原谅任何noobness.
我有一个User和Order模型,一个用户有很多订单:
# Inside User model
public function orders()
{
$this->hasMany('Order');
}
# Inside Order
public function user()
{
return $this->belongsTo('User');
}
// Not sure if this is upsetting anything (also in Order)
public function products()
{
return $this->belongsToMany('Product');
}
Run Code Online (Sandbox Code Playgroud)
所以我认为我有上述权利.
但是当我这样做时:
$users = User::with('orders')->find(1);
return $users;
Run Code Online (Sandbox Code Playgroud)
我得到Call to a member function addEagerConstraints() on null.
但是,如果我以相反的方式做到这一点,那么效果很好:
$orders = Order::with('User')->get();
return $orders;
Run Code Online (Sandbox Code Playgroud)
我做错了什么/我不懂什么?!或者我的问题比我想象的还要大?
数据库:

我不会在雄辩的模型中搜索不区分大小写的代码。现在我习惯了
Model::where($column, 'LIKE', '%' . $value . '%' );
Run Code Online (Sandbox Code Playgroud)
但这是区分大小写的。我该如何解决?
我也可以找到此帖子,如何使用LIKE通配符在列中搜索(不区分大小写)?但我不能在雄辩的模型中使用它
我有这种多对多关系 Laravel Eloquent 关系模型是:
class Email extends Model //actually represent the email account
{
protected $table = 'emails';
protected $fillable = [
'user_id',
'name',
];
public function messages() {
return $this->belongsToMany(Message::class)->withPivot('email_subtype_id');
}
}
class Message extends Model //actually represent the email message
{
protected $table = 'messages';
protected $fillable = [
'subject',
'body ',
];
public function emails() {
return $this->belongsToMany(Email::class)->withPivot('email_subtype_id');
}
}
class EmailMessage extends Pivot //actually represent the pivot table
{
protected $table = 'email_message';
protected $fillable = …Run Code Online (Sandbox Code Playgroud) 我的数据库中有 2 个表。
books 和 ratings
在books
id,name
在 ratings
id, book_id,rating
我已经为这些模型设置了很多关系。
所以在Book模型中 -
public function ratings()
{
return $this->hasMany('App\Rating');
}
Run Code Online (Sandbox Code Playgroud)
在Rating模型中 -
public function book()
{
return $this->belongsTo('App\Book');
}
Run Code Online (Sandbox Code Playgroud)
现在我想获取所有具有平均评分但按高评分排序的书籍。
所以我先给书评分高,然后评分低。
那么我如何加入 2 个表来实现这个结果。
谢谢。
我在刀片模板中有这个集合过滤器,我只想显示值字段。实际上,我得到了过滤后的数组。
{{ $room->products->filter( function($value, $key){ if( $value['product_id'] == 71 ){ return $value['value']; } }) }}
Run Code Online (Sandbox Code Playgroud)
输出:
[{"id":10,"product_id":71,"room_id":2,"value":"3896"}]
Run Code Online (Sandbox Code Playgroud)
我只想显示值字段
我正在使用Laravel雄辩的模型。我想添加一个查询条件where start_time later than now。
例如:
Model::whereAfterNow('start_time')->all()
Run Code Online (Sandbox Code Playgroud)
你能帮助我吗??
我有日历模型。我不会插入大量数据。
$data = [
[
'weekday' => 1,
'status' => 'active'
]
.......
];
$calendar->insert($data)
Run Code Online (Sandbox Code Playgroud)
但是在db created_at 中,updated_at 为null 为什么??
laravel ×12
eloquent ×10
php ×7
collections ×2
laravel-5 ×2
composer-php ×1
filter ×1
laravel-4 ×1
laravel-5.3 ×1
linux ×1
mysql ×1
pivot ×1
web-services ×1