小编ggd*_*ras的帖子

Laravel的多级急切加载?

假设我有这些关系

class Invitation
{
    public function invitedPeople()
    {
        return $this->belongsTo('App\Person', 'personID');
    }
}

class Person 
{
    public function apartments()
    {
        return $this->belongsToMany('App\Apartment', 'apartment_person', 'personID', 'apartmentID');        
    }
}

class Apartment 
{
    public function city()
    {
        return $this->belongsTo('App\City', 'cityID');
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是,使用Laravel eager loading时我们可以拥有多少个嵌套级别?我已经尝试过这个查询并且它无法正常工作,有人可以建议解决这个问题吗?

return Invitation::with(['invitedPeople', 'invitedPeople.apartments', 'invitedPeople.apartments.city'])
Run Code Online (Sandbox Code Playgroud)

php eager-loading laravel laravel-5

6
推荐指数
1
解决办法
1540
查看次数

addSelect()Builder方法如何在Laravel中工作

说我有这些模型:

用户模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'User';

    protected $fillable =
    [
       'username', 'favoriteColor'
    ];          

    public function flights()
    {
        return $this->hasMany('App\Flight');
    }
}
Run Code Online (Sandbox Code Playgroud)

飞行模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class Flight extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'Flight';

    protected $fillable =
    [
       'flightName', 
    ];              
}   
Run Code Online (Sandbox Code Playgroud)

每当我这样做时,我都会尝试用急切的加载做一些事情:

$usersWithFlights = …
Run Code Online (Sandbox Code Playgroud)

php eager-loading laravel laravel-5

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

Laravel如何知道调度程序何时更新?

我的问题更多的是一般的疑惑.我有两个使用Laravel创建的命令,我们称之为A和B.

这些命令中的每一个都使用 - > dailyAt($ par)方法进行调度.但$ par参数来自查询.

我的意思是这样的:

protected function schedule(Schedule $schedule)
{
    $schedulerTime_commandA = App\Model\CommandsTime::where('id', 1)->first()->time;
    $schedulerTime_commandB = App\Model\CommandsTime::where('id', 2)->first()->time;

    $schedule->command('A')
            ->dailyAt($schedulerTime_commandA);

    $schedule->command('B')
            ->dailyAt($schedulerTime_commandB);
}
Run Code Online (Sandbox Code Playgroud)

这是因为超级用户想要安排这些命令运行的时间.我的问题是:Laravel如何知道App\Console\Kernel.php文件中的此调度方法已被更改?

注意:我有以下cron条目,因为Laravel在文档上讨论它.

* * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1
Run Code Online (Sandbox Code Playgroud)

php scheduler laravel laravel-4 laravel-5

5
推荐指数
1
解决办法
2352
查看次数

FindAndUpdate 如何检查文档是否真的更新了

想象一下以下模型:

var Office = 
{
    id: 1,
    name: "My Office",
    branches: 
    [
      {
        adddress: "Some street, that avenue",
        isPrincipal: true,
      },
      {
        adddress: "Another address",
        isPrincipal: false,
      },      
    ]
}
Run Code Online (Sandbox Code Playgroud)

我想删除一个分支,但我们不能让用户从办公室删除主要分支。所以这是我的功能:

remove: function(body)
{
  return new Promise(function(resolve, reject)
  {
    return Office.findByIdAndUpdate(1, { $pull: {'branches': {_id: body.branch.id}}}, { new: true })
    .then(function(updatedOffice){
      resolve(updatedOffice)
    })
    .catch(function(error){
      reject(error);
    });

  })
}    
Run Code Online (Sandbox Code Playgroud)

我在这里有一些疑问:

  1. 正如您所看到的,我没有在 isPrincipal 属性中包含另一个 WHERE,这是因为我不知道如何确定 office 对象是否已实际更改。因为对象总是会被检索但是......我怎么能确定呢?
  2. 考虑到我们不能让用户删除主体分支,FindByIdAndUpdate 是最好的方法,如果他试图这样做,我们必须显示警告。

javascript mongoose mongodb node.js mongodb-query

4
推荐指数
1
解决办法
3324
查看次数

在 Laravel 中将参数验证为 JSON 字符串

前端部分

参数的发送方式如下:

前端发送数据

Laravel 请求

class CarCreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        //TODO: Define authorization logic, possibly a middleware
        return true;
    }  

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'car.name' => 'present|required'
        ];
    }
}
Run Code Online (Sandbox Code Playgroud)

真正的问题

请求类始终验证为 false。我检查了验证数组部分,但看起来这可以发送如下参数:

car[name]=Spidey Mobile
Run Code Online (Sandbox Code Playgroud)

但是,我需要使用 JSON.stringify() 发送字符串化的数据。

有解决方法吗?看起来点符号不起作用,因为这是一个 JSON …

php json laravel laravel-5 laravel-validation

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