标签: eloquent

如何在核心PHP中使用Laravel Eloquent模型?

我正在用核心PHP开发一个项目,并且我想在项目中使用雄辩的查询结构来简化设置mySQL连接和执行mySQL查询的过程。

php mysql laravel eloquent

0
推荐指数
1
解决办法
426
查看次数

雄辩的关系 - 其中NotNotNull不存在

我在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不存在.

有任何想法吗?

laravel eloquent

0
推荐指数
1
解决办法
342
查看次数

PHP Laravel:比这些if语句更好的方法

我需要将佣金分配给新创建的价格.佣金适用于客户,类型和价格.因此,如果有类型佣金,它应该首先获得它,回退到客户端,回退到默认值.

我的代码有效,但感觉有点"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)

php laravel eloquent

0
推荐指数
1
解决办法
77
查看次数

显示在所选字段中匹配的WHERE条件值

让我们考虑一下,我有一个名为Citizens的表,其中包含以下字段和值:

| ID | Name   | Age |
|----|:------:|---: |
| 1  | King   | 60  |
| 2  | Queen  | 50  |
| 3  | Prince | 25  |
Run Code Online (Sandbox Code Playgroud)

如果我想展示" 50100岁" 之间的公民和" 530岁之间"的公民,我可以使用这样一个简单的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)

mysql sql eloquent

0
推荐指数
1
解决办法
70
查看次数

如何使用空参数在Laravel Eloquent中创建whereIn查询构建器

我在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不适用它.我无法在网上找到任何有效的解决方案.我该怎么做这样的查询?

php laravel eloquent

0
推荐指数
1
解决办法
209
查看次数

雄辩的updateOrCreate总是创建

我试图使用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)

我有一些可为空的字段,但我认为这不是问题。

php mysql laravel eloquent

0
推荐指数
1
解决办法
132
查看次数

赞功能不适用于搜索

我正在尝试创建搜索功能,但搜索中什么也没显示

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)

php laravel eloquent

0
推荐指数
1
解决办法
33
查看次数

无法为模型的属性设置新值

我正在尝试为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)

php laravel eloquent laravel-5

0
推荐指数
1
解决办法
93
查看次数

在Laravel中保存数据时如何禁用updated_at和created_at

我试图将数据保存在模型函数中。使用模型的save()函数时,出现如下错误。

照射\数据库\ QueryException:SQLSTATE [42S22]:柱未找到:1054未知列'的updated_at'在'字段列表'(SQL:插入到account_typesnameupdated_atcreated_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)

php model laravel eloquent

0
推荐指数
1
解决办法
109
查看次数

Laravel Where子句不适用于Null值

我有一张customers桌子,Customer作为模特。我正在尝试获取ID为1的客户类别以外的所有客户。customer_category_id可以为空。

我有以下查询。

Customer::where('customer_category_id', '!=', 1)->with('customerMeta')->get();
Run Code Online (Sandbox Code Playgroud)

上面的查询不会为客户提供空的客户类别ID。请问是什么原因?

我需要类别为nu​​ll或类别不等于1的所有客户

任何形式的建议,表示赞赏。

php laravel eloquent

0
推荐指数
1
解决办法
81
查看次数

标签 统计

eloquent ×10

laravel ×9

php ×8

mysql ×3

laravel-5 ×1

model ×1

sql ×1