【问题标题】:Updating/Adding value to array in Laravel 8在 Laravel 8 中更新/向数组添加值
【发布时间】:2021-12-23 17:36:40
【问题描述】:

我是新开发人员,我似乎被困在 laravel 中处理数组上。我正在使用 Laravel 8,我似乎无法解决这种情况。

我正在建立一个内部招聘网站,一旦经理发布职位,员工就可以申请这些特定职位。我已将数据库中的表定义为将“申请人”作为由 user_id 组成的数组。但是,我似乎无法向其中添加多个数组。

以下是我的招聘模式

class Recruitment extends Model
{
    use HasFactory;

    protected $fillable = [
        'title',
        'salary',
        'term_start',
        'term_end',
        'deadline',
        'details',
        'status',
        'applicants',
    ];

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

    protected $casts = [
        'applicants' => 'array'
    ];
}

接下来是我的迁移(我用的是文本格式,因为服务器上的DB比较老,不支持json)

    public function up()
    {
        Schema::create('recruitments', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->decimal('salary', 10, 2);
            $table->date('term_start');
            $table->date('term_end');
            $table->date('deadline');
            $table->longText('details');
            $table->string('status');
            $table->text('applicants')->nullable();
            $table->timestamps();
        });
    }

这是我的刀

<div class="container">
    <div class="row">
        <div class="card col-sm-12 py-3">
            <div class="card-header border d-flex justify-content-between align-items-center">
                <h3 class="w-75">{{ $job->title }}</h3>
                <div class="w-25">
                    <p class="my-0 my-0">Created at: <span class="text-info">{{ $job->created_at }}</span></p>
                    <p class="my-0 my-0">Last updated at: <span class="text-primary">{{ $job->updated_at }}</span></p>
                </div>
            </div>
            <div class="card-body">
                // display job details here

                <form action="{{ route('add-applicant', ['id' => $job->id ]) }}" method="POST" class="col-sm-12 d-flex justify-content-center align-items-center">
                    @csrf
                    <input type="text" name="user_id" id="user_id" value="{{ Auth::user()->id }}" hidden>
                    <button type="submit" class="btn btn-success w-25">Apply</button>
                </form>
            </div>
        </div>
    </div>
</div>

最后是我的控制器

public function addApplicant($id, Request $reqst){
        $job = Recruitment::find($id);
        $user[] = $reqst->user_id;

        $job->applicants = $user;
        $job->save();

        return redirect()->back();
    }

虽然此控制器将能够保存一个数组,但不幸的是它会覆盖已经存在的数组(假设应用了第二个用户)。当我尝试使用 array_push 时,它什么也没做,而且我仍然在数组中只有一个值。

抱歉,这有点读过,但我感谢我在这方面获得的任何帮助。谢谢

【问题讨论】:

  • 不要存储数组。更好地为您的申请人建立模型和关系
  • 这个 $job->applicants = $user; - 应该是 $job->applicants = $job->applicants.",".$user; .这将在新的申请人 id 前面附加一个逗号。这里可能有重复的 ID,您可能需要检查。

标签: php laravel database


【解决方案1】:

试试

public function addApplicant($id, Request $reqst){
        $job = Recruitment::find($id);

        $job->applicants = $reqst->user_id;
        $job->save();

        return redirect()->back();
    }

在招聘模式中应该有

public function user()
    {
        return $this->belongsTo(User::class, 'applicants');
    }

和迁移文件

$table->integer('applicants')->nullable();

【讨论】:

    猜你喜欢
    • 2017-09-16
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多