0

I am looking to create multiple views from IF statement inside controller as I have a platform which needs to be able to show different pages with tables on them. so the formula would be IF table contains "MO" show TextMO.blade.php but I don't know how to do it? I have updated my table but am still getting an "undefined constant" when I hover over the "table contains" and then a semicolon expected when hovering "MO"

My controller:

<?php

namespace App\Http\Controllers;

use App\Message;
use App\Suggestion;
use Carbon\Carbon;
use Google\Auth\ApplicationDefaultCredentials;
use Google\Cloud\PubSub\PubSubClient;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Storage;
use Propaganistas\LaravelPhone\PhoneNumber;

class DetailsController extends BaseController
{

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

            if (table contains MO) {
            return view('textMO', compact('any_variable'));
        else
            if (table contains MT) {
                return view('fileMT', compact('any_variable'));
            }
        }
    }
}

enter image description here

My route:

Route::get('/messages/{id}', 'DetailsController@index')->name('Index');

My Main Blade:

@if(table contains MO)
            @include('partials.textMO')
        @elseif(table contains MT)
            @include('partials.fileMT')
        <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">IN/OUT</th>
                    <th scope="col">SENT TIME</th>
                    <th scope="col">TYPE</th>
                    <th scope="col">SUGGESTION</th>
                    <th scope="col">RESPONSE</th>
                    <th scope="col">STATUS</th>
                </tr>
            </thead>

            <tbody>
                <tr>
                    <td style='font-size:14px'>{{$message->id}}</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->created_at}} </td>

                    <td>{{$message->content->text}}</td>
                    <td>{{$message->suggestion}}</td>
                    <td>{{$message->response}}</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>
        @endif

1 Answer 1

1

You exactly have your partials in a folder that's great. Now, in your view, do something like this:

@if(table contains MO)
    @include('partials.textMO')
@elseif(table contains MT)
    @include('partials.fileMT')
...

Or just do it in your controller with some if/elseif statements and return the desired view instead of returning 'details' view.

UPDATE: Code in controller

So, you want to insert your partials in your view with the controller, here's how I'd do it:

if ($message->direction == "mo" && $message->type == "text")
    return view('textMO', compact('any_variable'));
elseif($message->direction == "mt" && $message->type == "text")
    return view('fileMT', compact('any_variable'));
...

If you do so, the details view won't be rendered because you are rendering some partials. The only and optimal way would be like I said before this update, to do IF statements in the blade to include the partials you want.

UPDATE 2: There is a simpler solution for this, but you need to make sure your files have a certain naming convention and you need to follow it. If you don't Laravel won't find them, here's the solution:

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

This would return the good file with the following naming convention: <direction><type>.blade.php

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

9 Comments

Would you be able to show me the code which I put inside my controller, it's more specific for my case to go into the controller then the view
See the updated version of my answer. It's basically the same thing, but I think the best way for you to insert your partials in your view would be to do IF statements in your blade directly.
I added it to the blade as the main one are you able to briefly have a look and see if I did it right? It's in my updated question
You have to do it on one or the other. I think in your situation, you want the IF statements in the blade views because you ALWAYS want to render the details view but the partials should appear only if the condition is satisfied right? If so, remove the IFs i your controller and only keep the ones in your blade and it should work. Obviously, the condition I wrote is not good, and you need to put a realistic one, mine was only an example.
I see. I want to do a return view, not an include view, the blades are separate from each other as the columns are different. What I have is a message log blade and then Five or so partial blades which all have a table but the columns and data are different depending on each one so I want the user to be able to click on it a button to view the message and the IF statement. The button is a view details so the user can read the message. That's why the controller feels like the best way as it goes from the message log view and then with one click goes to either one of the blades
|

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.