标签: eloquent

Laravel - Eloquent"Has","With","WhereHas" - 它们是什么意思?

我发现这些方法背后的概念和含义有点令人困惑,是否有人可以向我解释在一个例子(如果可能)的背景下,has和之间的区别with是什么?

orm relational-database relationship laravel eloquent

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

获取在Laravel 3/4中执行的查询

如何使用Laravel Query Builder或Eloquent ORM在Laravel 3/4中检索原始执行的SQL查询?

例如,像这样:

DB::table('users')->where_status(1)->get();
Run Code Online (Sandbox Code Playgroud)

要么:

(posts (id, user_id, ...))

User::find(1)->posts->get();
Run Code Online (Sandbox Code Playgroud)

否则,至少我如何保存执行到laravel.log的所有查询?

php orm laravel eloquent laravel-query-builder

178
推荐指数
11
解决办法
17万
查看次数

迁移:无法在laravel中添加外键约束

我正在尝试在Laravel中创建外键但是当我使用artisani 迁移我的表时抛出以下错误:

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `priorities` add constraint priorities_user_id_foreign foreign 
key (`user_id`) references `users` (`id`))     
Run Code Online (Sandbox Code Playgroud)

我的迁移代码如下:

优先级迁移文件

public function up()
{
    //
    Schema::create('priorities', function($table) {
        $table->increments('id', true);
        $table->integer('user_id');
        $table->foreign('user_id')->references('id')->on('users');
        $table->string('priority_name');
        $table->smallInteger('rank');
        $table->text('class');
        $table->timestamps('timecreated');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    //
    Schema::drop('priorities');
}
Run Code Online (Sandbox Code Playgroud)

用户迁移文件

public function up()
{
    //
    Schema::table('users', function($table)
    {
    $table->create();
    $table->increments('id');
    $table->string('email');
    $table->string('first_name');
    $table->string('password');
    $table->string('email_code'); …
Run Code Online (Sandbox Code Playgroud)

migration foreign-keys laravel eloquent laravel-4

175
推荐指数
17
解决办法
19万
查看次数

Laravel 4:如何使用Eloquent ORM"排序"

简单的问题 - 如何通过Laravel 4中的'id'降序排序.

我的控制器的相关部分如下所示:

$posts = $this->post->all()
Run Code Online (Sandbox Code Playgroud)

据我了解你使用这一行:

->orderBy('id', 'DESC');
Run Code Online (Sandbox Code Playgroud)

但是这与我上面的代码有什么关系呢?

php laravel eloquent laravel-4

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

Laravel迁移变为使列可以为空

我用unsigned创建了一个迁移user_id.如何user_id在新迁移中编辑以进行编辑nullable()

Schema::create('throttle', function(Blueprint $table)
{
    $table->increments('id');
    // this needs to also be nullable, how should the next migration be?
    $table->integer('user_id')->unsigned();
}
Run Code Online (Sandbox Code Playgroud)

nullable laravel eloquent laravel-5 laravel-migrations

172
推荐指数
9
解决办法
17万
查看次数

Get Specific Columns Using "With()" Function in Laravel Eloquent

I have two tables, User and Post. One User can have many posts and one post belongs to only one user.

In my User model I have a hasMany relation...

public function post(){
    return $this->hasmany('post');
}
Run Code Online (Sandbox Code Playgroud)

And in my post model I have a belongsTo relation...

public function user(){
    return $this->belongsTo('user');
}
Run Code Online (Sandbox Code Playgroud)

Now I want to join these two tables using Eloquent with() but want specific columns from the second table. I know I can use the Query …

php orm laravel eloquent laravel-query-builder

165
推荐指数
9
解决办法
20万
查看次数

禁用Laravel的Eloquent时间戳

我正在将我们的一个Web应用程序从CodeIgniter转换为Laravel.但是此时我们不想将updated_at/ created_atfields 添加到所有表中,因为我们已经有了一个日志类,它已经为我们更深入地完成了所有这些.

我知道我可以$timestamps = false;进入:

Vendor\laravel\framework\src\illuminate\Datebase\Eloquent\Model.php
Run Code Online (Sandbox Code Playgroud)

但是我宁愿不为Laravel更改核心文件,也不要让我的每个模型都在顶部.有没有办法在其他所有型号禁用它?

php laravel eloquent

163
推荐指数
8
解决办法
19万
查看次数

创建和更新Laravel Eloquent

如果新记录不存在,插入新记录或更新的简写是什么?

<?php

$shopOwner = ShopMeta::where('shopId', '=', $theID)
    ->where('metadataKey', '=', 2001)->first();

if ($shopOwner == null) {
    // Insert new record into database
} else {
    // Update the existing record
}
Run Code Online (Sandbox Code Playgroud)

php laravel eloquent laravel-query-builder

142
推荐指数
7
解决办法
27万
查看次数

使用雄辩的ORM在Laravel中批量插入

我们如何使用Eloquent ORM在Laravel中执行批量数据库插入?

我想在Laravel中完成此任务:https://stackoverflow.com/a/10615821/600516 但我收到以下错误.

SQLSTATE [HY093]:参数号无效:混合命名和位置参数.

php database laravel eloquent

135
推荐指数
6
解决办法
17万
查看次数

有没有办法用Laravel的ELOQUENT ORM"限制"结果?

有没有办法用Laravel的ELOQUENT ORM"限制"结果?

 SELECT * FROM  `games` LIMIT 30 , 30 
Run Code Online (Sandbox Code Playgroud)

和Eloquent?

mysql laravel sql-limit eloquent

131
推荐指数
3
解决办法
15万
查看次数