标签: eloquent

Laravel雄辩的ORM集团在哪里

如何将以下查询转换为Laravel 4 eloquent ORM?

select * from table where ((starttime <= ? and endtime >= ?) or (starttime <= ? and endtime >= ?) or (starttime >= ? and endtime <= ?))
Run Code Online (Sandbox Code Playgroud)

eloquent laravel-4

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

Laravel Eloquent LEFT JOIN WHERE NULL

我正在尝试使用Eloquent在数据库种子期间执行以下查询:

SELECT
    *
FROM
    customers
LEFT JOIN
    orders
    ON customers.id = orders.customer_id
WHERE
    orders.customer_id IS NULL
Run Code Online (Sandbox Code Playgroud)

这是我在Eloquent中的实现:

$c = Customer::leftJoin('orders', function($join) {
      $join->on('customers.id', '=', 'orders.customer_id');
    })
    ->whereNull('orders.customer_id')
    ->first();
Run Code Online (Sandbox Code Playgroud)

虽然第一个查询总是返回完整的结果,但Eloquent等效项总是返回除表emailphone字段之外的所有内容的空元素customers.我在无法解释这一点,因为在CustomersOrders模特都是工匠产生的骨架.

例如:

class Customer extends \Eloquent {

    // Add your validation rules here
    public static $rules = [
        // 'title' => 'required'
    ];

    // Don't forget to fill this array
    protected $fillable = [];

}
Run Code Online (Sandbox Code Playgroud)

这是我在dd()种子上的第一个Eloquent查询(最初由Faker生成)时输出的数组:

protected $original =>
  array(25) {
    'id' …
Run Code Online (Sandbox Code Playgroud)

php mysql laravel eloquent laravel-4

32
推荐指数
4
解决办法
10万
查看次数

Laravel 5迁移标识符名称太长

我正在尝试运行以下迁移:

public function up()
{
    Schema::create('lifestyle_questions', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('question');
        $table->timestamps();
    });

    Schema::create('lifestyle_question_answers', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('lifestyle_question_id')->unsigned();
        $table->foreign('lifestyle_question_id')->references('id')->on('lifestyle_questions');
        $table->string('answer');
        $table->timestamps();
    });

    Schema::create('user_lifestyle_question_answers', function(Blueprint $table)
    {
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->integer('lifestyle_question_answer_id')->unsigned();
        $table->foreign('lifestyle_question_answer_id')->references('id')->on('lifestyle_question_answers');
    });
}
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'user_lifestyle_question_answers_lifestyle_question_answer_id_foreign' is too long (SQL: alter table `user_lifestyle_question_answers` add constraint user_lifestyle_question_answers_lifestyle_question_answer_id_foreign foreign key (`lifestyle_question_answer_id`) references `lifestyle_question_answers` (`id`))

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'user_lifestyle_question_answers_lifestyle_question_answer_id_foreign' is too long
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent laravel-5

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

直接从模型实例化查询构建器

当我做类似的事情SomeModel::with('user')时返回一个Query\Builder实例.如何在不需要调用with()(或类似)的情况下获取此实例?

例如,我尝试过:new SomeModel但它显然会返回我的模型的实例,而不是查询构建器(不对我工作).在SomeModel::getQuery没有工作过,因为它返回一个Query\Builder不相关的我的模型.

我需要它来基于一些条件设置.所以最初它需要是空的,就像它:

$someBuilder = SomeModel::getQueryBuilder(); // eg.

if(condition()) {
    $someBuilder->where(...);
}

$someResults = $someBuilder->get();
Run Code Online (Sandbox Code Playgroud)

model laravel eloquent

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

Laravel Eloquent按关系表列排序

我试图从shop_products表中逐pinned列对产品进行排序shop_products_options:

$products = Shop\Product::with(['options' => function ($query) {

    $query->orderBy('pinned', 'desc'); 

}])->paginate(5);
Run Code Online (Sandbox Code Playgroud)

我在Shop\Product模型中设置关系:

public function options()
{
    return $this->hasOne('Shop\Options');
}
Run Code Online (Sandbox Code Playgroud)

但产品没有分类.我得到一个只适用于shop_products_options表的查询.

SELECT * FROM `shop_products_options` WHERE `shop_products_options`.`product_id` in ('8', '9', '10', '11', '12') ORDER BY `pinned` DESC
Run Code Online (Sandbox Code Playgroud)

怎么解决?

php orm laravel eloquent

31
推荐指数
3
解决办法
4万
查看次数

Laravel雄辩更新记录,无需从数据库加载

我对laravel很新,我正在尝试从表单的输入更新记录.但是我看到要更新记录,首先需要从数据库中获取记录.是不是可能更新记录(主键设置):

$post = new Post();
$post->id = 3; //already exists in database.
$post->title = "Updated title";
$post->save();
Run Code Online (Sandbox Code Playgroud)

php eloquent laravel-5

31
推荐指数
5
解决办法
7万
查看次数

在Laravel 5中从数据库创建模型

有没有办法 Laravel 5中的数据库生成模型

generator包只创建一个空模型.

models laravel eloquent laravel-5

31
推荐指数
3
解决办法
5万
查看次数

如何按Eloquent ORM中多对多关系的数据透视表的字段进行排序

我一直在使用Eloquent ORM已经有一段时间了,我知道它非常好,但我不能做到以下几点,而在Fluent中很容易做到.

我的用户有多对多的歌曲,中间表是song_user(就像它应该的那样).根据播放次数判断,我想获得用户的热门歌曲.当然,播放计数存储在中间表中.

我可以用Fluent做到:

$songs = DB::table('songs')
    ->join('song_user', 'songs.id', '=', 'song_user.song_id')
    ->where('song_user.user_id', '=', $user->id)
    ->orderBy("song_user.play_count", "desc")
    ->get();
Run Code Online (Sandbox Code Playgroud)

简单.但我想在Eloquent中做到这一点,当然这不起作用:

$songs = Song::
    with(array("song_user" => function($query) use ($user) {
        $query->where("user_id", "=", $user->id)->orderBy("play_count", "desc");
    }))
Run Code Online (Sandbox Code Playgroud)

laravel eloquent

30
推荐指数
2
解决办法
2万
查看次数

为什么软删除的实体出现在查询结果中?

我正在尝试实现软删除概念.

这是我的对象:

class Post extends Eloquent {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'posts';
    protected $softDelete = true;

    ...
Run Code Online (Sandbox Code Playgroud)

软删除已开启.

现在,如果我'删除'帖子,它会获得'deleted_at'时间戳:

描述

问题是,当我搜索或仅用于all()显示帖子时,会在那里显示软删除的项目.怎么了?

soft-delete laravel eloquent laravel-4

30
推荐指数
4
解决办法
4万
查看次数

Laravel 5.1 Eloquent ORM随机返回不正确的关系 - *主要更新*

我有一个Laravel应用程序,为电子商务网站提供适度的流量.该网站允许人们通过前端下订单,但它还具有通过呼叫中心通过电话接听订单的后端功能.

订单与客户相关,客户可以选择是用户 - 用户是登录前端的用户.只有通过呼叫中心下达订单才能创建没有用户帐户的客户.

我遇到的问题非常奇怪,我相信可能是某种Laravel错误.

它只是偶尔发生,但正在发生的事情是,当通过呼叫中心为没有用户帐户的客户下订单时,订单确认将被发送给随机用户 - 字面意思是随机的,据我所知,尽管数据没有关系,但刚从数据库中拔出.

这些是项目中模型的相关部分:

class Order extends Model
{
    public function customer()
    {
        return $this->belongsTo('App\Customer');
    }
}

class Customer extends Model
{
    public function orders()
    {
        return $this->hasMany('App\Order');
    }

    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

class User extends Model
{ 
    public function customer()
    {
        return $this->hasOne('App\Customer');
    }
}
Run Code Online (Sandbox Code Playgroud)

这些是上面的数据库迁移(为简洁起见编辑):

   Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('first_name');
        $table->string('last_name');
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->boolean('active');
        $table->rememberToken();
        $table->timestamps();
        $table->softDeletes();
    });

    Schema::create('customers', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('user_id')->nullable->index();
        $table->string('first_name');
        $table->string('last_name'); …
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent laravel-5 laravel-5.1

30
推荐指数
1
解决办法
1802
查看次数

标签 统计

eloquent ×10

laravel ×8

php ×5

laravel-5 ×4

laravel-4 ×3

laravel-5.1 ×1

model ×1

models ×1

mysql ×1

orm ×1

soft-delete ×1