在我的laravel应用程序中,我有一个表来存储作业的deatails。
以下是我的Job.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Job extends Model
{
use HasFactory;
// Define the table associated with the model
protected $table = 'jobs';
// The attributes that are mass assignable
protected $fillable = [
'company_id',
'job_title',
'employment_type',
'primary_tag',
'job_description',
'how_to_apply',
'seo_support',
'premium_support',
'show_logo',
'highlight_post',
'sticky_24_hours',
'sticky_1_week',
'sticky_1_month',
'minimum_salary',
'maximum_salary',
'compensation_type',
'benefits',
'seo_title',
'seo_meta_desc',
'user_id',
'invoice_email',
'is_published',
'is_active',
'is_expired',
'paid_amount',
'is_paid',
'views',
'timeDifference',
];
// Cast attributes to appropriate data types
protected $casts = [
'seo_support' => 'boolean',
'premium_support' => 'boolean',
'show_logo' => 'boolean',
'highlight_post' => 'boolean',
'sticky_24_hours' => 'boolean',
'sticky_1_week' => 'boolean',
'sticky_1_month' => 'boolean',
'benefits' => 'array',
'secondary_tag' => 'array',
'is_published' => 'boolean',
'is_active' => 'boolean',
'is_expired' => 'boolean',
'is_paid' => 'boolean',
];
public function company()
{
return $this->belongsTo(Company::class, 'company_id');
}
public function employmentType()
{
return $this->belongsTo(EmploymentType::class, 'employment_type');
}
public function primaryTag()
{
return $this->belongsTo(PrimaryTag::class, 'primary_tag');
}
public function secondaryTags()
{
return $this->belongsToMany(SecondaryTag::class, 'job_secondary_tag', 'job_id', 'secondary_tag_id');
}
}
以下是我的SecondaryTag.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class SecondaryTag extends Model
{
protected $table = 'secondary_tags';
protected $fillable = [
'tag_name',
];
// Define any relationships or additional methods here
}
当我创建新作业时,我的辅助标记以数组格式存储。
Example: ["1","3","4","13","16"]
现在我正在做编辑工作。我在我的编辑工作刀片上尝试了这个
<!-- Secondary Tags (Checkboxes) -->
<div class="mb-3">
<label class="form-label">Other Tags, Keywords, Stack</label>
<div class="row">
<!-- Loop through secondary tags and create checkboxes -->
@foreach ($secondaryTags as $secondaryTag)
<div class="col-md-3">
<div class="form-check">
<input class="form-check-input" type="checkbox"
id="secondary_tag_{{ $secondaryTag->id }}" name="secondary_tags[]"
value="{{ $secondaryTag->id }}"
{{ $job->secondaryTags->contains($secondaryTag->id) ? 'checked' : '' }}>
<label class="form-check-label" for="secondary_tag_{{ $secondaryTag->id }}">
{{ $secondaryTag->tag_name }}
</label>
</div>
</div>
@endforeach
</div>
</div>
尽管这会打印所有辅助标记,但不会选中已选中的复选框。
在我前面的例子中,带有[“1”,“3”,“4”,“13”,“16”] id的复选框应该被选中。
但他们仍然不受控制。
以下是我的控制器功能
public function edit(Job $job)
{
// Fetch the secondary tags and benefits to pass to the view
$primaryTags = PrimaryTag::all();
$employerTypes = EmploymentType::all();
$secondaryTags = SecondaryTag::all();
$benefits = Benefit::all();
// Retrieve the company details for editing
return view('profile/org/jobs/edit', compact('job', 'secondaryTags', 'benefits'));
}
我做错了什么,我如何正确显示检查的旧值?
1条答案
按热度按时间ix0qys7i1#
为什么要把事情搞复杂?您可以充分利用刀片模板。试着像
@checked($job->secondaryTags->contains($secondaryTag->id))
希望这对你有帮助。