如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
我试图通过使用关系从数据库中选择数据时使用
paginate
。 我有三个表,即users
,contacts
和contact_messages
,分别是User
,Contact
,ContactMessage
。我试图通过使用以下方法获取特定用户的所有消息:
public function listMessage($user_id){ $user1 = User::find($user_id); $messages = $user1->contact_messages; return View::make('message.listmessage') ->with('user_id', $user_id) ->with('messages', $messages); }
通过这种方法,我收到所有消息,但我无法对
$messages
分页。 我如何分页$messages
? 有人可以帮忙吗?我的模型和关系如下:
class User extends Authenticatable { public function contacts() { return $this->hasMany('App\Contact'); } public function contact_messages() { return $this->hasManyThrough('App\ContactMessage', 'App\Contact'); } }
和联系模式是
class Contact extends Model { public function user(){ return $this->belongsTo('App\User'); } public function messages() { return $this->hasMany('App\ContactMessage'); } }
最后ContactMessage模型是
class ContactMessage extends Model { public function contact(){ return $this->belongsTo('App\Contact'); } }
I am trying to use
paginate
while selecting data from database by using relationships. I have three tables namely,users
,contacts
andcontact_messages
with modelsUser
,Contact
,ContactMessage
respectively.I am trying to fetch all messages of a particular user by using the following method :
public function listMessage($user_id){ $user1 = User::find($user_id); $messages = $user1->contact_messages; return View::make('message.listmessage') ->with('user_id', $user_id) ->with('messages', $messages); }
By this method I am getting all messages, but I couldn't paginate
$messages
. How can I paginate$messages
? Can anyone help?My models and relationships are given below :
class User extends Authenticatable { public function contacts() { return $this->hasMany('App\Contact'); } public function contact_messages() { return $this->hasManyThrough('App\ContactMessage', 'App\Contact'); } }
and Contact Model is
class Contact extends Model { public function user(){ return $this->belongsTo('App\User'); } public function messages() { return $this->hasMany('App\ContactMessage'); } }
finally ContactMessage model is
class ContactMessage extends Model { public function contact(){ return $this->belongsTo('App\Contact'); } }
原文:https://stackoverflow.com/questions/38741266
满意答案
最后想出了这一点,部分得到了Adam 在这里的回答。
在我的控制器的
show
动作中,我将块更改为:@order.each do |order| order.fillforms(order.id, agent) end
在
Order
模型中,我将fillforms
方法的前两行更改为:def fillforms(order_id, agent) order = Order.find(order_id)
现在,Mechanize进入块,因此块正确执行。
Finally figured this out, helped in part by Adam's answer here.
In my controller's
show
action, I changed the block to:@order.each do |order| order.fillforms(order.id, agent) end
And in the
Order
model, I changed the first two lines of thefillforms
method to:def fillforms(order_id, agent) order = Order.find(order_id)
Now Mechanize follows into the block, and the block consequently executes properly.
相关问答
更多如何使用Mechanize填充dom节点输入的值?(How do I fill the value of a dom node input with Mechanize?)
在Google文档中使用Mechanize(Using Mechanize with Google Docs)
Mechanize Link#click vs Agent#get(Mechanize Link#click vs Agent#get)
使用来自控制器的Mechanize代理进入模型方法(Having Mechanize agent from controller follow into model method)
WWW :: Mechanize关注链接无法找到链接(WWW::Mechanize follow link can't find link)
class << self,alias_method和monkey patching Mechanize :: Cookie(class << self, alias_method, and monkey patching Mechanize::Cookie)
Ruby Mechanize 404 => Net :: HTTPNotFound(Ruby Mechanize 404 => Net::HTTPNotFound)
Ruby Mechanize,Nokogiri和Net :: HTTP(Ruby Mechanize, Nokogiri and Net::HTTP)
Mechanize和HTTParty中的“主机名与服务器证书不匹配”错误(“Hostname does not match the server certificate” error in Mechanize and HTTParty)
为什么Mechanize没有关注链接(Why is Mechanize not following the link)
相关文章
更多LARAVEL学习--安装
微信5.2
微信5.2
CentOS5.2下使用Ganglia对Hadoop进行监控
RHEL 5.2下的Hadoop分布式集群环境搭建文档
【第五章】Spring表达式语言 之 5.1 概述 5.2 SpEL基础 ——跟我学spring3
如何在rails中使用支付宝
如何在jsf标签中使用java的枚举类型
如何在 ListView 中显示 RadioButton???
如何在SOLR中嵌入自己的分词系统??
最新问答
更多获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
如何通过引用返回对象?(How is returning an object by reference possible?)
矩阵如何存储在内存中?(How are matrices stored in memory?)
每个请求的Java新会话?(Java New Session For Each Request?)
css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
xcode语法颜色编码解释?(xcode syntax color coding explained?)
在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
Copyright ©2023 peixunduo.com All Rights Reserved.粤ICP备14003112号
本站部分内容来源于互联网,仅供学习和参考使用,请莫用于商业用途。如有侵犯你的版权,请联系我们(neng862121861#163.com),本站将尽快处理。谢谢合作!