Reputation: 11
I have 6 views, I want to call a view based on a condition, in my case I'm loading contracts, each contract is within a specific view
How can I call the view contract_1.blade.php when contract_1 is selected? to explain this more, I have Contract class:
namespace App;
use \DateTimeInterface;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
class Contract extends Model implements HasMedia
{
use SoftDeletes;
use InteractsWithMedia;
use HasFactory;
public const TYPE_LOCATION_SELECT = [
'contract_1' => 'Contrat de bail meublée',
'contract_2' => 'Contrat de bail vide',
'contract_3' => 'Contrat de bail commercial',
'contract_4' => 'Contrat de bail saisonnier',
'contract_5' => 'Contrat de bail professionnel',
'contract_6' => 'Contrat de bail d\'habitation',
];
public $table = 'contracts';
protected $appends = [
'attachment',
];
protected $dates = [
'start_rent_date',
'end_rent_date',
'created_at',
'updated_at',
'deleted_at',
];
protected $fillable = [
'user',
'type_location',
'full_name',
'dob',
'identity',
'address',
'estate_type_id',
'estate_location',
'rent_period',
'start_rent_date',
'end_rent_date',
'equipments',
'superficie',
'etage',
'porte',
'charges',
'garantie',
'total_amount',
'created_at',
'updated_at',
'deleted_at',
];
public function registerMediaConversions(Media $media = null): void
{
$this->addMediaConversion('thumb')->fit('crop', 50, 50);
$this->addMediaConversion('preview')->fit('crop', 120, 120);
}
public function estate_type()
{
return $this->belongsTo(Estate::class, 'estate_type_id');
}
public function getStartRentDateAttribute($value)
{
return $value ? Carbon::parse($value)->format(config('panel.date_format')) : null;
}
public function setStartRentDateAttribute($value)
{
$this->attributes['start_rent_date'] = $value ? Carbon::createFromFormat(config('panel.date_format'), $value)->format('Y-m-d') : null;
}
public function getEndRentDateAttribute($value)
{
return $value ? Carbon::parse($value)->format(config('panel.date_format')) : null;
}
public function setEndRentDateAttribute($value)
{
$this->attributes['end_rent_date'] = $value ? Carbon::createFromFormat(config('panel.date_format'), $value)->format('Y-m-d') : null;
}
public function getAttachmentAttribute()
{
return $this->getMedia('attachment');
}
protected function serializeDate(DateTimeInterface $date)
{
return $date->format('Y-m-d H:i:s');
}
}
My generated pdf file is called getpdf.blade.php
{{$contract_type = \App\Contract::where(['location_type' => $contract->location_type])}}
@if ($contract_type == contract_1)
@extends('layouts.contract_1')
@else
extends('layouts.contract_2')
@endif
@section('content')
<div class="content">
<div class="row">
<div class="col-lg-12">
<div class="panel panel-default">
<div class="panel-heading">
{{ trans('global.show') }} {{ trans('cruds.contract.title') }}
</div>
<div class="panel-body">
<div class="form-group">
<div class="form-group">
<a class="btn btn-default" href="{{ route('admin.contracts.index') }}">
{{ trans('global.back_to_list') }}
</a>
<a class="btn btn-default" href="{{ route('admin.contracts.getpdf', $contract->id) }}">
{{ trans('global.datatables.pdf') }}
</a>
</div>
<table class="table table-bordered table-striped">
<tbody>
<tr>
<th>
{{ trans('cruds.contract.fields.id') }}
</th>
<td>
{{ $contract->id }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.user') }}
</th>
<td>
{{ $contract->user }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.type_location') }}
</th>
<td>
{{ App\Contract::TYPE_LOCATION_SELECT[$contract->type_location] ?? '' }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.full_name') }}
</th>
<td>
{{ $contract->full_name }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.dob') }}
</th>
<td>
{{ $contract->dob }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.identity') }}
</th>
<td>
{{ $contract->identity }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.address') }}
</th>
<td>
{{ $contract->address }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.estate_type') }}
</th>
<td>
{{ $contract->estate_type->estate_type ?? '' }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.estate_location') }}
</th>
<td>
{{ $contract->estate_location }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.rent_period') }}
</th>
<td>
{{ $contract->rent_period }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.start_rent_date') }}
</th>
<td>
{{ $contract->start_rent_date }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.end_rent_date') }}
</th>
<td>
{{ $contract->end_rent_date }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.equipments') }}
</th>
<td>
{{ $contract->equipments }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.superficie') }}
</th>
<td>
{{ $contract->superficie }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.etage') }}
</th>
<td>
{{ $contract->etage }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.porte') }}
</th>
<td>
{{ $contract->porte }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.charges') }}
</th>
<td>
{{ $contract->charges }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.garantie') }}
</th>
<td>
{{ $contract->garantie }}
</td>
</tr>
<tr>
<th>
{{ trans('cruds.contract.fields.attachment') }}
</th>
<td>
@foreach($contract->attachment as $key => $media)
<a href="{{ $media->getUrl() }}" target="_blank">
{{ trans('global.view_file') }}
</a>
@endforeach
</td>
</tr>
</tbody>
</table>
<div class="form-group">
<a class="btn btn-default" href="{{ route('admin.contracts.index') }}">
{{ trans('global.back_to_list') }}
</a>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
I'm getting this error:
ErrorException htmlspecialchars() expects parameter 1 to be string, object given (View: /var/www/app/resources/views/admin/contracts/getpdf.blade.php)
I'm not sure where the mistake is.
Upvotes: 0
Views: 89
Reputation: 8082
Your error is on the first line, you are literall doing {{ $variable = object }}
but that object
is not stringable, so it does not know how to turn the object to a string.
So, this:
{{$contract_type = \App\Contract::where(['location_type' => $contract->location_type])}}
should be:
@php
$contract = \App\Contract::where(['location_type' => $contract->location_type])->first();
@endphp
And then, the if
:
@if ($contract_type == contract_1)
should be:
@if ($contract->type_location == "contract_1")
Have in mind that ->first()
could return no object (null
), so doing $contract->type_location
could throw an error. In that case, I would recommend to check how to solve those types of issues depending what PHP version you have.
Upvotes: 2