标签: eloquent

Laravel eloquent - 其中字段等于 2 个值之一

我需要在 Laravel 项目中进行查询,我将使用该查询获取数据库中的记录,该记录的 slug 等于postpage

where('slug', 'post' || 'page')
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

laravel eloquent

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

在 Laravel 5 模型中按自定义属性排序?

我有一个名为的模型customers,它有一个名为name;的自定义属性。这可以是客户全名或公司名称,具体取决于他们的帐户类型。

class Customer extends Model
{

    const BUSINESS = "Business";
    const INDIVIDUAL = "Individual";

    protected $table = 'users';
    protected $appends = ['name'];
    private $name;

    public static function boot()
    {
        parent::boot();

        static::created(function () {
        });

        static::updating(function () {
        });
    }


    /**
     * Get the display name for the customer
     * 
     * if Customer::BUSINESS then use company field
     * else if Customer::INDIVIDUAL then use their name
     * 
     * @return mixed|string
     */
    function getDisplayName() {
        return ($this->account == Customer::BUSINESS) …
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent laravel-5

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

Laravel:即使明确提供了密码,“密码”列中的空值也违反了非空约束

我正在 Laravel 5.5 中构建一个 GraphQL API,我收到一个错误消息User::create(),说我没有为“密码”列提供值,即使我确实提供了。这是来自User::create():的完整消息:

SQLSTATE[23502]: Not null violation: 7 ERROR:  null value in column \"password\" violates not-null constraint
DETAIL:  Failing row contains (507, null, null, 2017-09-23 14:12:11, 2017-09-23 14:12:11, 658495, 56854685, Joe, Appleseed, royal, 1970-01-01, +31684736248, null, Poplar St, 14a, 1012AB, London, null, joe.appleseed@overwatch.com, joe.appleseed@mindef.nl, null). (SQL: insert into \"users\" (\"wid\", \"unumber\", \"first_name\", \"last_name\", \"civil_status\", \"birthdate\", \"phone_number\", \"street\", \"street_number\", \"postal_code\", \"city\", \"email_address_overwatch\", \"email_address_mindef\", \"updated_at\", \"created_at\") values (658495, 56854685, Joe, Appleseed, royal, 1970-01-01, +31684736248, Poplar …
Run Code Online (Sandbox Code Playgroud)

php postgresql laravel eloquent

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

此集合实例上不存在属性 [名称]

我在数据库中有 3 个表(用户、区域、区域用户),

用户表:id 名称 用户名 密码

区域表:id 名称

area_user 表:id user_id area_id

我正在尝试创建一个(列表用户页面),它将显示用户分配区域的所有用户表列。

User.php 模型文件

  <?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'password','role_id',];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token',];

    public function areas(){
        return $this->belongsToMany('App\Area','area_user');
    }
Run Code Online (Sandbox Code Playgroud)

Area.php 模型文件:

<?php

namespace …
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent laravel-query-builder laravel-eloquent

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

如何使用出生日期计算用户的平均年龄

我想使用用户的出生日期计算用户的平均年龄。我有一个名为 的模型类Member,包括一个获取用户年龄的函数。

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Member extends Model {
    public static function averageAge() {
        //What to do here
    }

    public function age() {
        $birthDate = Carbon::parse($this->birthDate);
        return $birthDate->diffInYears(Carbon::now());
    }
}
Run Code Online (Sandbox Code Playgroud)

php mysql sql laravel eloquent

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

雄辩的删除在 Laravel 5.6 中不起作用

这是我的代码:

路线

Route::resource('ItemName', 'ItemNameController');
Run Code Online (Sandbox Code Playgroud)

控制器

public function destroy(ItemName $itemName)
{
    $itemName->delete();
    return redirect('ItemName')->with('success', 'Item Has Been Delete');
}
Run Code Online (Sandbox Code Playgroud)

看法

<form action="{{ route('ItemName', $ItemName->id) }}" method="post">
    @csrf
    @method("DELETE")
    <input type="submit" class="btn btn-danger btn-sm" href="{{ $ItemName- 
    >id }}" value="Delete" onclick="return confirm('Are You Sure To Delete 
    This Item? #{{ $ItemName->inc }} ')">
</form>
Run Code Online (Sandbox Code Playgroud)

问题是代码无法删除表中的项目,有什么帮助吗?谢谢

php laravel eloquent laravel-5.6

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

laravel 5.5 group by 不起作用

我正在尝试获取一些数据,但我只是收到此错误

SQLSTATE[42000]:语法错误或访问冲突:1055 'ms_mascotas.ms_razas.id' 不在 GROUP BY 中(SQL:select ms_razas.id, ms_razas.nombre, ms_mascotas.raza_id, ms_mascotas.idfrom ms_mascotasinner join ms_razason ms_razas.id= ms_mascotas.raza_idwhere ms_razas.tipo_animal_id= 1 group by ms_mascotas.raza_id)”

我的查询是这样的

$data=Mascota::select("ms_razas.id","ms_razas.nombre","ms_mascotas.raza_id","ms_mascotas.id")
       ->join("ms_razas","ms_razas.id","=","ms_mascotas.raza_id")
       ->where("ms_razas.tipo_animal_id",$id)
       ->groupBy("ms_mascotas.raza_id")
       ->get();
Run Code Online (Sandbox Code Playgroud)

我一直在阅读这个错误,它与数据库文件中的严格模式有关,严格默认设置为false,我该怎么办?

php group-by laravel eloquent

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

Laravel:orderBy 喜欢计数

在我的网站上,注册用户可以创建帖子并喜欢其他人的帖子。在显示所有帖子时,我可以选择用户可以按标题、日期和喜欢计数排序。我已经完成了标题和日期,但我在按喜欢计数排序时遇到了麻烦。在我的系统中实现此功能的最佳方法是什么。

这是我用于检索和排序帖子的帖子控制器方法:

public function getEvents(Request $request){

    if($request['sort'] == "created_at"){
        $posts = Post::orderBy('created_at', 'desc')->get();
    }
    elseif($request['sort'] == "title"){
        $posts = Post::orderBy('title', 'desc')->get();
    }
    elseif($request['sort'] == "like"){

        //how would I go about ordering by like count here
    }
    return view ('eventspage',  ['posts' => $posts]);
}
Run Code Online (Sandbox Code Playgroud)

这是我的用户选择订购内容的视图:

<form action="{{ route('events') }}">    
    <select name="sort" onchange="this.form.submit()" class="form-control">
        <option value="">Sort By</option>
        <option value="created_at">Date</option>
        <option value="title">Title</option>
        <option value="like">Likes</option>
        <input type="hidden" name="formName" value="sort">
    </select>
</form>
Run Code Online (Sandbox Code Playgroud)

html php laravel eloquent

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

Eloquent Route 隐式绑定 Laravel 5.6 不起作用

我试图开发一个应用程序,我想使用路由绑定,但出了点问题,我不知道是什么。Plz,看看下面的代码,帮我看看哪里出了问题。

路线

|        | PATCH    | api/v1/filial/{filial}                  |      | Genesis\Base\Filial\Controllers\FilialController@update                    | auth:api   |
Run Code Online (Sandbox Code Playgroud)

模型

class Filial extends Model{

/**
 * @var string
 */
protected $table = "filiais"; ...
Run Code Online (Sandbox Code Playgroud)

控制器

class FilialController extends BaseFormController{...
    public function update(FilialRequest $request, Filial $filial){
       dd($filial);
    }...
Run Code Online (Sandbox Code Playgroud)

然后输出是一个空模型。我不知道这是什么错误,参数的,模型的,Uri 所有这些都匹配。我从这个项目开始就使用 Laravel 5.6。

php binding laravel eloquent

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

在 Laravel 中使用 WhereBetween 进行条件查询

对于一个小的搜索块表单,我有以下控制器功能部分:

    $manifests = DB::table('carrier_manifests')
        ->join('customers', 'carrier_manifests.carrierOrigin', '=', 'customers.id')
        ->select('carrier_manifests.*', 'customers.customer_name')          
        ->where([
                    ['manifestNumber', 'LIKE', '%' . $manifest . '%'],
                    ['originTerminal','LIKE','%' . $terminal . '%'],
                    ['carrierOrigin', $direction[0], $direction[1]],
                ])
        ->whereNull('deleted_at')
        ->orderBy('dateUnloaded', 'DESC')
        ->whereBetween('dateUnloaded', [$startDate, $endDate])
        ->limit(100)
        ->get();
Run Code Online (Sandbox Code Playgroud)

它的每个部分都正常工作,除了一个部分, the whereBetween,由于工作流程的必要性,有时dateUnloaded没有为 each 填写 in 子句carrier_manifest,这意味着如果该dateUnloaded字段为空,它将被排除在搜索结果之外。

是否有关于如何包含那些缺少的结果的建议dateUnloaded

laravel eloquent laravel-5

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