Laravel Blade Helpers not pulling in columns with spaces - laravel

I have a table with many columns and some of those columns have spaces in their names (i.e. 'Provider First Name'). I know the syntax to use these in Blade helpers and am using this in other parts of my app: {{$provider->{'Provider First Name'} }}. This works fine in other parts.
I have the following in my ProviderController:
public function show($id)
{
$provider = NPIData::where('NPI', $id)->first();
$providers = NPIData::all();
return view ('profiles.provider', compact('provider', 'providers'));
}
I have brought in the NPIData Model to the controller.
I have the following in my provide.blade.php file:
#extends('layouts.profiles')
#section('content')
<div>
{{ $provider->NPI }}
{{$provider->{'Provider First Name'} }}
</div>
#endsection
Oddly, the NPI will pull in, but the 'Provider First Name' does not. I have tried many other columns with spaces and none of them work. I even copied and pasted from other parts of my app where the syntax to pull these in works and it does not work here.

Instead of:
{{$provider->{'Provider First Name'} }}
Try this:
#php
$providerFirstName = $provider->{'Provider First Name'};
#endphp
{{ $providerFirstName }}
UPDATE
If not you can always go with array access:
{{ $provider['Provider First Name'] }}

Related

Laravel - Trying to get property text' of non-object

This is something I've done a million times before so I'm really confused as to why I'm getting this error.
In my Controller, I fetch some results:
$modOfSubchanIDs = Auth::user()->modOf->pluck('id')->toArray();
$modmails = ModMail::whereIn('subchan_id', $modOfSubchanIDs)
->with('subchan')
->with('creator')
->with('appeal')
->orderBy('created_at', 'desc')
->paginate(10);
then in Blade, I try to output the results of the following relationship:
#foreach ($modmails as $modmail)
<div>
{{$modmail->appeal->text}}
</div>
#endforeach
Modmail Model relationship:
public function appeal()
{
return $this->belongsTo(SubchanBanAppeal::class, 'appeal_id');
}
But I get the following error:
ErrorException Trying to get property 'text' of non-object
Strange. So I try just
<div>
{{$modmail->appeal}}
</div>
which returns the following in Blade:
{"id":1,"subchan_id":8,"subchan_ban_id":1,"text":"test","denied":0,"created_by":5003,"created_at":"2021-03-26T00:07:58.000000Z","updated_at":"2021-03-26T00:07:58.000000Z"}
So now I'm really confused. Why is it returning that properly but not when I try a specific column? I also tried {{$modmail->appeal['text']}} but this returns
Trying to access array offset on value of type null
It feels like I'm missing something really basic here...
Did you do the {{ $modmail->appeal }} debug inside the loop? It is likely that most of the $modmails have an appeal, but one or two do not. If it's not the first one that is missing, your test would look right.
The optional() helper method can help in this scenario.
#foreach ($modmails as $modmail)
<div>
{{ optional($modmail->appeal)->text }}
</div>
#endforeach
https://laravel.com/docs/master/helpers#method-optional

Laravel blade creating url

I have a simple problem, basically I am getting name of the website from database and create a link according to it's name. it looks like:
#foreach ($websites as $website)
<a class="websites" href=" {{ asset ($website->name )}}"> {{ asset ($website->name )}}
</a>
#endforeach
Which gives for example: http://localhost/name
Howver links needs to be like this:
http://localhost/website/name how can I add /website into my URL using blade template in laravel?
Try this:
{{ url('website/' . $website->name) }}
This have some improvement on #Laran answer regarding best practices.
You would better use url parameters instead of concatenating the $name parameter
{{ url('website', [$name]) }}
And using named routes will be better to decouple the routing from the views.
// routes/web.php
Route::get('website')->name('website');
and write inside your {{ route('website', [$name]) }}

About Laravel - How to use function string in view

i try to to show data from database article...the content is contain html tags and long text..
so i want make a read more and convert tag html to html view..
this is my code is run :
{{ HTML::decode($show->content) }}
{{ str_limit($show->content, $limit=100, $end=' ...') }}
i try this :
{{ HTML::decode(str_limit($show->content,$limit=100, $end=' ...')) }}
{{ str_limit(HTML::decode($show->content),$limit=100, $end=' ...') }}
but not show ( blank )
annyone can help me to fix it??
thank u b4
Is {{ $show->content }} returning any data to format?
Is your template is .blade?
Maybe it placed somewhere in HTML, where it is not visible?
Try to use this construction with some predefined string to find out if it works. Here is working example:
{{ str_limit('Some big text', $limit = 5, $end='...') }}

Get image name from database in laravel

I have database, with columns image and alttag. I want to use them in laravel blade view. I try something like this:
{{ HTML::image('images/{{ $item->image }}', $alt="{{ $item->alttag }}") }}
But syntax isn't correct. If i just echo image and alttag like this:
<h1>{{ $item->alttag }}</h1>
then they are correct. I wonder what is wrong in my code.
You used blade syntax in a PHP string. Watch the compiled blade templates to see where you did go wrong.
In short. Try this:
{{ HTML::image('images/'. $item->image, $alt = $item->alttag) }}
or equally short:
{{ HTML::image("images/{$item->image}", $alt = $item->alttag) }}

model relationship and routes get model id

i have the following route:
Route::get('notes/main', function(){
$destinations = Destination::where('show', '=','1')->get();
$notes = Destination::find($destination->id)->notes()->get();
return View::make('notes.main')
->with('destinations', $destinations);
});
//the relationship models:
<?php
class Destination extends Eloquent {
public function notes()
{
return $this->has_many('Note');
}
}
<?php
class Note extends Eloquent
{
public function destination()
{
return $this->belongs_to('Destination');
}
}
//View:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
{ $notes->destination->text }} // this isn't echoed
#endforeach
what's the correct way to filter this and define $destination->id
thanks
How would i Filter the notes in an if statement inside the loop ?
#if (isset($note->title) != 'shorttext')
#else
<p> {{ $note->text }} </p>
#endif
#endforeach
You use $destination->id in your Route but it seems to be not defined yet. The question is, what do you want to achieve with your code?
With:
$destinations = Destination::where('show', '=','1')->get();
$notes = Destination::find($destination->id)->notes()->get();
you are getting only the Notes of one specific destination (so far, $destination is not defined. You could use Destination::all()->first() to get the first, or Destination::find(id), with ID being replaced with the primary key value of the destination you need).
But I guess you don't want it that way. From your Output it seems like you want to have an output with each destination and below each destination the corresponding Notes.
Controller:
//gets all the destinations
$destinations = Destination::where('show', '=','1')->get();
return View::make('notes.main')
->with('destinations', $destinations);
View:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
#foreach($destination->notes as $note)
{{ $note->text }} <br>
#endforeach
<hr>
#endforeach
Didn't test this, but this way your View would show you all your Destinations and for each Destination all the Notes.
In your route, your not sending $notes variable to the view.
How I would do it:
$destinations = Destination::where('show', '=','1')->get();
$destinations->notes = Destination::find($destination->id)->notes()->get();
Then in view:
#foreach( $destinations as $destination)
{{ $destination->name}}<br>
{{ $destination->notes->text }}
#endforeach
So first you asked a question, I gave you a correct answer.
You marked my answer as accepted, but later on you edit your initial question to add another question (for which you should create a new thread instead).
Then you create your own answer which is only answering part of your initial question and mark that as the good solution?
I could tell you why your isset and empty did not work, but why should I if you don't value my help at all?
OK
for the last part this worked out at the end
function isset and empty didn't work , strange:
#if ($note->title == 'shorttext')

Resources