Edward Palen
Edward Palen

Reputation: 565

Undefined property: Illuminate\Support\Collection::$id in Laravel 5.2

I have a confusion and I need a help to solve.

I am trying to add a record to a table that depends on the initial record, it is an event log table related to the initial registration of a call record.

As I try to do this it is with an input hidden, but it does not receive the id and it generates the following error (Undefined property:Illuminate\Support\Collection::$id).

This is part of the view with the record button of the incidences.

@foreach ($data as $call)
<tr class="active">
    <td align="center">{{ ++$i }}</td>
    <td style="text-align: center">{{ $call->created_at->format('d - m - Y') }}</td>
    <td>{{ $call->name }}</td>
    <td>{{ $call->last_name }}</td>
    <td align="center">
        @if($call->type == 1)         
            <span class="label label-info">Saliente</span>        
        @else         
            <span class="label label-success">Entrante</span>        
        @endif
    </td>
    <td>{{ $call->phone }}</td>
    <td>{{ $call->movil }}</td>
    <td align="center">

        <a class="btn btn-info btn-xs" href="{{ route('calls.show',$call->id) }}" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Detalle de llamada"> <i class="material-icons">info_outline</i> </a>

        @permission('role-edit')
        <a class="btn btn-primary btn-xs" href="{{ route('calls.edit',$call->id) }}" data-toggle="tooltip" data-placement="top" title="Editar registro de llamada"> <i class="material-icons">create</i> </a>
        @endpermission

        <a class="btn btn-warning btn-xs" href="{{ route('comments.create', $call->id) }}" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Registrar incidencia"> <i class="material-icons">event</i> </a>

        {!! Form::open(['method' => 'DELETE','route' => ['calls.destroy', $call->id],'style'=>'display:inline']) !!}

            @permission('role-delete')
            <button type="submit" class="btn btn-danger btn-xs" data-toggle="tooltip" data-placement="top" title="Eliminar llamada"><i class="material-icons delete-white">delete</i></button>
            @endpermission

        {!! Form::close() !!}
    </td>
</tr>
@endforeach

This is part of the view where the issue is logged, but I need to pass the id in the hidden field.

{!! Form::open(array('route' => 'comments.store','method' => 'POST')) !!}

    <div class="col-md-12 col-xs-12">
        <div class="input-group">
            <div class="col-md-4 col-xs-4">
                {!! Form::select('call_id', $calls, null, ['class' => 'form-control', 'placeholder' => 'Seleccionar cliente']) !!} <!--This is the select-->

                {{ Form::hidden('call_id', $calls->id) }} <!--This is the hidden mode-->
            </div>
            <div class="col-md-8 col-xs-8">
                {!! Form::text('name', null, array('placeholder' => 'Registrar incidencia','class' => 'form-control')) !!}
            </div> 
            <span class="input-group-btn">                  
                <button type="submit" class="btn btn-success btn-xs" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Guardar">
                    <i class="material-icons">save</i>
                </button> 
            </span>
        </div>
    </div>

{!! Form::close() !!}

The error generated by the view is:

Undefined property: Illuminate\Support\Collection::$id

These are my methods for the controller (CommentController).

public function create()
    {
        $calls = Call::orderBy('id', 'asc')->lists('name', 'id');

        return view('comments.create', compact('calls'));
    }


public function store(Request $request)
{
    //return $request->all();

    $this->validate($request, [
      'name'        => 'required|unique:categories|max:255',
    ]);

    $comments = Comment::create([
        'name'          => $request->get('name'),
        'call_id'       => $request->get('call_id'),
    ]);

    return redirect()->route('comments.index')
                    ->with('success','Comentario agregado correctamente!!!');
}

This is my route method.

Route::resource('comments','CommentController');

This is the call log view, when clicking the orange button calls the comment view to record an issue.

enter image description here

This is the view of record of incidents, here I have the dropdown but ideally instead of a dropdown can receive the ID of the record you select from the previous view.

enter image description here

Someone who can guide me, since I have used several methods and I have not been able to solve it.

Upvotes: 2

Views: 2579

Answers (2)

Edward Palen
Edward Palen

Reputation: 565

Solve the confusion and method as follows:

The button to create the incidence in the view I defined it as follows:

@foreach ($data as $call)

<a class="btn btn-warning btn-xs" href="{{ route('comments.create', ['id' => $call->id]) }}" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Registrar incidencia"> <i class="material-icons">event</i> </a>

@endforeach

In the create method of CommentController I structured it as follows:

public function create($id)
{
    $calls = DB::table('calls')->find($id);

    return view('comments.create', compact('calls'));
}

The store method of CommentController I structured it as follows:

public function store(Request $request)
{
    //return $request->all();

    $rules = [
        'call_id'                   => 'required',
        'comments_name'             => 'required',
    ];

    $messages = [
        'call_id.required'           => 'Debe seleccionar un código de llamada',
        'comments_name.required'    => 'Debe ingresar incidencia',
    ];

    $this->validate($request, $rules, $messages);       

    $comments = Comment::create([
        'comments_name'     => $request->get('comments_name'),
        'call_id'           => $request->get('call_id'),
    ]);

    return redirect()->route('calls.index')
                    ->with('success','Incidencia agregada correctamente!!!');
}

The labels in the view create incidents are as follows:

{!! Form::open(array('route' => 'comments.store','method' => 'POST')) !!}

    <div class="col-md-12 col-xs-12">
        <div class="input-group">
            <div class="col-md-12 col-xs-12">
            {!! Form::text('comments_name', null, array('placeholder' => 'Nombres','class' => 'form-control')) !!} 
            {{ Form::hidden('call_id', $calls->id) }}
            </div> 
            <span class="input-group-btn"> 
                <a class="btn btn-warning btn-xs" href="{{ route('calls.index') }}" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Retornar">
                    <i class="material-icons">arrow_back</i>
                </a>   

                <button type="submit" class="btn btn-success btn-xs" data-toggle="tooltip" rel="tooltip" data-placement="top" title="Guardar">
                    <i class="material-icons">save</i>
                </button> 
            </span>
        </div>
    </div>

{!! Form::close() !!}

And I defined the following route in the routes file:

Route::get('comments/create/{id}', [
    'middleware' => 'auth',
    'as' => 'comments.create',
    'uses' => 'CommentController@create'
]);

This way you can create a call log with a list of many incidents.

If this method should be improved please give me the recommendations, but it was the way in which I could do it.

Thanks @RobFonseca and @CarlosAdames for your comments and help.

Upvotes: 2

Carlos Adames
Carlos Adames

Reputation: 178

The lists method returns a collection instance. You need to convert the Collection into a plain array using the all method.

In your controller try this:

$calls = Call::orderBy('id', 'asc')->lists('name', 'id')->all();

You can read more about it in the following link The lists Method

Let me know if your problem is solved.

Upvotes: 1

Related Questions