Route [/download] not defined. (View: - laravel

hi I am downloading data directly from URL, but when I try to make a button that when I click on it then it becomes downloadable then it said:
Route [/download] not defined.
D:\xampp\htdocs\laravel\webpro5\resources\views\showrecord.blade.php
code of button:
<div align="center">
Export to Excel
</div>
route:
Route::get('/download', function(){
return Excel::download(new ExcelsExport, 'importpdfs.xlsx');
});

The route() function expects a single parameter that matches a named route. Currently, you don't have a named one. Either use the url() function:
...
or name your route:
Route::get("/download", ...)->name("download");

<div align="center">
Export to Excel
</div>
or
<div align="center">
Export to Excel
</div>
route helper is used for named routes.
Additionally you can do
Route::get('/download', 'Controller#Method')->name('download');
then you can use the route helper like this:
{{ route('download') }}

Related

Laravel returns 404 error when passing a parameter

Hello when I send a data press a link to show another view laravel returns a 404 error, what am I failing?, I have created other crud and I have had no problems so far
Expansion -> index.blade.php:
<a href="{{url('/cards/'.$expansion->exp_id.'/vcards')}}"class="btn btn-primary form-control" >Go!</a>
URL:
http://localhost/CardSystem/cartas/1/vcards
Route:
Route::resource('cards', 'cardControllerr');
cardController:
public function vcards($id){
$data['cards']= DB::table('cards')
->join('expansion','expansion.exp_id','=','cards.exp_id')
->select('cards.card_id','cards.card_nom','expansion.exp_nom')
->where('cards.exp_id','=',$id)
->orderBy('cards.card_num')
->paginate(5);
return view('cards.vcards',$data);
vcards.blade.php:
#extends('layouts.app')
#section('content')
<div class="container">
#foreach($cards as $card)
<div class="form-group">
<h5 class="card-title">{{$card->card_nom}}</h5>
</div>
#endforeach
</div>
#endsection
I think you misunderstood routing, views etc.
For your controller to be hit, you will need to make the following route.
Route::get('cartas/{card}/vcards', 'cardControllerr#vcards')->name('cartas.vcards');
If you wanna link to this route use route().
route('cartas.vcards', ['card' => $card]);

Form not showing due to routing problem in action parameter

I am working on an Excel import module as part of a CRM for my company. I want to import an excel sheet. I use the Maatwebsite Excel package, version 3.1. I want to show the form and then upload a sheet. However I can't even get to that point. I have already determined the issue is within the form route, just not sure what it is exactly that I am missing.
Routes that I use to display the page (index works fine)
Form used to get the Excel sheet imported
Navigation bar link in the menu
DataController (from which I am trying to call the import method)
If you know what may be wrong please do tell, this is really frustrating!
Route code:
Route::get('importeren', 'Datacontroller#index');
Route::post('import', 'Datacontroller#import');
<div class="container-fluid">
<form action="import" method="POST" enctype="multipart/form-data">
#csrf
<input type="file" name="import_file">
<br>
<input type="submit" value="Import">
</form>
</div>
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Imports\DataImport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class DataController extends Controller
{
public function index(){
return view('importeren');
}
public function import(Request $request){
Excel::import(new DataImport(), $request->file('import_file'));
return redirect()->route('/home');
}
}
Don't use route() method because you're not defining any routes name, use something like:
form action="/import" method="POST" enctype="multipart/formdata">
you should use route() only with route name, use this instead :
return redirect('/home');
You can give a name to your route:
Route::post('import', 'Datacontroller#import')->name('import');
and leave the form action as it is with the route() helper:
<div class="container-fluid">
<form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
#csrf
<input type="file" name="import_file">
<br>
<input type="submit" value="Import">
</form>
</div>
You could also use the url() helper and leave the route with no name, but I highly recommend the option of giving your route a name.
<form action="{{ url('import') }}" method="POST" enctype="multipart/form-data">
Note that in the controller return you are also using a redirect to a named route, so I suggest that you give that route a name and use that name in the redirect. For example:
Route::get('home', 'SomeController#someMethod')->name('home');
and
return redirect()->route('home');

How to pass variable to view without using url in Laravel?

guys. I'm a newbie of Laravel.
I'm just wondering that if I need to pass some sensitive info, which is been wrote on a form, to controller, then to another view.
How could I pass the info to the view without using URL?
I have made some small test, but the result is all the same (url would include the data).
Here is the code.
Html(a):
<form action="/data" method="GET" class="form-horizontal">
<div class="form-group">
<input type="text" class="form-control" name="Test">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">test</button>
</div>
web.php:
Route::get('/data', 'DataController#show');
DataController.php:
public function show(Request $request) {
return view('testShow');
}
Html(b):
<div class="ShowDataHere">
#if(!empty(request()->all()))
{{ request()->Test }}
#endif
Simply change your form to use POST instead of GET.
On the form...
<form action="/data" method="POST" class="form-horizontal">
In your route ...
Route::post('/data', 'DataController#show');
This will send all of the form fields to the server in a way that is less visable (not in the URL) but to make the form secure, you'll want to ensure it's served using HTTPS as well.
In Laravel when you use Route::post('/data', 'DataController#show') the Laravel expect a GET, because of the show function.
So you can use Route::post('/data', 'DataController#someFunction') and in your laravel controller get with:
public function someFunction(Request $request)
{
$array = $request->all(); //your fields
}
In this function you can get the attributes with $array = $request->all();

Laravel NotFoundHttpException although route exits

I added a new route as:
Route::post('friendSend/{uid}','FriendController#sendFriendRequest')->name('friends.add');
and called it as a hyperlink to submit form:
<a href="{{route('friends.add',$user->uid)}}"
onclick="event.preventDefault();
document.getElementById('addfriend-form).submit();">
<i class="glyphicon glyphicon-facetime-video" style="color:#F44336;"></i> Add Friend
</a>
<form action="{{route('friends.add',$user->uid)}}" method="post" id="addfriend-form">
{{ csrf_field() }}
</form>
However when I click on the said link, I get redirected to /friendSend with the said error.
the route is visible in:
php artisan route:list
which makes sense since I called it via it's name 'friends.add'. It doesn't even go the controller.
I've already tried the following:
Laravel NotFoundHttpException although route exists
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Friend;
use Auth;
class FriendController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return redirect()->route('home');
}
public function sendFriendRequest($id)
{
echo "hello world";
}
}
Update:
Manually entering the url as /friendSend/2 (or any number for that matter) works.
You are missing a ' in the onclick javascript.
<a href="{{route('friends.add',$user->uid)}}"
onclick="event.preventDefault();
document.getElementById('addfriend-form').submit();">
<i class="glyphicon glyphicon-facetime-video" style="color:#F44336;"></i> Add Friend
</a>
<form action="{{route('friends.add',$user->uid)}}" method="post" id="addfriend-form">
{{ csrf_field() }}
</form>
If this doesn't work, what is the href on the generated page? Do you have multiple of these forms on a single page?
I thing you are doing wrong on set the url in form submit.You are adding route like
href="{{route('friends.add',$user->uid)}}"
Just modified it as
href="{{route('friends.add',['uid' => $user->uid])}}"
You can refer Laravel Named Routes

Is it possible to delete record without using forms in laravel 5.4

I want to delete a record but I haven't been successful, apparently my code is wrong. Solutions i came across say i have to use a post in my form method and add the method_field helper. This would mean my view having a form in it, i want to avoid this if possible. Is it then possible to do my delete another way. Below is my code
snippet of my view
<div class="backbtn">
<a class="btn btn-savvy-delete" href="/tasks/{{$task->id}}" data-toggle="tooltip" title="Delete"><i class="fa fa-trash-o" aria-hidden="true"> Delete</i></a>
</div>
<div class="panel-body">
<p><strong>Owner:</strong> {{ ucfirst($task->employee->firstname) }} {{" "}} {{ ucfirst($task->employee->lastname) }}</p>
<p><strong>Task:</strong> {{ $task->title }}</p>
<p><strong>Description:</strong> {{ $task->description }}</p>
</div>
TaskController
public function destroy($id)
{
Task::destroy($id);
Session::flash('status', "Task was successfully deleted.");
return redirect('/tasks');
}
web.php
Route::delete('/tasks/{id}', 'TaskController#delete');
Im not sure what error you are getting, but i can point out a few things. For one use Route::get instead of ::delete, you are calling it via a link not a form method.
Secondly to delete follow what the laravel doc says here eg.
$task = App\Task::find(1);
$task->delete();

Resources