Laravel 5.2.45 Method store does not exist - laravel-5

I am trying to copy a file to my server Public Folder.
I am getting the following error :
BadMethodCallException in Macroable.php line 74: Method store does not exist.
This is the html to upload the file :
<form action="/leads/csvFiles" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<input type="file" name="csvfile" />
<input type="submit"/>
</form>
And here is the Route:
Route::post('leads/csvFiles', function(){
request()->file('csvfile')->store('Public');
return back();
});

store() method has been implemented from Laravel 5.3, you need to use something like:
Route::post('leads/csvFiles', function(){
$request->file('csvfile')->move('Public');
return back();
});
Its advised to check first if file is valid:
if ($request->file('csvfile')->isValid()) {
//next code here
}
Then you you can actually save the file with whatever name you want.
$request->file('csvfile')->move('Public', 'myfilename.csv');

Related

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');

Laravel custom login is failing for the first time with "route [login] not defined" error. Working fine in second attempt

I'm setting up my custom authentication system in my Laravel app. I've deleted all the default auth controllers and not using make::auth. And my auth is working properly. My main problem is that when I tried to log in for the first time, it's failing with "Route [login] not defined" error, but in second attempt, it's working properly. And if I repeat the process, it's continuing again and again like the first two attempt. Actually, I've never used login route anywhere.
Here is my form:
<form action="{{ url('/log-in') }}" method="POST">
#csrf
<input type="text" name="phone" placeholder="Telefon" class="form-control input-phone">
<input type="password" name="password" placeholder="Parol" class="form-control">
<button type="submit" class="btn">Kirish</button>
</form>
Here is my route:
Route::post('/log-in', 'AuthController#login');
Here is my controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Auth;
use App\User;
class AuthController extends Controller
{
public function login(Request $request) {
// Get current user.
$user = User::where('phone', $request->phone)
->first();
if ( Hash::check($request->password, $user['password']) ) {
Auth::login($user, true);
Auth::logoutOtherDevices($request->password);
return redirect()->back();
}
}
}
Use for route.
Route::post('/log-in', 'AuthController#login')->name('login');
Use your form.
<form action="{{ route('login') }}" method="POST">
#csrf
<input type="text" name="phone" placeholder="Telefon" class="form-control input-phone">
<input type="password" name="password" placeholder="Parol" class="form-control">
<button type="submit" class="btn">Kirish</button>
</form>
This is a tricky and annoying problem. you need to change your return from return redirect()->back(); to loading 1 known blade or a known redirect. Sometimes at login your route fails to set a back redirect. so you can try to set a return view() or a return to a known url. So for example if the login is success return to index , if not load error page.
Hope this helps

Laravel 5.7 delete does not reach controller delete method (returns 404 error page)

So i have this delete form
<form action="/remove-cart/{{ $item->rowId}}" method="POST">
#method('DELETE')
#csrf
<input type="submit" value="Remove item">
</form>
which goes to this route
Route::delete('/remove-cart/{$id}', 'CartController#removeCart');
which is supposed to go to a method
public function removeCart($id){
return $id;
}
but the method is not reached, i get a 404 page not found with url showing
http://project.dev/remove-cart/123 (123 is the value of $item->rowId)
What am i doing wrong here?
Please try Route::delete('/remove-cart/{id}', 'CartController#removeCart'); instead {$id}

Laravel 5 Plain Html MethodNotAllowedHttpException

Hello so I am trying to make an update form in Laravel using plain html. The url of my edit form is http://localhost:8000/profile/sorxrob/edit, sorxrob is the username. And the code in that url is:
<form class="form-horizontal" role="form" action="" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input name="_method" type="hidden" value="PATCH">
//more inputs
</form>
And in my controller named AccountController:
public function update(Request $request, $id)
{
$account = Accounts::findOrFail($id);
$input = $request->all();
$account->fill($input)->save();
return 'success';
}
And I am getting the error MethodNotAllowedHttpException when I click the update button. Is it because my action is equal to nothing? If yes, what is the correct way of routing there?
This is due to your action url which is not correct routing url. Use the following
(1) First define a route in your route.php file
Route::post('profile/{username}/edit', array('as' => 'profile.update', 'uses' => 'AccountController#update'));
(2) Change your action attribute from your form tag
action="{{ URL::route('profile.update', [$username]) }}"
here $username variable will be passed from your AccountController#edit method.

Move File doesn't work in Laravel 4

I am just using a form to upload a file and a simple route to move the file in my /public/images folder. But the moved file cant be found in the images directory. I am following Leanpub Laravel CodeBright book.
my blade template
<form action="{{ url('handle-form') }}"
method="POST"
enctype="multipart/form-data">
<input type="file" name="book" />
<input type="submit">
</form>
route.php
Route::get('/', function()
{
return View::make('form');
});
Route::post('handle-form', function()
{
Input::file('book')->move('/public/images');
return 'File was moved.';
});
After pressing submit button 'File was moved.' shown but there is no file in the desired directory.
I got it. I just remove leading '/' from the '/public/images' and its working fine.Now i am using 'public/images'.

Resources