Laravel 5.6 not working translate in content - php

I want to use translate in another file than main file(file where i have some content and this file is extended by main file), but it is not working. Do you know anyone why? The same problem is with theme_url('path').
in main file i have:
#php
App::setLocale('en');
Theme::Set('mobile');
#endphp
head...
<body>
#yield('content')
</body>
and in extended file:
#extends('main file')
#section('content')
<img src="{{ theme_url('img/logo.png') }}">
<span>{{ __('lang.title') }}</span>
#endsection
When i replaced #yield('content') by #include, all will be working(switching theme using from author igaster)

I think the problem is that
#extends('main file')
#section('content')
<img src="{{ theme_url('img/logo.png') }}">
<span>{{ __('lang.title') }}</span>
#endsection
this code executed first and then put in main file
so in this file locale is not 'en'
try this
#extends('main file')
App::setLocale('en');
Theme::Set('mobile');
#section('content')
<img src="{{ theme_url('img/logo.png') }}">
<span>{{ __('lang.title') }}</span>
#endsection

Related

Include blade file from another file not working

I am trying to include this comments.index file:
#extends('posts.show')
#section('title', 'Comments')
#section('comments')
Create Comment
#foreach ($comments as $comment)
#if ($comment->post_id == $post->id)
<div class="border margin mb-1">
<div class="border margin">
<p>{{$comment->text}}</p>
</div>
Edit
</div>
#endif
#endforeach
Back
#endsection
From this posts.show file:
#extends('layouts.myapp')
#section('content')
<head>
<link href="{{ asset('css/posts.css') }}" rel="stylesheet">
</head>
<div class="post_container">
<div id="post_title">
{{$post->title}}
</div>
<div id="post_text">
{{$post->text ?? 'Empty'}}
</div>
<div>
<img src="{{ asset('storage/images/'. $post->image) }}">
<p>
{{$post->image}}
</p>
</div>
</div>
<div>
<h2>Comments</h2>
<div>
#include('comments')
</div>
Comments
</div>
Back
#endsection
Now the issue is that when I include 'comments' it throws 'view comments not found' error and asks if I am sure there is a blade file with such a name. This made me think that I need to include 'comments.index', but when I do this I get 'undefined variable $comments' error.
Doing an include of a partial is dot notation. So in this example...
#include( 'includes.modals.dude' )
This is really located in views -> includes -> modals -> dude.blade.php
The file name must be YourName.blade.php.
I think you are providing a relative path, include directive needs absolute path. So, in your case following code should work.
#include('comments.index')

Yield blade file from another file not working

I am trying to display comment index content
#section('title', 'Comments')
Create Comment
#foreach ($comments as $comment)
#if ($comment->post_id == $post->id)
<div class="border margin mb-1">
<div class="border margin">
<p>{{$comment->text}}</p>
</div>
Edit
</div>
#endif
#endforeach
Back
#endsection
From posts show file:
#extends('layouts.myapp')
#section('content')
<head>
<link href="{{ asset('css/posts.css') }}" rel="stylesheet">
</head>
<div>
<h2>Comments</h2>
<div>
#include('comments.index')
</div>
Comments
</div>
Back
#endsection
I want to be able to see the comments without having to go on comments.index link, which I have to delete.
It's only a blind guess: maybe you should look at your blade directory structure, where the file with the comments-section is located, maybe it should be
#yield('posts.comments')
And by the way the head tag in the content section looks a bit displaced imho.
The problem is that you are not yielding data here. You just include the value here. So use #include in stead of #yield. The following code is below:
In comment file :
Create Comment
#foreach ($comments as $comment)
#if ($comment->post_id == $post->id)
<div class="border margin mb-1">
<div class="border margin">
<p>{{$comment->text}}</p>
</div>
Edit
</div>
#endif
#endforeach
Back
Now in the From posts show file:
#extends('layouts.myapp')
#section('content')
<head>
<link href="{{ asset('css/posts.css') }}" rel="stylesheet">
</head>
<div>
<h2>Comments</h2>
<div>
#include('comments')
</div>
Comments
</div>
Back
#endsection

How do I show the Image that I uploaded in the EDIT page, Laravel

So I have this edit form and I want to show the image that I uploaded from the create form (its working thou) so when I edit a building I just want to see the old picture that I uploaded cause its not showing in the form. The edit works thou but I want to see the picture that already been uploaded. I tried using "$building->picture" but not working, the column name of my database is "picture".
here's the code
#extends('layouts.main')
#section('title', 'Edit Building')
#section('content')
{!! Form::open(array('route' => ['editbuilding',$id], 'class' => 'form' , 'files'=>'true')) !!}
<div class="container">
<div class="form-group">
{!! Form::label('Building Name') !!}
{!! Form::text('buildingname', $building->name, array('required',
'class'=>'form-control',
'placeholder'=>'Building Name')) !!}
</div>
<div class="form-group">
{!! Form::label('Building Name') !!}
{!! Form::file('buildingpics',
array('onchange'=>'previewFile()')) !!}
<img src="{{asset(''.$building->picture) }}" id="previewImg" style="height:300px; width:300px;" alt="">
</div>
<div class="form-group">
{!! Form::submit('Update',
array('class'=>'btn btn-primary')) !!}
Back
</div>
</div>
{!! Form::close() !!}
<script type="text/javascript">
function previewFile() {
var preview = document.querySelector('#previewImg');
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();
reader.addEventListener("load", function () {
preview.src = reader.result;
}, false);
if (file) {
reader.readAsDataURL(file);
}
}
</script>
#endsection
#section('scripts')
#endsection
Are you storing the URL to the image in the picture file? If so, you can just pass that URL to the src of the img tag.
<img src="{{ $building->picture }}" ... />
first choose where your file is saved. i mean the path of file where it is stored.
<img src="{{ url($building->picture) }}" ... />
if your file in public folder.
asset() - Generate a URL to an application asset.
change name of folder_name according to your folder name if any here its inside "assets" folder
#if ($building->picture)
<img src="{{asset('assets/'.$building->picture)}}">
#else
<p>No image found</p>
#endif

Logic in Laravel components

I have a laravel component
<section class="section {{ $classes }}">
<div class="inner">
<h1>{{ $heading }}</h1>
<h2>{{ $subheading }}</h2>
<p>{{ $copy }}</p>
</div>
{{ $slot }}
</section>
I render in blade template
#component('components.section', ['classes' => 'lightgrey'])
#slot('heading')
The best thing ever....
#endslot
#slot('subheading')
#endslot
#slot('copy')
Lots of interesting words go here
#endslot
#endcomponent
Sometime I only have an H1. How can I remove the markup if I do not have a sub heading?
From the Laravel docs: https://laravel.com/docs/5.5/blade
The #isset and #empty directives may be used as convenient shortcuts for their respective PHP functions:
#isset($records)
// $records is defined and is not null...
#endisset
#empty($records)
// $records is "empty"...
#endempty
I'm thinking that this would probably work:
#isset($subheading)
#slot('subheading')
#endslot
#endisset

Different default template loading even though i have set routing correctly

In my views folder i have layouts/main.blade.php and under store/index.blade.php i am using the layout file like so:
#extends('layouts.main') // importing here.
#section('promo')
<section id="promo">
<div id="promo-details">
<h1>Today's Deals</h1>
<p>Checkout this section of<br/>
products at a discounted price.</p>
Shop Now
</div><!-- end promo-details -->
{{ HTML::image('img/promo.png' , 'Promotional Ad') }}
</section><!-- promo -->
#stop
#section('content')
<h2>New Products</h2>
<hr>
<div id="products">
#foreach($products as $product)
<div class="product">
<a href="store/view/{{ $product->id }}">
{{ HTML::image($product->image , $product->title , array('class'=>'feature' ,
'width'=>'240' , 'height'=>'127')) }}
</a>
<h3>
<a href="/store/view/{{ $product->id }}">
{{ $product->title }}
</a>
</h3>
<p>
{{ $product->description }}
</p>
<h5>Availability:
<span class="{{ Availability::displayClass($product->availability) }}">
{{ Availability::display($product->availability) }}
</span>
</h5>
<p>
<a href="#" class="cart-btn">
<span class="price">${{ $product->price }}</span>
{{ HTML::image('img/white-cart.gif' , 'Add to Cart') }}
ADD TO CART
</a>
</p>
</div>
#endforeach
</div> <!-- end products -->
#stop
In my storeController , i am calling the store/index.blade.php file like so:
public function getIndex() {
return View::make('store.index')
->with('products' , Product::take(4)->orderBy('created_at' , 'DESC')->get());
}
In my routing file, i have the following:
Route::get('/', array('uses' => 'StoreController#getIndex'));
But still when i load my / , i get some other template rather than layouts/main.blade.php. Why is this happening ? can anybody explain ?
Why is layouts/main.blade.php not loading when i hit / in the browser ?
Thank you.
Alex-z.
Well i had the following syntax .
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
{{ HTML::style('css/bootstrap.css') }}
{{ HTML::style('css/animate.min.css') }}
{{ HTML::style('css/style.css') }}
{{ HTML::style('css/nprogress.css') }}
{{ HTML::style('css/new-arrivals.css') }}
Removing the below line
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">
solved my problem ... Why though ? also how do i disable cache in laravel during development ?

Categories