3

Would you please let me know if this controller SerieController is correct? As I don't think so. There are two models in fact. One Model is Serie and the other is Mark.

When, I click on the drop down list, I don't see my items...

enter image description here

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');
    }

My 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($series->marks as $mark)
<option value="{{$mark['id']}}">{{$mark['name_mark']}}</option>
@endforeach
</select>
</div>

Concerning the model

Model Serie

class Serie extends Model
{

    protected $fillable = ['name', 'fk_mark'];

    public function marks(){

        return $this->belongsTo('App\Mark', 'fk_mark');
       
    }

}

Model Mark

class Mark extends Model
{

    protected $fillable = ['name_mark'];
    
    public function series(){
        return $this->hasMany('App\Serie', 'fk_mark');
    }

    
    public function cars(){
     return $this->hasMany('App\Car','fk_serie');
   }

}

Thank you in advance.

6
  • Why do you want to use the same controller for two different models? Commented Mar 9, 2019 at 9:47
  • You can name controller's methods however you want. Even, for example, editMark or editSerie. Just do not forget to bind these methods to urls in routes/api.php or routes/web.php. Commented Mar 9, 2019 at 9:49
  • @Ross Wilson: Because there is a foreign key. Commented Mar 9, 2019 at 10:04
  • what is the relationship between two models? Commented Mar 9, 2019 at 11:07
  • And also show the database structure of these tables? Commented Mar 9, 2019 at 11:08

2 Answers 2

2

In your edit function you have to do this:

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

and in your blade you will fetch the marks like this:

$series->marks->name_mark;

and it is the same method for update function to update two tables data.

I hope it would helpful.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a lot for the synthax of the methode edit. Do you have an idea if the syntax of the method update is correct ? In my drop down list I don't get the items... :-( I have edited my first message.
0

First of all: Which version of Laravel do you use?

Check out route model binding which would compact your functions like this:

public function edit(Serie $serie)
{
    $series->load('marks');
    return view('admin.series.edit', compact('series'));
}

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

    $series->load('marks');

    $series->name = $request->get('name');
    $series->fk_mark = $request->get('fk_mark');
    $series->save();

    return redirect()->route('series.index')
              ->with('success', 'updated successfully');
}

Further tipps for debugging:

  1. Set 'debug' => true in your app.php
  2. Try to use dd($serie); to see what happens to your model.
  3. Install debugbar: composer require barryvdh/laravel-debugbar --dev
  4. Check your database tables to see if your code successfully created the data.
  5. Write Unit/Feature Test to validate your application logic
  6. Provide more data in your next SO question like sql schema, error messages etc

Without the sql schema and exact problem you're facing it's very hard to debug someone else code. I hope this helps nonetheless.

Edits: grammar, spelling and last sentence fixed/added

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.