我用unsigned user_id创建了一个迁移。我如何编辑user_id在一个新的迁移,也使它为空()?

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();
}

我想使用Laravel Eloquent中的orderBy()方法对Laravel 4中的多个列进行排序。查询将使用Eloquent像这样生成:

SELECT *
FROM mytable
ORDER BY
  coloumn1 DESC, coloumn2 ASC

我该怎么做呢?

如何检查字段是否为雄辩不为空?

我试着模型::(‘sent_at’,‘不是’,DB::生(“空”))- >…但它给出了IS NOT作为一个绑定,而不是一个比较。

这是DB::getQueryLog()所说的:

  'query' => string 'select * from my_table where sent_at = ? and profile_id in (?, ?) order by created_at desc' (length=101)
  'bindings' => 
    array (size=3)
      0 => string 'IS NOT' (length=6)
      1 => int 1
      2 => int 4

我有两个表,User和Post。一个用户可以有多个帖子,一个帖子只能属于一个用户。

在我的用户模型中,我有一个hasMany关系…

public function post(){
    return $this->hasmany('post');
}

在我的post模型中,我有一个belongsTo关系…

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

现在我想使用Eloquent with()连接这两个表,但想要第二个表中的特定列。我知道我可以使用查询生成器,但我不想这样做。

在Post模型中,我写…

public function getAllPosts() {
    return Post::with('user')->get();
}

它运行以下查询…

select * from `posts`
select * from `users` where `users`.`id` in (<1>, <2>)

但我想要的是…

select * from `posts`
select id,username from `users` where `users`.`id` in (<1>, <2>)

当我用…

Post::with('user')->get(array('columns'....));

它只返回第一个表中的列。我想要第二个表中使用with()的特定列。我该怎么做呢?

这可能是一个微不足道的问题,但我想知道Laravel是否推荐了一种特定的方法来检查从$result = Model::where(…)->get()返回的Eloquent集合是否为空,以及计算元素的数量。

我们目前正在使用!$result来检测空结果,这足够了吗?至于count($result),它实际上涵盖了所有情况,包括空结果吗?

我发现这些方法背后的概念和意义有点令人困惑,有人能在一个例子中(如果可能的话)向我解释一下has和with is之间的区别吗?

如何在Laravel框架中使用Eloquent或Fluent选择随机行?

我知道,通过使用SQL,您可以按RAND()进行排序。但是,我希望获得随机行,而不是在初始查询之前对记录的数量进行计数。

什么好主意吗?

我怎么说WHERE (a =1 OR b =1) AND (c =1 OR d =1)

对于更复杂的查询,我应该使用原始SQL吗?

我刚来拉拉维尔。如何查找是否存在记录?

$user = User::where('email', '=', Input::get('email'));

我能做什么来查看$user是否有记录?

我目前正在使用下面的代码在一个表中插入数据:

<?php

public function saveDetailsCompany()
{
    $post = Input::All();

    $data = new Company;
    $data->nombre = $post['name'];
    $data->direccion = $post['address'];
    $data->telefono = $post['phone'];
    $data->email = $post['email'];
    $data->giro = $post['type'];
    $data->fecha_registro = date("Y-m-d H:i:s");
    $data->fecha_modificacion = date("Y-m-d H:i:s");

    if ($data->save()) {
        return Response::json(array('success' => true), 200);
    }
}

我想返回插入的最后一个ID,但我不知道如何获取它。

亲切的问候!