我正在使用Laravel.我刚刚遇到Carbon供应商文件夹中已有的内容.我做了一些快速搜索,发现它是PHP日期时间的一个很好的扩展.
在我的应用程序的某个地方,我需要一个逻辑来检查给定时间是否落在两个时间点之间.让我们说,我想检查10:00是否落在时间9:55和10:05之间.当然,它会下降,但我应该如何在Carbon的帮助下使用这种逻辑.
默认情况下,Carbon::now()将以2014-12-02 14:37:18格式返回日期和时间.我在想是否只能提取时间部分,14:37:18然后我可以比较两次,以了解测试时间是否属于两个时间点.
如果我直接检查两个Carbon对象,它也会尝试检查年份.但我所需要的只是时间部分.
事实上,我甚至不确定如果时间(h:m:s)可以直接通过碳进行比较.
我读过Dayle Rees的Code Bright,了解更多关于CollectionLaravel中使用的Eloquent的信息.做了一些其他的研究,但找不到我想要的答案.
我想将一个对象(Model类型对象)插入到Collection特定位置的Object中.
例如:
这是返回的集合
Illuminate\Database\Eloquent\Collection Object
(
[0] => Attendance Object
([present_day] => 1)
[1] => Attendance Object
([present_day] => 2)
[2] => Attendance Object
([present_day] => 4)
[3] => Attendance Object
([present_day] => 5)
)
Run Code Online (Sandbox Code Playgroud)
正如你可以看到上面[present_day]有一个值范围为1 to 5,但价值,3在序列中丢失.现在,我真正想要做的是,我想明确地Attendance Object在Collection Object的[2]索引号/位置的位置放置一个新的,因此通过推送其余的Attendance Object.我真的很难做到这一点.我怎么能这样做使上面的集合对象看起来像下面的东西:
Illuminate\Database\Eloquent\Collection Object
(
[0] => Attendance Object
([present_day] => 1)
[1] => Attendance Object
([present_day] => 2)
[2] …Run Code Online (Sandbox Code Playgroud) 我有点困惑,在代码点火器中使用库和助手的方法.我还在学习代码点火器.
CONTROLLER
function index(){
$this->load->helper('text');
$this->load->library('auth'); //custom library
$data['string'] = 'this is sample ..... this is sample';
$this->load->view('article', $data);
}
Run Code Online (Sandbox Code Playgroud)
视图
<?php
if(is_logged_in()){ //is_logged_in() is the method from the library, 'auth'
echo 'You are logged in';
}
<p><?php echo word_limiter($string, 10); ?></p> <!--word_limiter() is the method from the helper, 'text' -->
Run Code Online (Sandbox Code Playgroud)
在上面的视图文件中,帮助方法word_limiter()工作正常.但该方法is_logged_in()不起作用.但如果我这样做($this->auth->is_logged_in()),它会起作用.
但是为什么来自helper的方法word_limiter()不必像这样写($this->text->word_limiter()).
调用helper和library的方法有什么区别吗?
我正在测试是否成功登录.为此,我正在检查,
为此,我controller看起来像这样
public function loginPost(){
if (Auth::attempt(array(
'email' => Input::get('email'),
'password' => Input::get('password')
))){
return Redirect::intended(route('dashboard'));
}
return Redirect::route('login')
->withInput()
->with('errorMessage', 'Failed');
}
Run Code Online (Sandbox Code Playgroud)
我test看起来像这样
public function testLoginSuccess(){
$input = [
'email' => 'xyz@gmail.com',
'password' => 'computer'
];
Input::replace($input);
Auth::shouldReceive('attempt')
->with($input)
->once()
->andReturn(true);
$this->call('POST', 'login', $input);
$this->assertRedirectedToRoute('dashboard');
}
Run Code Online (Sandbox Code Playgroud)
虽然这适用于浏览器.但在测试时,它失败并显示以下消息:
BadMethodCallException:方法Mockery_0_Illuminate_Auth_AuthManager :: check()在此模拟对象上不存在
我只是这个涉及websocket,Ratchet和ZeroMQ的整个领域的初学者.
基本了解:
websocket 是有助于在服务器和客户端之间创建开放连接的东西.
Ratchet 是一个基于PHP的库,它使用PHP的核心Socket函数来创建一个PHP套接字框架,允许我们简化PHP套接字编程.
ZeroMQ 是一个套接字库,可以帮助非棘轮应用程序(其他PHP脚本)通过Ratchet Socket和web-socket发送数据.
我正在按照棘轮中关于'hello world'和'pusher'的教程进行操作,但它们似乎都是不完整的,只讲授如何使用控制台.我也在github中找到了棘轮示例,但没有正确记录.我正在寻找一个完整的例子(带有专用的html页面和javascript)
下面是我正在处理的代码:这是我正在进行Ajax请求的控制器的方法之一.这个方法会创建一个新帖子(比方说).我想在ZeroMq的帮助下通过广播/推送在多个客户端的浏览器中动态更新帖子列表.
控制器中的方法:
public function create_new_post(){
// ------
// code to create a new post.
// -------
// After creating a post
$response = [
'new_post_title' => $title,
'post_id' => $id
];
$context = new ZMQContext();
$socket = $context->getSocket(ZMQ::SOCKET_PUSH, 'my pusher');
$socket->connect("tcp://localhost:8000");
$socket->send(json_encode($response));
}
Run Code Online (Sandbox Code Playgroud)
推送文件:
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\WampServerInterface;
class Pusher implements WampServerInterface{
public function onPostEntry($data){
// Data that were sent by ZeroMQ through create_new_post() method
$entry_data = json_decode($data);
// …Run Code Online (Sandbox Code Playgroud) 我创建了一个名为Author的模型.我试着在eloquent create方法的帮助下创建一个模型,如下所示:
public function postCreate(){
Author::create(array(
'user' => Input::get('user'),
'info' => Input::get('info')
));
return Redirect::to('authors')
->with('message', 'User created successfully');
}
Run Code Online (Sandbox Code Playgroud)
'user'和'info'是表单元素的名称.我确信我并没有误解错字.当我运行它时,不会创建模型并显示MassAssignmentException.
但是,当我尝试使用以下方法时,模型已创建并保存在表中
public function postCreate(){
$author = new Author;
$author->name = Input::get('user');
$author->info= Input::get('info');
$author->save();
return Redirect::to('authors')
->with('message', 'User created successfully');
}
Run Code Online (Sandbox Code Playgroud)
我真的想要使用create方法,它看起来更干净,更简单.
我试图计算给定月份有多少个周末(周六和周日)。这将允许我减去这些周末以获得我的应用程序所需的实际工作日数。
我现在正在做的是,首先我像这样创建了 Carbon 对象:
$thisDate = Carbon::createFromDate(2014, 12, 2);
然后,我在这样的 Carbon 对象的帮助下计算了一个月中落在上述日期内的天数
$daysInMonth = $thisDate->daysInMonth;
现在,我得到了一个月的天数。可以通过循环该月的每一天并检查它是否是周末(类似于 CarbonObject->isWeekday)并增加计数器来找到周末的数量,但它看起来很混乱。
有没有更优雅的方法来做到这一点?我的意思是 CarbonObject 中是否有我遗漏的更好的东西,它可以以更简单的方式给出结果?
注意:我正在 Laravel4.2 的帮助下创建我的应用程序
我也读过这个:(但想知道是否有碳方式解决方案) 获取给定月份的工作日数
php ×5
laravel ×4
laravel-4 ×4
datetime ×2
eloquent ×2
ajax ×1
codeigniter ×1
collections ×1
javascript ×1
ratchet ×1
sockets ×1
unit-testing ×1