在Laravel 5.1中查看计划任务代码

时间:2016-05-24 23:17:54

标签: laravel laravel-5 laravel-5.1

我正在编写一个计划偶尔运行的代码。我想删除在users表上有详细信息但在profiles表中没有记录的用户记录。我正在考虑删除他们的身份,但我不确定如何才能进行比赛。个人资料迁移:

public function up()
{
    Schema::create('profiles', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
        $table->string('gender',5);
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); // When a profile is deleted- his corresponding associated id details are deleted as well.
        $table->tinyInteger('age')->unsigned()->nullable();
        $table->string('goals')->nullable();;
        $table->string('activityType')->nullable();
        $table->rememberToken(); // for remember me feature.
    });
}

用户迁移:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('username',20)->unique(); 
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->rememberToken();
        $table->timestamps();
    });

我已经开始编写查询,但我不确定如何继续。

 public function handle()
    {
    // type the query to get users that have no data in their profiles.

        $empty_profile=App\Profile:::where('id','')->get;
        $user_id=App\User::where('id')
    }

感谢您的帮助。感谢。

1 个答案:

答案 0 :(得分:1)

虽然你的例子对我来说并不是很清楚(我想知道为什么会有没有配置文件的用户?)我认为这就是你所需要的:

public function handle()
{
    $empty_profiles = App\User::doesntHave('profile')->delete();
}

如果模型连接正确,这将起作用另见: https://laravel.com/docs/master/eloquent-relationships