-1

There are two modals attributes & its options

the problem now in options edit blade

This part of code was work correctly in create.blade

**but in edit.blade ** show this error.

Then I try to add this variable to the controller in several ways but still the same error appears

you can find my codes down, please help me

edit.blade

<div class="form-body">
    <h4 class="form-section">
        <i class="ft-home"></i> Option name
    </h4>
    <div class="row">
        <div class="col-md-6">
            <div class="form-group">
                <label for="projectinput1"> Choose a feature
                </label>
                <select name="attribute_id" class="select2 form-control" >
                    <optgroup label="Choose a feature ">
                        @if($attributes && $attributes -> count() > 0)
                            @foreach($attributes as $attribute)
                                <option
                                    value="{{$attributes -> id }}">{{$attributes -> name}}</option>
                            @endforeach
                        @endif
                    </optgroup>
                </select>
                @error('attribute_id')
                <span class="text-danger"> {{$message}}</span>
                @enderror
            </div>
        </div>

attribute model

public function options() {
    return $this->hasMany(Option::class, 'attribute_id');
}

option model

public function attribute() {
    return $this->belongsTo(Attribute::class, 'attribute_id');
}

controller

public function edit($id)
{
    //get specific options and its translations

    $option = Option::orderBy('id', 'DESC')->find($id);
    if (!$option)
        return redirect()->route('admin.options')->with(['error' => 'this option is not available']);

    return view('dashboard.options.edit', compact('option'));
}

public function update($id, OptionsRequest $request)
{
    try {
        //validation

        //update DB

        $option = Option::find($id);
        if (!$option)
            return redirect()->route('admin.options')->with(['error' => 'this option is unavailable']);

        if (!$request->has('is_active'))
            $request->request->add(['is_active' => 0]);
        else
            $request->request->add(['is_active' => 1]);

        $option->update($request->except('_token', 'id'));

        //save translations
        $option->name = $request->name;
        $option->save();

        DB::commit();

        return redirect()->route('admin.options')->with(['success' => 'success']);
    } catch (\Exception $ex) {
        return redirect()->route('admin.options')->with(['error' => 'fail, try again later']);
    }
}
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
Dina
  • 1
  • 2
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Apr 26 '21 at 23:09

2 Answers2

0

i cannot find any $attributes variable in your controller that you are sending to your blade!!

so when you dont send data to your blade, it is impossible to use that data in blade

send it to blade

or you can put your $options in @foreach and then get every option's attribute like this:

@foreach($options as $option)
    $option->attributes
@endforeach
0

in edit method there no $attributes, you should write $option in place of $attributes edit.blade.php, f