如何在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.
相关问答
更多-
更改行input.value = "2010" 至 input['value'] = "2010" type , name等是input元素的属性。 您需要使用Nokogiri::XML::Node#[]=方法。 阅读[]=(name, value)的文档 将属性名称的属性值设置为value 示例: require 'nokogiri' doc = Nokogiri::XML <<-xml
test xml doc.at('foo')['atr'] # ... -
安迪,你真棒! 您的代码帮助我使我的脚本可行并登录到Google帐户。 几个小时后我发现了你的错误。它是关于html转义的。 正如我发现的那样,Mechanize会自动转义它作为'get'方法的参数。 所以我的解决方案是: EMAIL = ".." PASSWD = ".." agent = Mechanize.new{ |a| a.log = Logger.new("mech.log")} agent.user_agent_alias = 'Linux Mozilla' agent.open_timeo ...
-
看看这里有什么点击 。 它调用get ,但首先设置引用并进行一些机器人检查。 The problem turned out not to be a difference between Link#click and Agent#get, but the server had changed its response in certain situations. In other words, my assumptions were wrong.
-
使用来自控制器的Mechanize代理进入模型方法(Having Mechanize agent from controller follow into model method)[2023-09-11]
最后想出了这一点,部分得到了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 ou ... -
您需要在您的网址前加上$dir 。 而不是使用follow_link()您指定URL的follow_link() ,只需使用另一个get() : $mech->get( "file:$dir/" . $link->url() ); You need to prepend $dir to your URL. Instead of using follow_link(), which won't let you specify the URL, simply use another get(): $mech- ...
-
yield cookie if block_given?看起来原始的解析方法有一个yield cookie if block_given? 声明。 你也需要能够传递一个块。 编辑: 更清楚...... class Foo def self.x yield "yielded from x!" if block_given? end end class Foo class <好, 问题是该网站禁用了Mechanize用户代理。 我刚将其更改为: mechanize.user_agent_alias = 'Windows Chrome' Ok, The problème was that the website disable the Mechanize user agent. I just changed it to : mechanize.user_agent_alias = 'Windows Chrome'我不确定你为什么认为使用Net :: HTTP会更好。 Mechanize将处理重定向和cookie,并提供对Nokogiri解析文档的随时访问。 require 'mechanize' agent = Mechanize.new page = agent.get('http://www.example.com') # Use Nokogiri to find the content of the
tag... puts page.at('h1').content # => "Example ...
对于Mechanize,应将SSL验证为none agent = Mechanize.new agent.verify_mode = OpenSSL::SSL::VERIFY_NONE 对于HTTParty,有verify:选项请参阅此问题如何使HTTParty忽略SSL? 如果你想一般地设置它,使用这个脏技巧: OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE For Mechanize this should set verify SSL t ...您应该获取Charles( http://www.charlesproxy.com/ )的副本,或者允许您查看从浏览器提交表单时发生的情况。 无论如何,你的问题是这部分: agent.page.forms[0]["lookup_text"] = "VG278H" agent.page.forms[0].submit 正在返回一个看起来像这样的html片段: