我正在尝试在 Laravel 控制器中使用非 Laravel composer 包。我已将项目添加到 composer.json 文件中,如下所示:
"require": {
"laravel/framework": "5.0.*",
"php": ">=5.4.0",
"abraham/twitteroauth": "0.5.2"
},
Run Code Online (Sandbox Code Playgroud)
然后我跑了:
composer update
Run Code Online (Sandbox Code Playgroud)
在项目中,它已按预期将软件包安装在 vendor/ 目录中,我在那里看到了它。但是,当将以下代码添加到控制器时:
<?php
namespace App\Http\Controllers;
class HomeController extends Controller {
use Abraham\TwitterOAuth\TwitterOAuth;
public function index()
{
$o = new TwitterOauth();
return view('home');
}
Run Code Online (Sandbox Code Playgroud)
Laravel 返回以下错误:
未找到特性“App\Http\Controllers\Abraham\TwitterOAuth\TwitterOAuth”
我怀疑这与命名空间已经声明的事实有关,但我对 PHP 命名空间的了解不足以解决这个问题。
欢迎任何帮助!
我有一个'X'数量的变量(可能在3到20个选项之间),它们将被组合起来计算所有可能的组合以满足标准.对于每个额外的变量,引入了一个额外的循环,但我不知道是否可以动态创建循环(在excel VBA中,代码不必非常快).
为了证明:我有var.a,h = 2,var.B,h = 3.我想知道所有等于10的组合或2个变量的最佳组合.
在这种情况下:选项1 = 5*A = 10,3*B = 9,2*A和2*B = 10,3*A和1*B = 9.
代码如下所示:
For A = 0 to 5
h = 0 'Reset previous h if solution is found
For B = 0 to 5
h_test = A * height(A) + B * heigth(B)
if h_test > 10
if h = 0 then
exit for
else
write h
exit for
end if
h = h_test
Next B
Next A
Run Code Online (Sandbox Code Playgroud)
如果引入了另一个参数(例如C = 4),则代码为:
For A …Run Code Online (Sandbox Code Playgroud) 我对 Laravel 的方法有疑问->paginate()。基本上,我有一个动态查询,根据GET传递给控制器的参数,其中包含或不包含子句::where()。如果没有::where(),我可以调用->paginate(100)结果集并将其拆分为 100 页。当尝试使用 执行相同的操作时,::where(...)->paginate(100)我得到 0 个结果。
起初我以为我的查询是错误的,但是删除->paginate(100)并替换它->get()会返回预期的结果数量。这是完整的代码:
$search = ((Input::get("search") != "") ? Input::get("search"):"");
$field = ((Input::get("field") != "") ? Input::get("field"):"");
if($search != "" && $field != ""){
$templates = Template::where($field, "LIKE", $search)
->orderBy($sort, $order)
->paginate(100); // Fails (0 Results)
->get(); // Works (3 Results)
} else {
$templates = Template::orderBy($sort, $order)
->paginate(100);
}
Run Code Online (Sandbox Code Playgroud)
我不确定为什么->paginate()返回 0 结果。另外,dd($templates)返回整个object(Illuminate\Pagination\Paginator) …
我正在尝试将自助服务终端功能添加到Android应用程序中.我希望利用Lollipop的屏幕固定功能,一旦启动就自动固定应用程序.应用程序UI已经构建了几个不同的活动.我在入口点活动startLockTask()的onCreate()方法中调用一次,但是一旦我导航到应用程序中的另一个活动,它就会取消.
我需要startLockTask()在每次活动中打电话onCreate()/onResume()吗?这将产生糟糕的用户体验,因为每次我在应用程序中从一个活动移动到另一个活动时,将出现"屏幕固定"烤面包.
我正在开发运行Android 5.0.2的Moto E(第二代)上的应用程序.
所以我正在使用Native Base <Input>标签,试图设置refs来处理表格字段中的"tabbing".我有以下代码:
<Item floatingLabel>
<Label style={{ color: "#fff" }}>First Name</Label>
<Input
ref={input => {
this.firstNameRef = input;
}}
onSubmitEditing={() => {
this.lastNameRef._root.focus();
}}
returnKeyType={"next"}
/>
</Item>
<Item floatingLabel last>
<Label style={{ color: "#fff" }}>Last Name</Label>
<Input
ref={input => {
this.lastNameRef = input;
}}
/>
</Item>
Run Code Online (Sandbox Code Playgroud)
基本上,我有2个<Input>标签,都有refset(this.firstNameRef和this.lastNameRef),但在加载App时,按First Name,然后在键盘上按"next"我收到以下错误:
无法读取未定义
onSubmitEditing Signup.js:162:26的属性'_root'
看来,使用<Input>实际上不设置任何参考价值,所以this.lastNameRef是null.
我也尝试使用React Native的<TextInput>元素,它确实如上所述处理设置引用,但在提交后仍然似乎没有处理焦点(即使._root从.focus()逻辑中删除).
还有其他人看过这个问题吗?
注意:目前仅在iOS中测试过.
我试图包含另一个视图,如果它存在.我有一个主要page.blade.php应该呈现所选页面的内容.在应用程序的后端,我得到一个包含所有内容的平面数组(id和name).所以现在如果存在模板我想渲染它,但首先我需要检查模板是否确实存在.
我现在得到以下代码:
@foreach($flattenPage as $page)
@if(file_exists('templates.'.strtolower($page['name'])))
@include('templates.'.strtolower($page["name"]))
@endif
@endforeach
Run Code Online (Sandbox Code Playgroud)
问题是它不会通过if语句.我也尝试过:
@if(file_exists('../templates/'.strtolower($page['name']).'.blade.php'))
Run Code Online (Sandbox Code Playgroud)
当前模板是views/page/show.blade.php,我需要渲染views/templates/myContent.blade.php
所以我在Laravel更新和保存时遇到了一个烦人的问题。我有一个模型/表Invoice和invoices,具有时间戳sent_at。
Invoice.php
class Invoice extends Model {
protected $dates = [
"sent_at",
];
}
Run Code Online (Sandbox Code Playgroud)
我有以下功能来更新Invoice:
InvoicesController.php:
public function postPayInvoice(Request $request, $invoiceId){
$user = $this->apiResponse->user;
$invoiceItemIds = $request->input("invoice_item_ids");
$invoice = Invoice::with(["invoiceItems" => function($subQuery) use($invoiceItemIds){
return $subQuery->whereIn("invoice_items.id", $invoiceItemIds);
}])->where("id", "=", $invoiceId)->first();
\Log::info("Load: ".$invoice->sent_at);
DB::beginTransaction();
try {
foreach($invoice->invoiceItems AS $invoiceItem){
$invoiceItem->status = "paid";
$invoiceItem->paid_at = Carbon::now();
$invoiceItem->save();
}
$totalInvoices = $invoice->invoiceItems()->count();
$paidInvoiceItems = $invoice->invoiceItems()->where("status", "=", "paid")->count();
if($totalInvoices == $paidInvoiceItems){
$invoice->status = "paid";
$invoice->paid_at = …Run Code Online (Sandbox Code Playgroud) 所以,我有以下模型:
class Recursive extends Model {
public function __construct() {
parent::__construct();
}
// ...
}
class Place extends Recursive {
protected $table = 'places';
protected $fillable = ['name', 'parent_id'];
// ...
}
Run Code Online (Sandbox Code Playgroud)
以下代码用于创建一个新的Place:
$place = Place::create([
'name' = 'Second',
'parent_id' => 1
]);
Run Code Online (Sandbox Code Playgroud)
这会在数据库中产生以下记录:
| Actual | Expected |
---------------------------------------------------------
| id | name | parent_id | id | name | parent_id |
| 1 | 'Top' | NULL | 1 | 'Top' | NULL |
| 2 …Run Code Online (Sandbox Code Playgroud) 我在尝试使用DB::rawLaravel查询时遇到了一个问题.当我尝试遍历我的结果集时,我无法定位某个列,因为它的名称包含一个空格:
$result = DB::connection("example")->select(DB::raw(
"SELECT table.column_1 AS 'NameOne',
table.column_2 AS 'Name Two'
FROM example;"
));
Run Code Online (Sandbox Code Playgroud)
如您所见,NameOne不包含空格,但Name Two确实如此.循环遍历结果集时,我无法回显结果table.column_2 AS 'Name Two,因为我无法正确定位它.我尝试过的:
<!-- language: html -->
@foreach($result AS $item)
<tr>
<td>{!! $result->NameOne !!}</td> <!-- Works Fine -->
<td>{!! $result->Name Two !!}</td> <!-- Doesn't work (obvious syntax error) -->
<td>{!! $result->Name_Two !!}</td> <!-- Doesn't work (undefined property) -->
<td>{!! $result->Name+Two !!}</td> <!-- Doesn't work (undefined property) -->
<td>{!! $result->NameTwo !!}</td> <!-- Doesn't work (undefined property) …Run Code Online (Sandbox Code Playgroud) 我的数据库中有一个表,如下所示:
id | date | amount
========================
1 | 2015-01-26 | 1000
2 | 2015-02-26 | 100
3 | 2014-06-26 | 10
Run Code Online (Sandbox Code Playgroud)
我想按年份对记录进行分组,然后找到每组金额的总和。
我可以使用以下查询找到组:
Fee::orderBy('date', 'DESC')
->get()
->groupBy(function($date) {
return Carbon::parse($date->date)->format('Y'); // grouping by years
})
Run Code Online (Sandbox Code Playgroud)
但我无法获得每组金额的总和。
laravel ×7
php ×7
laravel-5 ×3
mysql ×3
android ×1
blade ×1
dynamic ×1
eloquent ×1
excel ×1
javascript ×1
laravel-4 ×1
laravel-5.4 ×1
namespaces ×1
native-base ×1
nested-loops ×1
pagination ×1
react-native ×1
reference ×1
transactions ×1
vba ×1