-1

I am stuck for 1 day on a problem. I have an error message, in my file edit.blade.php

"Undefined variable: marks (View: C:\wamp64\www\azerty\resources\views\admin\series\edit.blade.php)"

Small screenshot below

enter image description here

My problem is at the line 33, perhaps the loop ?

Here is my file edit.blade.php

form class="panel-body" action="{{route('series.update',$series->id)}}" method="POST">
<input name="_method" type="hidden" value="PATCH">
@csrf
<fieldset class="form-group">
<label for="form-group-input-1">Name</label>
<input type="text" name="name" class="form-control" id="form-group-input-1" value="{{$series->name}}">
</fieldset>

<div class="form-group">
<label for="company-content">Select Mark</label>
<select name="fk_mark" id="" class="form-control">
@foreach($marks as $mark)
<option value="{{$mark->id}}">{{$mark->name_mark}}
</option>
@endforeach
</select>
</div>

I also think my problem is in the SerieController?

public function edit($id)
    {
        $series = Serie::with('marks')->find($id);
        return view('admin.series.edit', compact('series'));
    }



    public function update(Request $request, $id)
    {
        $request->validate([
                'name' => 'required',
                'fk_mark' => 'required'
        ]);

        $series = Serie::with('marks')->find($id);
        $series->name = $request->get('name');
        $series->fk_mark = $request->get('fk_mark');
        $series->save();
        return redirect()->route('series.index')
                  ->with('success', 'updated successfully');
    }

For information, here is also my file index.blade.php

<table class="table">
<a class="btn btn-sm btn-success" href="{{ route('series.create') }}">Ajouter</a>
<thead>
<tr>
  <th>Serie Car</th>
  <th>Mark Car</th>
</tr>
</thead>
@foreach($series as $serie)
<tr>
<td> {{$serie->name}}</td>
<td> {{$serie->marks->name_mark}}</td> 
<td>
<form method="POST" action="{{ route('series.destroy', $serie) }} ">
<a class="btn btn-sm btn-warning" href="{{route('series.edit',$serie->id)}}">Editer</a>
@csrf
@method('DELETE')
<button type="submit" class="btn btn-sm btn-danger">Deleter</button>
</form>
</td>
</tr>
@endforeach
</table>

Thank you for your help.

user11124425
  • 961
  • 1
  • 11
  • 22

2 Answers2

1

Instead of $marks, it's $series->marks that is to be used in your foreach loop.

Thierry Maasdam
  • 924
  • 1
  • 8
  • 19
1

In your edit method in your controller you are not passing a variable called marks to the controller in order to use it. So in order to access it in the loop you should do it through your series item.

@foreach($series->marks as $mark)
...
nakov
  • 13,938
  • 12
  • 60
  • 110
  • Ok thank you... Concerning theses lines it's ok according you? ``` @endforeach``` – user11124425 Mar 09 '19 at 17:47
  • 1
    yes, those should be fine. – nakov Mar 09 '19 at 17:48
  • Sorry, I yet have an ask... Why, I have this error message please? `"Trying to get property 'id' of non-object ` – user11124425 Mar 09 '19 at 17:52
  • 1
    The message says it all. The problem is because the item is `null` or it is a non-object which means it might be array and you should access it as `$mark['id']` if the error is on the same line in the loop. – nakov Mar 09 '19 at 17:54
  • Ok thank you... I don't have of error message but my dropdown list is empty (not items)... I think my problem is now in my Controller? – user11124425 Mar 09 '19 at 17:59
  • 1
    yup, you've got to make sure that you retrieve the items as you expect, and that your relationship is setup correctly. – nakov Mar 09 '19 at 18:00