I have a page with data/information on it with a download button that converts my page to pdf. Now, the download button is my problem with how I can do it.
My current page URL.
Route::get('/inventory/{id}', function ($id) {
$inventory = Inventory::find($id);
return view('layouts._inventory-template', ['inventory' => $inventory])
});
My button in the view
<span></span>PDF
Function in ClientController
public function viewPdf($id)
{
$inventory = Inventory::find($id);
$pdf = PDF::loadView('layouts._inventory-template');
return $pdf->download('inventory.pdf');
}
Question: How I can implement the logic of PDF download button in the view page?
In your router:
Route::get('/viewPdf/{id}', 'ClientController#viewPdf');
You can simple call your route in href:
<span></span>PDF
I would suggest:
Route::get('/inventory/{id}', 'ClientController#downloadPdf')->name('download-pdf');
ClientController
public function downloadPdf()
{
$inventory = Inventory::find($id);
$pdf = PDF::loadView('layouts._inventory-template', $inventory);
return $pdf->download('inventory.pdf');
}
Button
Download<i class="la la-download"></i>
Note: change Auth::user()->id in the button if ever you want something else
Simply add download property in a tag
<a href="{{ action('ClientController#viewPdf', ['id' => $request()->route('id')]) }}" class="button button-secondary" download><span></span>PDF</a>
Related
I have an edit page, I have used resource controller for it. Everytime the page redirects from edit (../members/16/edit) it continues with the edit page url (../members/16/members). [where it should only take ../members]
How can I remove/change url for edit.
Route
Route::resource('members', MemberController::class);
edit function
public function edit(Member $members,$id)
{
$members = Member::find($id);
return view('edit', compact('members'));
}
public function update(Request $request,$id)
{
$members = Member::find($id);
$members->name = $request->name;
$members->save();
return view('committee')->with('success', 'Member Updated successfully');
}
<a class="btn btn-sm btn-icon btn-success" title="Edit NewsRoom Details"
href="'.route("members.edit",[$members->id]).'"><i class="fas fa-edit"></i></a>
You should identify what you are passing example:
...
Your update method is not redirecting anywhere, it is simply loading a different view, the correct way to do it is:
public function update(Request $request,$id)
{
$members = Member::find($id);
$members->name = $request->name;
$members->save();
//return view('committee')->with('success', 'Member Updated successfully');
return redirect()->route('members.committee')->with('success', 'Member Updated successfully');
}
I want to make a profile photo on the admin page, this photo is in the layouts.template file, how can I get the $profil to be sent to the layouts.template page?
#if($profil->upload!=null)
<img src="{{asset('backend/assets/img/{{$profil->upload}}.jpg')}}" alt="..." class="avatar-img rounded-circle">
#else
<img src="{{asset('backend/assets/img/mlane.jpg')}}" alt="..." class="avatar-img rounded-circle">
#endif
and i created $profil in UserController
$auth = Auth::user()->id;
$profil = Profil_user::where('user_id',$auth)->first();
how do I get my $profil to be used in layouts.template
You can share variable along all views inside below file
app/Providers/AppServiceProvider.php
public function boot()
{
//Define your user auth call here
if(confition){
$profile_pic = 'admin.png';
} else {
$profile_pic = 'default.png';
}
\View::composer('*', function($view){
$view->with('profile_pic', $profile_pic);
});
}
I'm trying to receive data on a sidebar that is included in the blade template but i'm not getting any data delivered. I've tried adding #include('admin.sidebar',['message_counter' => $message_counter]) and in the sidebar view show as {{$message_counter}}. I'm getting a Undefined variable: message_counter.
My router:
Route::get('/admin/sidebar', [
'uses' => 'MessagesController#counter',
'as' => 'admin.sidebar'
]);
My controller
use App\Message;
public function counter()
{
$message_counter = Message::where('status', 0)->get();
return view('admin.sidebar')->with('message_counter', $message_counter);
}
My View
<span class="menu-collapsed">Messages <span class="badge badge-pill badge-primary ml-2"> {{$message_counter}} </span></span>
What i ultimately intend to do is to show the amount of unread messages in the sidebar of the administrator backend, which is #includein every page.
It may be because i'm accessing two different controllers everytime I enter any page on the admin backend.
I've looked into Including Sub-Views but i'm probably missing something silly or not understanding some key concept, help is appreciated!
Thank you!
Note: I think this is inconvenient and unrecommendable. This is just to answer the question, you can scroll down to see other answers or approach.
Controller
public function counter()
{
$message_counter = Message::where('status', 0)->get();
return view('admin.sidebar');
}
View
#php
$message_counter = App\Message::where('status', 0)->get();
#endphp
Messages <span class="badge badge-pill badge-primary ml-2"> {{$message_counter}} </span></span>
You can try like this way
In Route:
Route::get('/admin/sidebar', 'MessagesController#counter');
In Controller
use App\Message;
public function counter()
{
$message_counter = Message::where('status', 0)->get();
return view('admin.sidebar', compact('message_counter));
}
And your view is ok.. Try this and if it is not working please let me know....
With a View Composer: add this to App\Providers\AppServiceProvider#boot()
View::composer('admin.sidebar', function ($view) {
$message_counter = Message::where('status', 0)->get();
$view->with([''message_counter' => $message_counter]);
});
I'm building a backend for my website in Laravel. I need only few text boxes to populate a template. So far I've managed to do that, but every time I pass a value to a template I see also all previous values, not only the last one.
So for example if I'm editing slider title, I see also all previous slider titles - I want to see only last one.
My controler:
public function store()
{
$input = Input::all();
homepage::create([
'slider_title' => $input['slider_title'],
]);
return Redirect::to('/');
}
Part of view responsible for displaying slider title:
#foreach ($homepage as $home)
{{$home->slider_title}}
#endforeach
View for user input in admin:
{{ Form::open(['url' => '/admin']) }}
<div>
{{ Form::label('slider_title', 'Slider title:') }}
{{ Form::text('slider_title') }}
</div>
<div>
{{ Form::submit('Change a slider title') }}
</div>
{{ Form::close() }}
Route for admin:
Route::post('/admin', 'AdminController#store');
Route for main page:
Route::get('/', 'PageController#home');
PageController home method:
public function home()
{
$homepage = Homepage::all();
return View::make('home')->with('homepage',$homepage);
}
I tried changing PageControl with:
$homepage = Homepage::orderBy('id', 'desc')->first();
but I'm getting this error:
ErrorException: Trying to get property of non-object on my view page
public function home()
{
$homepage = Homepage::all();
return View::make('home')->with('homepage',$homepage);
}
You are seeing all slides because you are selecting all of them:
$homepage = Homepage::all();
This should give you the last one:
$homepage = Homepage::orderBy('id', 'desc')->first();
or
$homepage = Homepage::orderBy('created_at', 'desc')->first();
Remember that now you are getting only one result and you cannot use foreach anymore:
foreach ($homepage as $home)
You can refer to it directly:
$homepage->slider_title
If you still want to return an array to use foreach, this is an option:
$homepage = array( Homepage::orderBy('created_at', 'desc')->first() );
I've got a problem with using URL::route. There is a public function in my controller called AuthController called delete_character, here's how it looks:
public function delete_character()
{
$player->delete();
return View::make('index')->with('danger', 'You have successfully deleted your character!');
}
Also, I've created a named route:
Route::post('delete_character', array(
'as' => 'delete_character',
'uses' => 'AuthController#delete_character'
));
All I want to do is to execute the $player->delete. I don't want it to be a site, just when I click a button it's gonna delete the player.
I've also done the button:
<td><a class="btn btn-mini btn-danger" href="{{ URL::route('delete_character') }}"><i class="icon-trash icon-white"></i> Delete</a></td>
But I constantly get MethodNotAllowedHttpException. Any hints?
In my example, I am using GET request method (POST is used when form is being submited, for instance) to capture this action.
I pass ID of client I wish to delete in the reqeust URL, which results into URL in this form: http://localhost:8888/k/public/admin/client/delete/1 (You should post it from form, according to your example/request).
Not posting whole solution for you to force you to learn! My answer is not 100% identical to your situation, but will help, for sure.
// routes.php
Route::group(['prefix' => 'admin'], function(){
Route::get('client/delete/{id}', 'Admin\\ClientController#delete');
});
// ClientController.php
<?php
namespace Admin;
use Client;
class ClientController extends BaseController
{
...
public function delete($clientId)
{
$client = Client::findOrFail($clientId);
// $client->delete();
// return Redirect::back();
}
...
}
// view file, here you generate link to 'delete' action
delete