error when sending array to vista laravel

1

I have an error and look for several ways to send the array and in the same error mark me when sending the array view. I'm sending a parameter to the method putEdit ($ id) and this should make me a query to the database and return it to the edit view but I get that the movie variable is not defined in the view.

Auth::routes();
Route::group(['middleware' => 'auth'], function() {
    Route::get('catalog','CatalogController@getIndex');
    Route::get('show/{id}','CatalogController@getShow');
    Route::get('create','CatalogController@getCreate');
    Route::get('edit/{id}','CatalogController@getEdit');
    Route::put('edit/{id}','CatalogController@putEdit');
    Route::post('create','CatalogController@postCreate');
});

@extends('layouts.master')

@section('content')

<div class="row" style="margin-top:20px">

    <div class="col-md-offset-3 col-md-6">

        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title text-center">
                    <span class="glyphicon glyphicon-film" aria-hidden="true"></span>
                    Añadir película
                </h3>
            </div>

            <div class="panel-body" style="padding:30px">

                {{-- TODO: Abrir el formulario e indicar el método POST --}}
                <form method="put">
                    
                    {{-- TODO: Protección contra CSRF --}}
                            {{ csrf_field() }}
                    <div class="form-group">
                        <label for="title">Modificar Título</label>
                        <input type="text" name="title" id="title" class="form-control" value="{{$pelicula->title}}">
                    </div>

                    <div class="form-group">
                        {{-- TODO: Completa el input para el año --}}
                        <label for="title">Año</label>
                        <input type="text" name="fecha" id="fecha" class="form-control">
                    </div>

                    <div class="form-group">
                        {{-- TODO: Completa el input para el director --}}
                        <label for="title">Director</label>
                        <input type="text" name="director" id="director" class="form-control">
                    </div>

                    <div class="form-group">
                        {{-- TODO: Completa el input para el poster --}}
                        <label for="imagen">Sube la imagen:</label>
                        <input type="file" name="imagen" id="imagen">
                    </div>

                    <div class="form-group">
                        <label for="synopsis">Resumen</label>
                        <textarea name="synopsis" id="synopsis" class="form-control" rows="3"></textarea>
                    </div>

                    <div class="form-group text-center">
                        <button type="submit" class="btn btn-primary" style="padding:8px 100px;margin-top:25px;">
                            Modificar película
                        </button>
                    </div>
                    {{ method_field('PUT') }}
                {{-- TODO: Cerrar formulario --}}
                </form>

            </div>
        </div>
    </div>
</div>
@stop
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Movie;
use DB;


class CatalogController extends Controller
{   

    public function getIndex(){
        $arrayPeliculas = DB::table('movies')->get();
        return view('videoclub/index',[
            'arrayPeliculas' => $arrayPeliculas
            ]);
    }

    public function getShow($id){
        $this->id = Movie::findOrFail($id);
        return view('videoclub/show', array('id'=>$this->id) );;
    }

    public function getCreate(){
        return view('videoclub/create');
    }

    public function getEdit($id){
        $this->id = Movie::findOrFail($id);
        return view('videoclub/edit', array('id'=>$this->id) );
    }

    public function putEdit($id){
        $pelicula = DB::table('movies')->where('id', $id)->get();
        return view('videoclub/edit',[
            'pelicula' => $pelicula
            ]);
    }
}
    
asked by Carlos Enrique Gil Gil 24.08.2017 в 01:54
source

3 answers

0

The problem seems to be that you're using get (), which gets a collection of elements (even if in your case it's just the result of the query).

You should then use the first() method to get only one element / object and not a collection of these:

$pelicula = DB::table('movies')->where('id', $id)->first();
    
answered by 24.08.2017 в 06:59
0

I usually return the views as follows:

return view('videoclub/edit')-> with(['pelicula' => $pelicula]);

I would also check if the movie exists:

public function putEdit($id){
    $pelicula = DB::table('movies')->where('id', $id)->get();
    if (!is_object($pelicula)) {
        //TODO: Avisar del error al usuario
        // Ya sea con un mensaje una vista especial, redirect a index...
        return view('videoclub/error')
    }
    return view('videoclub/edit')->with([
        'pelicula' => $pelicula
        ]);
}
    
answered by 24.08.2017 в 13:54
0

The error is because you are calling the variable "movie" in the view and you are not sending the data with that name. You can see it in this line

<input type="text" name="title" id="title" class="form-control" value="{{$pelicula->title}}">

What I would do is:

public function getEdit($id){
    $pelicula = Movie::findOrFail($id);
    return view('videoclub/edit')->with('pelicula',$pelicula);
}

And in the view then if you can do {{$ movie-> title}} since you are sending the model with that name. I hope you have been helpful. regards Maru

    
answered by 25.08.2017 в 17:06