Laravel Inertia 道具不传递值

Laravel Inertia prop not passing values

调用编辑函数时,道具 mealService 传递空值,不会用值填充表单字段。看起来控制器没有加载模型来查询单个记录。创建和存储功能工作正常。

第一次发帖。对编码非常陌生。如果问题需要更多信息,请告诉我。

edit.vue

export default {
  components: {
    Head,
    Link,
    LoadingButton,
    SelectInput,
    TrashedMessage,
  },
  layout: Layout,
  props: {
    mealService: Object,
    sites: Array,
  },
  remember: 'form',
  data() {
    return {
      form: this.$inertia.form({
        site_id: this.mealService.site_id,
        meal_type: this.mealService.meal_type,
        adults: this.mealService.adults,
        tally: this.mealService.tally,       
      }),
    }
  },

膳食服务控制器

public function edit(MealService $meal_service)
    {
        return Inertia::render('MealServices/Edit', [
            'mealService' => [
                'id' => $meal_service->id,
                'site_id' => $meal_service->site_id,
                'meal_type' => $meal_service->meal_type,
                'adults' => $meal_service->adults,
                'tally' => $meal_service->tally,
            ],
            'sites' => Auth::user()->sfa
                ->sites()
                ->orderBy('name')
                ->get()
                ->map
                ->only('id', 'name'),
        ]);
    }

餐饮服务模式

class MealService extends Model
{
    use HasFactory;
    use SoftDeletes;

    public function resolveRouteBinding($value, $field = null)
        {
            return $this->where($field ?? 'id', $value)->withTrashed()->firstOrFail();
        }

    public function site()
    {
        return $this->belongsTo(Site::class);
    }

    public function scopeFilter($query, array $filters)
    {
        $query->when($filters['search'] ?? null, function ($query, $search) {
            $query->WhereHas('site', function ($query) use ($search) {
                $query->where('name', 'like', '%'.$search.'%');
            });
        })->when($filters['trashed'] ?? null, function ($query, $trashed) {
            if ($trashed === 'with') {
                $query->withTrashed();
            } elseif ($trashed === 'only') {
                $query->onlyTrashed();
            }
        });
    }
}

路线

Route::get('mealServices/{mealService}/edit', [MealServicesController::class, 'edit'])
->name('mealServices.edit')
->middleware('auth');

在您的编辑功能中您使用 meal_service 而在您的路线中您使用 mealService

也尝试在编辑功能中命名 mealService