Skip to content
Advertisement

Laravel Undefined variable: messages

I am getting an undefined variable error after I have added this into my controller:

return view($message->direction . $message->type, compact($message));

I have a table with a details button the idea being it goes to a more advanced view and detailed to the specific message, I am having issues with it as it’s giving me an Undefined variable: message (View: C:UsersBradley KirklandDownloadsrcs-masterrcsresourcesviewsmttext.blade.php)

@extends ('layout')
@section('content')



    <script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <link href="https://nightly.datatables.net/css/jquery.dataTables.css" rel="stylesheet" type="text/css" />
    <script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>

    <script src="https://code.jquery.com/jquery-3.3.1.js"> </script>
    <script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/fixedheader/3.1.6/js/dataTables.fixedHeader.min.js"></script>

    <div class="row align-items-center">

        <div class="col">
            <h1>
                RCS Proof of Concept
            </h1>
        </div>
        <div class="col ">
            <a href="/" class="btn btn-primary">BACK</a>
        </div>
    </div>

    <div class="form-group">
        <table id="userTable" data-page-length='5' cellspacing="0"
               class="table table-bordered table-striped table-hover table-condensed"
               role="grid">
            <thead>
                <tr>
                    <th scope="col">MESSAGE ID</th>
                    <th scope="col">MSISDN</th>
                    <th scope="col">TYPE</th>
                    <th scope="col">MO/MT</th>
                    <th scope="col">SENT/RECEIVED</th>
                    <th scope="col">RESPONSE</th>
                    <th scope="col">STATUS</th>
                </tr>
            </thead>

            <tbody>
                <tr>
                    <td>{{$message->created_at}} </td>
                    <td>{{$message->msisdn}}</td>
                    <td class="text-center">
                        @if ($message->direction == 'mo')
                            <span class='badge badge-warning'>mo</span>
                        @else
                            <span class='badge badge-success'>{{$message->direction}}</span>
                        @endif

                    </td>
                    <td>{{$message->type}}</td>
                    <td class="text-center">
                        @if ($message->status == 'NOK')
                            <span class='badge badge-danger'>NOK</span>

                        @elseif ($message->status == 'received')
                            <span class='badge badge-info'>received</span>

                        @elseif ($message->status == 'delivered')
                            <span class='badge badge-primary'>delivered</span>

                        @elseif ($message->status == 'queued')
                            <span class='badge badge-warning'>queued</span>

                        @elseif ($message->status == 'read')
                            <span class='badge badge-success'>read</span>

                        @elseif ($message->status == 'sent')
                            <span class='badge badge-success'>sent</span>

                        @endif
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
@endsection

and my full controller is:

<?php

namespace AppHttpControllers;

use AppMessage;
use AppSuggestion;
use CarbonCarbon;
use GoogleAuthApplicationDefaultCredentials;
use GoogleCloudPubSubPubSubClient;
use GuzzleHttpClient;
use GuzzleHttpHandlerStack;
use IlluminateDatabaseEloquentModelNotFoundException;
use IlluminateFoundationAuthAccessAuthorizesRequests;
use IlluminateFoundationBusDispatchesJobs;
use IlluminateFoundationValidationValidatesRequests;
use IlluminateHttpRequest;
use IlluminateRoutingController as BaseController;
use IlluminateSupportFacadesStorage;
use mysql_xdevapiTable;
use PropaganistasLaravelPhonePhoneNumber;

class DetailsController extends BaseController
{

    public function index($id)
    {
        $message = Message::find($id);

        return view($message->direction . $message->type, compact($message));
    }
}

Advertisement

Answer

This is right syntax to pass variable to use compact. Please Try this:

return view($message->direction . $message->type, compact('message'));
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement