我正在尝试更新具有两个主键的Model.
模型
namespace App;
use Illuminate\Database\Eloquent\Model;
class Inventory extends Model
{
/**
* The table associated with the model.
*/
protected $table = 'inventories';
/**
* Indicates model primary keys.
*/
protected $primaryKey = ['user_id', 'stock_id'];
...
Run Code Online (Sandbox Code Playgroud)
移民
Schema::create('inventories', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('stock_id')->unsigned();
$table->bigInteger('quantity');
$table->primary(['user_id', 'stock_id']);
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('restrict')
->onDelete('cascade');
$table->foreign('stock_id')->references('id')->on('stocks')
->onUpdate('restrict')
->onDelete('cascade');
});
Run Code Online (Sandbox Code Playgroud)
这是应该更新Inventory模型的代码,但它没有.
$inventory = Inventory::where('user_id', $user->id)->where('stock_id', $order->stock->id)->first();
$inventory->quantity += $order->quantity;
$inventory->save();
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
Illegal offset type
Run Code Online (Sandbox Code Playgroud)
我也尝试使用updateOrCreate()方法.它不起作用(我得到同样的错误).
谁能告诉我们应该如何更新带有两个主键的Model?
我在我的项目中使用了一个包,它在里面存储了一个设置 config/packagename
我想在配置文件中动态更改此值,这是文件结构当前的外观;
<?php
return [
'view_id' => '118754561',
'cache_lifetime_in_minutes' => 60 * 24,
];
Run Code Online (Sandbox Code Playgroud)
我想把它改成这样的东西 -
'view_id' => Auth::user()->id,
Run Code Online (Sandbox Code Playgroud)
您可以在配置文件中执行此操作,还是必须存储某种变量以便稍后在控制器中更新.有没有办法将这些变量放在env文件中并从控制器访问这些新变量?
我试图在我的条件下给予条件Middleware.
这是我的剧本
if (auth()->check() && auth()->user()->type == 'TP001') {
$menu->add("User Control",array('nickname' => "user",'class'=>'treeview'))
->append(' <b class="caret"></b>')
->prepend('<span class="glyphicon glyphicon-user"></span> ');
$menu->user->add('Daftar User','user/list');
$menu->user->add('Tipe User','user/type');
} else {
/* Some code here...*/
}
Run Code Online (Sandbox Code Playgroud)
上面的脚本我不能看到带有条件的菜单,即使我已经登录'TP001'(总是在其他地方),然后我尝试修复我的代码
auth()->user()->isDeveloper()
Run Code Online (Sandbox Code Playgroud)
我的模特
public function isDeveloper()
{
return ($this->type == 'TP001');
}
Run Code Online (Sandbox Code Playgroud)
但仍然没有工作,有没有办法给出上述条件但是以正确的方式?提前谢谢,抱歉我的英语不好.
我的核心
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\Frontend::class,
];
Run Code Online (Sandbox Code Playgroud) 我找不到任何与两种类型的会话访问方法相关的信息.
$request->session()来自HTTP请求实例和session()Laravel 5.3中的会话助手.是否有任何差异或何时使用?
使用PHP单元时如何向get controller方法发送get请求
public function testMyMethod(Request $request){
$userExist = $request->session()->exists('user_id');
}
Run Code Online (Sandbox Code Playgroud) 我有一个Laravel 5.4应用程序,它的模型指向不同的数据库连接.
例如,我User指向一个MySQL数据库,然后Company指向一个PostgreSQL数据库(使用该$connection变量).
现在,当我运行PHPUnit时,我希望将$connection变量替换为phpunit.xml文件中指定的内容,这是内存类型数据库中的SQLite.
这怎么可以实现?
我已经看到这个问题在其他地方得到了解答,但我仍然无法使其成功,所以我需要进一步澄清:
给出的例子是:
$tag['_'] = 'yyy';
$tag['attr'] = 'xxx';
$tagVar = new SoapVar($tag, SOAP_ENC_OBJECT);
Run Code Online (Sandbox Code Playgroud)
生成的xml将是:
<tag attr="xxx">yyy</tag>
Run Code Online (Sandbox Code Playgroud)
但是,我得到了
<tag>
<_>yyy</_>
<attr>xxx</attr>
</tag>
Run Code Online (Sandbox Code Playgroud)
那么,还有什么需要让它按预期工作吗?甚至在SoapServer类或WSDL中的某种配置?
为了使事情复杂化,元素是命名空间,所以实际上我正在寻找一种方法
<ns:tag attr="xxx">yyy</ns:tag>
Run Code Online (Sandbox Code Playgroud)
让我感到惊讶的是,使用SoapClient和SoapServer完成工作是多么困难,以及SO和其他地方关于这个主题的大多数问题如何由提出问题的同一个人回答,提出某种黑客或奇怪的无证行为,当然是经过数小时(天)的斗争.