Home > OS >  Laravel Update DB with array from views
Laravel Update DB with array from views

Time:08-19

i'm doing a simple project but having some difficulties with updating my database with values that I get from a form. The project is having a list of movies and when you click on one it would take you to a page with more details. Theres a button which you can update the details of that movie. I'm trying save whatever details are made to the relevant index of the table using the id. I can't seem to get it to work though. I did something similar when creating a new entry but having some difficulty updating the values of a specific movie/page. Thanks for any help! Route:

Route::get('catalog', 'App\Http\Controllers\CatalogController@getIndex');

Route::get('catalog/show/{id}', 'App\Http\Controllers\CatalogController@getShow');

Route::get('catalog/create', 'App\Http\Controllers\CatalogController@getCreate');

Route::get('catalog/edit/{id}', 'App\Http\Controllers\CatalogController@getEdit');

Route::post('catalog/create', 'App\Http\Controllers\CatalogController@postCreate');

Route::put('catalog/edit/{id}','App\Http\Controllers\CatalogController@putEdit');

Controller:

public function getEdit($id)
{
    return view('catalog.edit', ['arrayPeliculas' => Movie::all()]);
}
public function getCreate()
{
    return view('catalog.create');
}

public function postCreate(Request $request)
{
    $Movie = new Movie;
    
    $Movie->title = $request->title;
    $Movie->year = $request->year;
    $Movie->director = $request->director;
    $Movie->poster = $request->poster;
    $Movie->synopsis = $request->synopsis;

    $Movie->save();

    return redirect()->action('App\Http\Controllers\CatalogController@getIndex');
}
public function putEdit(Request $request, $id)
{
    $Movie = new Movie;
    
    $Movie[$id]->title = $request->title;
    $Movie[$id]->year = $request->year;
    $Movie[$id]->director = $request->director;
    $Movie[$id]->poster = $request->poster;
    $Movie[$id]->synopsis = $request->synopsis;

    $Movie->save();

    return redirect()->action('App\Http\Controllers\CatalogController@getIndex');
}

Edit page:

<form method="PUT">
            {{ method_field('PUT') }}
        {{-- TODO: Protección contra CSRF --}}
            {{ csrf_field() }}
        <div >
           <label for="modificar">Modificar Pelicula</label>
           <input type="text" name="title" id="title"  value="{{ $arrayPeliculas[$id]->title }}">
        </div>

        <div >
           {{-- TODO: Completa el input para el año --}}
           <label for="Año">Año</label>
           <input type="text" name="year" id="year"  value="{{ $arrayPeliculas[$id]->year }}">
        </div>

        <div >
           {{-- TODO: Completa el input para el director --}}
           <label for="Director">Director</label>
           <input type="text" name="director" id="directo"  value="{{ $arrayPeliculas[$id]->director }}">
        </div>

        <div >
           {{-- TODO: Completa el input para el poster --}}
           <label for="Poster">Poster</label>
           <input type="text" name="poster" id="poster"  value="{{ $arrayPeliculas[$id]->poster }}">
        </div>

        <div >
           <label for="synopsis">Resumen</label>
           <textarea name="synopsis" id="synopsis"  rows="3" >{{ $arrayPeliculas[$id]->synopsis }}"</textarea>
        </div>

        <div >
           <button type="submit"  style="padding:8px 100px;margin-top:25px;">
               Añadir película
           </button>
        </div>
        </form>

I tried a few other things like->update but I can't seem to get it to work properly.

CodePudding user response:

To update an existing model, first find() it.


public function putEdit(Request $request, $id)
{
    $Movie = Movie::find($id);
    
    $Movie->title = $request->title;
    $Movie->year = $request->year;
    $Movie->director = $request->director;
    $Movie->poster = $request->poster;
    $Movie->synopsis = $request->synopsis;

    $Movie->save();

    return redirect()->action('App\Http\Controllers\CatalogController@getIndex');
}

Using the update function should also work then:

public function putEdit(Request $request, $id)
{
    $Movie = Movie::find($id);

    $Movie->update([
       'title' => $request->title,
       'year' => $request->year,
       'director' => $request->director,
       'poster' => $request->poster,
       'synopsis' => $request->synopsis,
    ]);

    return redirect()->action('App\Http\Controllers\CatalogController@getIndex');
}

You could really simplify it with some Laravel magic like Route Model Binding...

Route::put('catalog/edit/{movie}','App\Http\Controllers\CatalogController@putEdit');

...

public function putEdit(Request $request, Movie $movie)
{
    $movie->update($request->all());

    return redirect()->action('App\Http\Controllers\CatalogController@getIndex');
}

  • Related