I don't want that users can access images from simply using a URL like http://laravel.dev/123.jpg. Therefore I store all images in the storage directory, instead of storing them in the public folder.
With a controller like this
public function show($id) {
$img = storage_path('my_images/123.jpg');
return response()->download($img, null, [], null);
}
the image is correctly displayed by Laravel 5, although it is stored outside from the public folder.
Great up to this point ... but I want to use Blade and surround the image with some more details. Therefore my controller looks like this
public function show($id) {
$picture = Picture::find($id);
$img = storage_path('my_images/123.jpg');
return view('pictures.show', array('picture' => $picture, 'img' => $img));
}
and the view like this
<!DOCTYPE html>
<html>
<head>
<title>Picture {{ $picture->id }}</title>
</head>
<body>
<h1>Picture {{ $picture->id }}</h1>
<ul>
<li>Name: {{ $picture->name }}</li>
<li>Description: {{ $picture->description }}</li>
</ul>
<img src="{{ $image }}">
</body>
</html>
which obviously does not work, because the location of the image is not publicly accessible (which ist the intention).
What do I have to use instead of <img src="{{ $image }}"> to make this work?
Make the src attribute to call the URL of your first controller, this will work for you.
In other words, let's say, you have a route called image.show with that code.
public function show($id) {
$img = storage_path('my_images/123.jpg');
return response()->download($img, null, [], null);
}
So in your blade:
<img src={{route("image.show",$img_object)}}">
Related
i have a problem passing variable during the rendering of only a section
All works good but array of data passed to the section('sidebar') view create an error ($data doesn't exist)
My blade files are
Default.blade.php
..other html code before..
<body>
#include('includes.header')
<div class="container-fluid">
<div class="row">
#yield('sidebar')
<!-- main content -->
#include('includes.main')
</div>
<footer class="row">
#include('includes.footer')
</footer>
</div>
</body>
..other code after..
home.blade.php
#extends('layouts.default')
#section('sidebar')
#include('includes.sidebar')
#stop
sidebar.blade.php
..other html code before..
<h2>The current UNIX timestamp is {{ time() }}.</h2>
<ul>
#isset($data)
#foreach ($data as $item)
<li class="nav-item"> {{$item->polizza}}</li>
#endforeach
#endisset
</ul>
..other html code after..
Controller Method search
public function search(Request $request){
if ($request->ajax()) {
$data = Customers::select('id','contr_nom','email','targa','cliente')
->where('polizza',request('polizza'))
->get();
return view('pages.home',$data)->renderSections()['sidebar'];
}
//return json_encode($data);
}
I know that array $data is good because i try return just JSON and i know that just sidebar refresh because timestamp change.
But $data is not passed to sidebar section refreshed!!
Why?
Thks a lot
You have the right idea, you just need to send the variable in a form that will be recognized. I'll break it out to an extreme, to help understand the parts, but you can easily recombine for shorter code.
$view = view('pages.home', compact('data')); // Compact with the text name ('data') sends the variable through
$sections = $view->renderSections(); // returns an associative array of 'content', 'pageHeading' etc
return $sections['sidebar']; // this will only return whats in the sidebar section of the view
Using compact() should get your where you need, and is the key.
I have stored logos in the database and want to display them in the footer which is included #include('layouts.footer'). I am fetching images from the database using this code
public function show(){
$logo = DB::table('logos')->get();
return view('layouts.footer',['logo'=>$logo]);
}
I want to display these images in the footer using
<div class="footer">
#foreach($logo as $l)
{{ $l->company_name}}
{{ $l->company_logo}}
#endforeach
<p class="footer-text">© Copyright <?php echo date("Y"); ?> Hotel Store Partners</p>
</div>
but returns an error "undefined variable logo".
What is the correct way to display the images in the footer?
Try Using View Composer
In you AppserviceProvider boot method you can gather these name and pass to all Views at once.
Reference
Two ways you can achieve this:
1. Put this code in footer.
#php
$logo = DB::table('logos')->get();
#endphp
<div class="footer">
#if(count($logo))
#foreach($logo as $l)
{{ $l->company_name}}
{{ $l->company_logo}}
#endforeach
#endif
<p class="footer-text">© Copyright <?php echo date("Y"); ?> Hotel Store Partners</p>
</div>
But this method is a bad practice
Create FooterComposer
public function compose(View $view)
{
$logo = DB::table('logos')->get();
$view->with(['logo'=>$logo]);
}
Now $logo will be accessible in footer.
I have used View Composer.
In AppServiceProvider boot method, I added this code:
public function boot()
{
View::composer('layouts.app', function($view){
$view->with('logo', Logo::all());
});
}
In the Controller, I used this code:
return view('layouts.footer');
In the footer, I used this code:
<footer>
#foreach($logo as $l)
<p>{{ $l->company_name }}</p>
<img src="https://partners.hotelstore.co.ke/public{{ $l->company_logo }}" style="width:100px; height:75px;">
#endforeach
<p class="footer-text">© Copyright <?php echo date("Y"); ?> Hotel Store Partners</p>
</footer>
This is what finally displayed the images on the footer.
I'm trying to get meta data for each page
Each page has a unique slug
In appServiceProvider I have:
$view->with('seopage', Page::all());
In blade view (section head) I have:
<title>
#foreach($seopage as $sm)
{{ $sm->main_title }}
#endforeach
</title>
<meta name="description" content="
#if(Request::is('/*'))
#foreach($seopage as $sp)
{{ $sp->site_description }}
#endforeach
#endif
">
But it is getting all titles and descriptions for me at the moment. How can I get data for specified page?
Page model have unique SLUG variable. Thanks
In appServiceProvider you can share page which belongs to specified page:
$page = Page::where('slug', Request::path())->first();
View::share('seopage', $page);
And in view you can retreive shared variable:
<meta name="description" content="
#if(Request::is('/*') && isset($seopage))
{{ $seopage->site_description }}
#endif
">
You must be have unique uri column in seopage table
The you must be use
use Illuminate\Support\Facades\Route;
.....
$uri = \Route::current()->uri;
$seopage = Page::where('uri', $uri)->first();
if ($seopage) {
$seo = sprintf(
'<title>%s</title>' . PHP_EOL
. '<meta name="description" content="%s>',
$seopage->main_title ,
$seopage->site_description
);
} else {
$seo = '';
}
$view->with('seo', $seo);
In Blade use
{{ $seo }}
I have a login form. I want when user login success, my controller will select data of this user and share to all view.
I read laravel docs. [This][1] advises me to use the "View::share('key', 'value');"
in boot method of AppServiceProvider, but i dont know how to call it in my controller
my controller:
if( Auth::attempt(['mssv' => $username,'password' =>$password])) {
$user=SinhVien::where('mssv',$username)->first();
//i want to share $user to all view in here
$success = new MessageBag(['successlogin' => 'Login Success]);
return redirect()->back()->withErrors($success);
}
else {
$errors = new MessageBag(['errorlogin' => 'Login Fail']);
return redirect()->back()->withErrors($errors);
}
and in all view blade, I use:
{{$user->name}}
Please help!
[1]: https://laravel.com/docs/5.7/views#sharing-data-with-all-views
In your blades you would then have access to the key from the View::share('key', 'value'); you put in the AppServiceProvider.
So if the key is user, in your blades you just use the $user variable.
<!-- Stored in resources/views/layouts/app.blade.php -->
<html>
<head>
<title>App Name - #yield('title')</title>
</head>
<body>
<header>
<div>
#if(auth()->user())
{{ auth()->user()->name }}
#else
<button>login</button>
#endif
<div>
</header>
<div class="container">
#yield('content')
</div>
</body>
EDIT :
{{ auth()->user->name }} corrected -> {{ auth()->user()->name }}
chatcontroller.php function returns variable to view:
public function getChat()
{
$message = chat_messages::all();
return View::make('home',compact($message));
}
this is my route:
Route::get('/getChat', array('as' => 'getChat','uses' => 'ChatController#getChat'));
this is my home.blade.php:
#extends('layouts.main')
#section('content')
<div class="container">
<h2>Welcome to Home Page!</h2>
<p> <a href="{{ URL::to('/logout') }}" > Logout </a></p>
<h1>Hello <span id="username">{{ Auth::user()->username }} </span>!</h1>
<div id="chat_window">
</div>
<input type="text" name="chat" class="typer" id="text" autofocus="true" onblur="notTyping()">
</div>
<ul>
#foreach($message as $msg)
<li>
{{ $msg['sender_username']," says: ",$msg['message'],"<br/>" }}
</li>
#endforeach
</ul>
<script src="{{ asset('js/jquery-2.1.3.min.js') }}"></script>
<script src="{{ asset('js/chat.js') }}"></script>
#stop
I am trying to send result returned by select query to view from controller.
when I do this from homecontroller.php then it works fine.
if I try to pass from controller which I have defined it gives error message as:Undefined variable.
I have used the extends \BaseController do i need to do anything else to access my controller variable from view.
please suggest some tutorial if possible for same.
Verify the route to be sure it uses the new controller:
Route::get('user/profile', array('uses' => 'MyDefinedController#showProfile'));
First of all check your routes, as Matei Mihai says.
There are two different ways to pass data into your view;
$items = Item::all();
// Option 1
return View::make('item.index', compact('items'));
// Option 2
return View::make('item.index')->with('items', $items); // same code as below
// Option 3
View::share('item.index', compact('items'));
return View::make('item.index);
You can also do this:
$this->data['items'] = Item::all();
return View::make('item.index', $this->data);