我想显示按日期分组的照片。它可以工作,但只能使用两个查询并且没有分页。有没有一种方法可以使用 laravel paginate() 仅使用一个查询来完成此操作?我需要最近 5 天的数据(仅限那些有图片的数据),然后每页始终需要 5 天的数据。
问题是,我想对日子进行分页,而不是对图片进行分页。
$days = Pic::select(DB::raw('DATE(created_at) as datum'))
->distinct()
->orderBy('datum','desc')
->take(5)
->get();
$daysarray = array();
foreach($days as $day)
{
$pics = Pic::where(DB::raw('DATE(created_at)'),'=',$day->datum)
->orderBy('id','desc')
->get();
$daysarray[$day->datum] = $pics;
}
Run Code Online (Sandbox Code Playgroud) 当我使用运行应用程序时,每个路由都工作正常php artisan serve,但是当我尝试在 nginx 上运行它时,所有路由都工作正常,除了/json.json。它显示以下错误。
Constant LARAVEL_START already defined
Run Code Online (Sandbox Code Playgroud)
这是routes.php
<?php
Route::get('/', function()
{
return View::make('hello');
});
Route::get('json.json', 'HomeController@temp');
Route::post('anotheroute', 'HomeController@p');
});
Run Code Online (Sandbox Code Playgroud)
和 HomeController.php
<?php
include 'index.php' ;
class HomeController extends BaseController {
public function showWelcome()
{
return View::make('hello');
}
public function temp()
{
header('Access-Control-Allow-Origin: *');
$x = select_artist_by_name();
return Response::json(array_keys($x));
}
Run Code Online (Sandbox Code Playgroud) 我有一个基本的调查生成器,如果用户指定问题需要从列表(下拉列表)中选择一个答案,那么每个可能的答案都需要保存在数据库中。
因此,我的视图有一个文本区域,管理员可以输入可用的答案并用换行符分隔它们。我需要提取该文本(逐行)并保存在数组中,以便我的模型可以为每个项目创建一行。
我唯一的问题是如何分隔每一行并保存为数组。该视图实际上是一个弹出窗口,数据是使用 Ajax 发送的,因此最简单的方法是将整个值作为一个输入发送到控制器,然后在 PHP 中将其分开。我想知道 Laravel 是否有为此准备的函数,或者我是否应该使用标准 PHP,例如 str_replace 等......
因此,我的表单具有与字段不同的别名。但 Laravel 使用我的别名作为列名。我如何告诉 laravel 它只是一个别名,并提供另一个字段来代替。
我的代码:
$rules = array(
'email1' => 'required|email|unique:users',
);
Run Code Online (Sandbox Code Playgroud) entity我有一个带有表和表的数据库users。实体表具有created_at和 的字段updated_at。users 表有一个timezone默认值为 的字段America/Toronto。
在我看来,我可以使用以下代码以我想要的格式输出日期:
$entity->created_at->format('M j, Y \\a\\t g:i A')
Run Code Online (Sandbox Code Playgroud)
现在,如何使用用户表中的时区字段输出日期?就像是 ...
$entity->created_at->format('M j, Y \\a\\t g:i A', Auth::user()->timezone);
Run Code Online (Sandbox Code Playgroud)
我正在寻找一种使用Carbon执行此操作的简单方法(因为我在 Laravel 中使用 Eloquent,它将把上面的日期列转换为 Carbon 的实例)。
注意:我只希望在显示日期时更改时区 - 我仍然希望所有日期都存储在数据库中的默认 UTC 时区中。
另外,我在哪里可以找到可用于 Carbon 的有效时区列表?我想将这些存储在我的数据库中,以便用户可以更改他们的默认时区。
我有一个过滤器。我希望此过滤器仅在页面显示 HTML 时起作用,而不是在 json 或任何其他格式时起作用
App::after(function($request, $response)
{
if( $request->getMethod() == 'GET' && $request->getRequestFormat() == 'html' ) {
// do something!
}
});
Run Code Online (Sandbox Code Playgroud)
在我的控制器函数中,我返回 json 数据:
return Response::json($data);
Run Code Online (Sandbox Code Playgroud)
但是,$request->getRequestFormat()仍然等于“html”,但它不应该等于。
我知道我可以将格式设置为“json”,如下所示:
Request::setRequestFormat('json');
return Response::json($data);
Run Code Online (Sandbox Code Playgroud)
但这似乎是多余的。如果我返回 aResponse::json它应该知道它是 json 而不是 HTML。我怎样才能检测到它是一个Response::json?
这些是我的模型
class DateAttribute extends Eloquent{
public function xmlDocument(){
return $this->belongsTo('XMLDocument');
}
}
class XMLDocument extends Eloquent{
public function dateAttribute(){
return $this->hasOne('DateAttribute');
}
}
Run Code Online (Sandbox Code Playgroud)
该XMLDocument 模型具有模型之一DateAttribute。我可以成功插入到两个表中。
现在我正在尝试读取特定的 xml 文档。换句话说,我正在尝试查看该 xml 文档。
我在我看来尝试过这个:
<td>{{$xmlDocument->dateAttribute->name}}</td>
I got this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'date_attribute.x_ml_document_id' in 'where clause' (SQL: select * from `date_attribute` where `date_attribute`.`x_ml_document_id` = 10 limit 1)
Run Code Online (Sandbox Code Playgroud)
我不知道为什么会发生这种情况,我有很多很多关系,而且所有关系都正常,只是这个不起作用
请注意,错误消息中的和之间x_ml_document_id有一个,我搜索了所有代码,根本没有这个词。underscorexml
请帮助
我有一条路线:resetPassword,它接受两个参数:code 和 userid。代码和用户 ID 通过 get String 接受。在此路线上,我呈现一个表单来接受新密码(输入两次)。所以网址看起来像:/resetPasssword?code=asfsanj38uan&user=14
我希望此页面上的表单回发到/resetPasssword?code=asfsanj38uan&user=14而不是发回/resetPassword。
我尝试过Form::open("url"=>Request::url())将 url 留空,但在这两种情况下,它都会删除额外的参数。
我的数据透视表中有一个附加列,我需要访问它。
架构:
Schema::create('alert_criteria', function(Blueprint $table)
{
$table->increments('id');
$table->integer('alert_id')->unsigned()->index();
$table->foreign('alert_id')->references('id')->on('alerts')->onDelete('cascade');
$table->integer('criteria_id')->unsigned()->index();
$table->foreign('criteria_id')->references('id')->on('criterias')->onDelete('cascade');
$table->integer('viewed');
$table->timestamps();
});
Run Code Online (Sandbox Code Playgroud)
标准模态
public function alerts()
{
return $this->belongsToMany('Alert')->withPivot('viewed')->withTimestamps();
}
Run Code Online (Sandbox Code Playgroud)
控制器
public function getMatches()
{
$matches = Criteria::find(Auth::user()->id)
->alerts()
->get();
}
Run Code Online (Sandbox Code Playgroud)
看法:
@foreach($matches as $match)
<td>{{$match->viewed}}</td>
@endforeach
Run Code Online (Sandbox Code Playgroud)
该视图不会返回错误,但它只是不显示任何内容。“已查看”列只有 1 或 0。
提前谢谢了。
最近我看到《Laravel 4 From Apprentice to Artisan》一书中的界面,示例如下:
interface UserRepositoryInterface {
public function all();
}
class DbUserRepository implements UserRepositoryInterface {
public function all()
{
return User::all()->toArray();
}
}
Run Code Online (Sandbox Code Playgroud)
什么是接口?接口文件放在哪里?
laravel-4 ×10
laravel ×7
php ×5
eloquent ×1
filter ×1
group-by ×1
mysql ×1
pagination ×1
php-carbon ×1
pivot-table ×1
request ×1
response ×1
sql ×1
textarea ×1
timezone ×1