Reputation: 682
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'));
}
}
}
}
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
Upvotes: 0
Views: 1907
Reputation: 350
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
Upvotes: 1