I've encountered a strange error when using laravel. So essentially when I call this
{{ $product->images()->first()->fileName }}
I get the following error
Trying to get property 'fileName' of non-object
However, when I call it in dd it shows the variable fine...
#dd($product->images()->first()->fileName);
So I'm not really sure whats wrong, 'images' is related by a hasMany call in product. Would love some advice on whats going on here!
This is a simple, but not necessarily ideal solution:
#php $image = $product->images()->first(); #endphp
#if($image)
<img src="{{ $image->fileName }}"/>
#endif
Since, you are iterating over the products, one of the products must not have an image.
Without changing your code much, just add an # before the statement
{{ #$product->images()->first()->fileName }}
This will automatically handle the error and return null. So, your program won't crash.
See PHP Error Control Operators
Related
I am trying to use Laravel 8 Livewire Modal Popup for data entry with going on another page. But I get undefine the variable _instance and not able to understand it.
#entangle($attributes->wire('model'))
This line creates this error when I remove this from views/vendor/jetstream/components/modal.blade.php. the error will go.
Line no 34.
<div id="<?php echo e($id); ?>" x-data="{ show: <?php if ((object) ($attributes->wire('model')) instanceof \Livewire\WireDirective) : ?>window.Livewire.find('<?php echo e($_instance->id); ?>').entangle('<?php echo e($attributes->wire('model')->value(
x-show="show"
x-on:close.stop="show = false"
x-on:keydown.escape.window="show = false"
class="fixed top-0 inset-x-0 px-4 pt-6 sm:px-0 sm:flex sm:items-top sm:justify-center"
style="display: none;">
This was causing me much angst too but I think I found the solution: as #georgy-malanichev says, you can only call Livewire methods from inside a Livewire component (and not from inside a Blade component or any other custom components).
Given you are trying to use the component inside resources/views/dashboard.blade.php, the solution is to:
create a livewire component using artisan make:livewire MyDashboard
Cut everything between <x-app-layout> and </x-app-layout> in dashboard.blade.php and paste it into views/livewire/my-dashboard.blade.php
Add #livewire('my-dashboard') inside the x-app-layout tags and Bob's your uncle (it should start working)
To help you understand what's going on, if you look at the source code for the modal component, you'll see a line like: show: #entangle($attributes->wire('model')),. I'm not sure how to describe exactly what this does, but, essentially, #entangle() is expecting an instance of the "model" Livewire object and it's not finding one.
It's not finding it because it's getting called from a non-livewire component. Once you put it inside a Livewire component, it starts working.
I hope the additional details makes things clearer.
I was getting the same error but in my case it was the fact that I had x-data="{ open: #entangle('showDropdown') }" outside of the LiveWire component. Once I moved it inside the component template, where it should be, the issue went away.
I have problem with:
Blade::compileString()
I have article view:
{!! Blade::compileString($article->content) !!}
My $article->content contain:
<p>Test</p>
<p>{{ module('contact') }}</p>
And I don't know why blade compiling it to:
source screenshot
Why module() function is not executing?
Function calls are never compiled.
Let's say your view shows the current time and you'd call {{ date('H:i:s') }} in order to do that. When compiling, that will change to a certain timestamp, say 12:34:56. Now everytime an user refreshes the page, even hours later, the time won't change anymore until the view is next compiled, which would have the undesired effect of basically freezing your entire page.
Hope this makes sense.
I have a Laravel 5.1 project that I'm trying to improve and now migrating some features to Vue.js to make it more interactive and stuff.
In my view I have
<img src="{{ file_storage_path($product->path) }}" alt="{{$product->name}}"/>
This is default implementation using Laravel's blade.
Now, when I delegate this to the Vue.js I would have something like this
<img src="{{ file_storage_path(product.path) }}" alt="#{{product.name}}"/>
But this of course fails. So I need both - to invoke a php-function and at the same time to render Vue data.
I tried this
src="{{ file_storage_path( #{{product.path}} ) }}"
But of course this fails too. any suggestions?
Since I couldn't find a way to inline both blade syntax and Vue.js variable rendering I see the solution is to first invoke the php function and then append the Vue.js rendering to it.
So, the workaround would be
<img :src="'{{ file_storage_path('/') }}' + image.path" alt="#{{product.name}}" />
This solution would work only for specific case, it doesn't get answer to my original question, when a php function actually needs to accept a Vue variable. But in this case, I can simply append values.
I am new to Laravel so my problem is that I am trying to add multiple script files to my blade.php page using this code:
{{
HTML::script('js/bootstrap.min.js');
HTML::script('js/Chart.js');
}}
without any results , am I doing anything wrong or misunderstood some concept, please specify the best way to achieve my goal
only first include is working, the second one is not including
Thanks
You can't have line breaks inside Blade tags (at least not in Laravel 3). What you need to do is to add {{ ... }} for every HTML:: you have.
{{ HTML::script('js/bootstrap.min.js'); }}
{{ HTML::script('js/Chart.js'); }}
I have been stumped for a couple days and I am seeking some direction.
I am attempting to call an image path stored in database to twig file in order to display said image. The twig example below, I am expecting the same image to be displayed twice. When inspecting the rendered html, the variable passes the path from the database, but the first image is not displayed.
From controller:
'logo' => $vendor->getLogovendors()
From database column logoVendors:
<img src={{asset('bundles/loginlogin/img/fs_logo_large.png')}} />
From twig:
<div class="container">
{{logo | raw}}
<img src={{asset('bundles/loginlogin/img/fs_logo_large.png')}} />
</div>
I am new to Symfony and its asset management. Any help or prodding in the right direction would be appreciated.
You should normally store only the path to the image in your database!
If logo was the variable you pass to the template holding the image path bundles/loginlogin/img/fs_logo_large.png you could simply include it using twig's asset function like this:
<img src="{{ asset(logo) }}"/>
what you're trying to do ( evaluating a twig function inside a string ) can be solved aswell...but i don't recommend it.
If you want to store the complete code including {{ asset() }} in your database you need to make twig evaluate the code inside the string.
This means twig shall execute the code inside the string instead of just printing it.
This can be achieved using the evaluate filter from this answer.
The final result would then be:
{{ logo |evaluate |raw }}