我正在用核心PHP开发一个项目,并且我想在项目中使用雄辩的查询结构来简化设置mySQL连接和执行mySQL查询的过程。
我在Eloquent中有一个关系,我正在尝试查询.
$deliveryOverride = $product->days->whereNotNull('margin')
->where('price_id', $order['price_id'])
->where('product_id', $product->id)
->where('sale_at', date('Y-m-d', strtotime($day)))
->first();
Run Code Online (Sandbox Code Playgroud)
我一直在收到错误
方法whereNotNull不存在.
有任何想法吗?
我需要将佣金分配给新创建的价格.佣金适用于客户,类型和价格.因此,如果有类型佣金,它应该首先获得它,回退到客户端,回退到默认值.
我的代码有效,但感觉有点"if-y".也许有更好的方法吗?
private function addDefaultOnlineCommission(Price $price)
{
$defaultCommission = (object)Commission::DEFAULT_COMMISSIONS;
$typeCommission = $price->type->commissions()
->where('is_online', '=', true)->first();
$clientCommission = $price->type->client->commissions()
->where('is_online', '=', true)->first();
if (!$clientCommission && !$typeCommission) {
$commission = $defaultCommission;
}
if ($clientCommission && !$typeCommission) {
$commission = $clientCommission;
}
if ($typeCommission) {
$commission = $typeCommission;
}
$price->commissions()->create([
'commission_type' => $commission->commission_type,
'commission_value' => $commission->commission_value,
'min_value' => $commission->min_value,
'is_online' => true,
'valid_from' => Carbon::now()->format('Y-m-d H:i:s'),
]);
}
Run Code Online (Sandbox Code Playgroud) 让我们考虑一下,我有一个名为Citizens的表,其中包含以下字段和值:
| ID | Name | Age |
|----|:------:|---: |
| 1 | King | 60 |
| 2 | Queen | 50 |
| 3 | Prince | 25 |
Run Code Online (Sandbox Code Playgroud)
如果我想展示" 50到100岁" 之间的公民和" 5到30岁之间"的公民,我可以使用这样一个简单的WHERE条件:
SELECT * FROM Citizens WHERE (Age BETWEEN 5 AND 30 ) OR (Age BETWEEN 50 AND 100 );
Run Code Online (Sandbox Code Playgroud)
但是,问题就出现了:如何在符合条件的每一行旁边显示最小和最大请求年龄?
| ID | Name | Age |INTERVAL|
|----|:------:|:---:|-------:|
| 1 | King | 60 …Run Code Online (Sandbox Code Playgroud) 我在Laravel Eloquent中进行了查询以在表格中搜索.
public function searchContest($search){
$category = [];
$area = [];
if(isset($search['area'])){
$area = $search['area'];
}
if(isset($search['category'])){
$category = $search['category'];
}
$qry = self::whereIn('area',$area)
->whereIn('category', $category)
->get();
var_dump($query);
return;
}
Run Code Online (Sandbox Code Playgroud)
但有时,area或者category是空的,whereIn不适用它.我无法在网上找到任何有效的解决方案.我该怎么做这样的查询?
我试图使用updateOrCreate简化我的代码,但是该功能始终会创建一个新行,从不更新。
我的迁移:
Schema::create('logs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('project_id')->unsigned();
$table->datetime('is_fixed')->nullable();
$table->datetime('snooze_until')->nullable();
$table->integer('snooze_while')->nullable();
$table->string('title', 100);
$table->string('level', 100);
$table->string('description');
$table->string('stage',100);
$table->json('details')->nullable();
$table->timestamps();
$table->foreign('project_id')->references('id')->on('projects');
});
Run Code Online (Sandbox Code Playgroud)
我的$ fillables
protected $fillable = [
'project_id',
'is_fixed',
'snooze_until',
'snooze_while',
'title',
'level',
'description',
'stage',
'details'
];
Run Code Online (Sandbox Code Playgroud)
我的测试
$log = new Log();
$log->fill([
'title' => 'Vicente\\Sally\\Schiller',
'level' => 'ERROR',
'description' => 'Laboriosam et architecto voluptatem.',
'stage' => 'production@wender-fedora',
'details' => '{"app":"MyApp"}',
'project_id' => 5
]);
Log::updateOrCreate($log->toArray());
Run Code Online (Sandbox Code Playgroud)
我有一些可为空的字段,但我认为这不是问题。
我正在尝试创建搜索功能,但搜索中什么也没显示
public function search(Request $request)
{
$search = $request->get('search');
$dictionaries = Dictionary::all()->where('title', 'LIKE', "%{$search}%");
return view('dictionary', compact('dictionaries'));
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试为Model的属性设置新值,但是它不起作用。我在设置新值后立即使用dd(),但它保留了旧值。
$business->users()
->where('userable_id', $business->id)
->where('userable_type', 'App\Models\Business')
->first()->first_name = "New";
Run Code Online (Sandbox Code Playgroud) 我试图将数据保存在模型函数中。使用模型的save()函数时,出现如下错误。
照射\数据库\ QueryException:SQLSTATE [42S22]:柱未找到:1054未知列'的updated_at'在'字段列表'(SQL:插入到
account_types(name,updated_at,created_at)的值(设备厂,2019年8月5日五时33分57秒, 2019-08-05 05:33:57))在文件F:\ HTML&
我已删除表coz中的created_at和updated_at列,我不需要它。我想知道在Laravel中使用模型save()函数时如何禁用created_at和update_at数据保存。
<?php
namespace pshub;
use Illuminate\Database\Eloquent\Model;
class AccountType extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name'];
// Create Type Function
public function createType($type) {
$existence = $this->where('name', $type)->get();
if (sizeof($existence) > 0) {
return $this->where('name', $type);
} else {
$this->name = $type;
$this->save();
return $this->id;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一张customers桌子,Customer作为模特。我正在尝试获取ID为1的客户类别以外的所有客户。customer_category_id可以为空。
我有以下查询。
Customer::where('customer_category_id', '!=', 1)->with('customerMeta')->get();
Run Code Online (Sandbox Code Playgroud)
上面的查询不会为客户提供空的客户类别ID。请问是什么原因?
我需要类别为null或类别不等于1的所有客户
任何形式的建议,表示赞赏。