laravel 5.5 storeAs not work well - laravel-5

I need to save an image, consider that the image arrive correctly from a request, after that I need to save in storage/app.
Below the code in controller class:
public function getImage(ImageRequest $request)
{
$path = $request->image->storeAs( 'images','img.jpg','local');
}
i see this error without nothing saved:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Call to a member function storeAs() on null

Call to a member function storeAs() on null
This error tells you that you are trying to call the storeAs() method on a null object, which means $request->image is null/doesn't exists.

Related

Laravel: Missing Argument error on a POST

I keep getting the error "Missing argument 1 for App\Http\Controllers\Users::deleteMobileAssets()" . I am making a call from my frontend with Vue. When I check the headers, it seems correct but I'm not sure what is causing the error. I've tried wrapping imageType in brackets too: {imageType: imageType} but still same error.
deleteImage(imageType) {
axios.post('/delete-mobile-assets', imageType);
}
public function deleteMobileAssets($imageType)
{
}
POST data is included in the body of the request that way you are getting Missing argument 1. Try this
deleteImage(imageType) {
axios.post('/delete-mobile-assets', {imageType:imageType});
}
public function deleteMobileAssets(Request $request)
{
$request->imageType
}
Or if you want to implement DELETE method. have a look Delete Method in Axios, Laravel and VueJS

Laravel: Function name must be a string

I'm currently working on a project. It was all working just fine until i tried to migrate some tables that I edited. I got this error:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Function name must be a string
Since I doesn't directly show me where the error is, I couldn't find it. Last things I changed before I tried to migrate tables:
Migrations
Laravel colletive/html forms
Store method in my controller
As I know, migrations and forms shouldn't be a problem with this error, so here's my controller code:
public function store(Request $request)
{
$user = Auth::user();
$input = $request->all();
if ($file = $request->file('photo_id')){
$name = time().$file->getClientOriginalName();
$file->move('images', $name);
$photo = Photo::create(['file'=>$name]);
$input['photo_id'] = $photo->id;
}
$user->posts()->create($input);
return redirect('/userPanel');
}
If the error isn't even in a controller code, where could it be. Any help appreciated.
I have found out it was because of a very silly mistake, i was calling a variable as a function...
$ownedMacs = intval($data()['owned_mac']);
Changed to
$ownedMacs = intval($data['owned_mac']);
This error message usually appears when we are doing something real stupid! :)
Same issue in Laravel can solve
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR)
Function name must be a string
When you checked on storage/logs/laravel.log
local.ERROR: Function name must be a string {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Function name must be a string at E:\\Project\\workspace\\turttyKidsProject\\app\\Http\\Controllers\\SiteContactController.php:52)
[stacktrace]
Solution
Data field requesting another format but you are trying to save an object.
$objectSave= new ObjectClass;
$objectSave->feild_db = $request->post('request_name');
Check whether you access request with your form submit method. it may be POST or GET

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message

the following route when i submit a form is working fine
Route::put('/autorisation', 'AdministratifController#update_autorisation')->name('administartif.updateautorisation');
but i'm wondering why when i try to access http://example.com/autorisation
it throws
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message
insted of page not found
it's not about the form it's about going to a browser and type example.com/autorisation and then u get that error of no message in place of 404
PS: in my routes i didn't define this Route::get('/autorisation')
By accessing the page yourself, you do a GET HTTP request instead of a PUT.
You need to set the route as GET instead of PUT.
You are defining the route as PUT but you are trying to use it like a GET route. That's why you are getting MethodNotAllowed.
If you want to show a 404 error instead this MethodNotAllowed (it would be a Whoops when DEBUG=false) you should handle it in your handler.
Edit report method at App\Exceptions\Handler to looks like this:
public function report(Exception $exception)
{
if ($exception instanceof MethodNotAllowedHttpException) {
abort(404, 'Page not found');
}
return parent::report($exception);
}
Please change your route and try this:
Route::post('autorisation', 'AdministratifController#update_autorisation')->name('administartif.updateautorisation');

laravel: Argument 1 passed to App\Exceptions\CustomException::report() must be an instance of Exception,

I have created a custom exception class in Laravel 5.2. It works well till laravel 5.4.
When Im trying to use the same custom exception class with laravel 5.5 it is throwing following error.
Type error: Argument 1 passed to App\Utility\Exceptions\CustomException::report() must be an instance of Exception, none given, called in /var/www/html/bubbles/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php on line 102 {"exception":"[object] (Symfony\\Component\\Debug\\Exception\\FatalThrowableError(code: 0): Type error: Argument 1 passed to App\\Utility\\Exceptions\\CustomException::report() must be an instance of Exception, none given, called in /var/www/html/bubbles/vendor/laravel/framework/src/Illuminate/Foundation/Exceptions/Handler.php on line 102 at /var/www/html/bubbles/app/Utility/Exceptions/CustomException.php:39)
Here is the custom exception class I've been using
<?php
namespace App\Utility\Exceptions;
use Illuminate\Support\Facades\Lang;
use Exception;
class CustomException extends Exception
{
private $error_code = NULL;
private $error_info = NULL;
function __construct($type = NULL, $errors = NULL)
{
$this->error_code = $type['error_code'];
$this->error_info = $errors;
$message = Lang::get('exceptions.'.$this->error_code);
parent::__construct($message, $type['code'], NULL);
}
public function report(Exception $exception)
{
parent::report($exception);
}
public function getErrorCode()
{
return $this->error_code;
}
public function getErrorInfo()
{
return $this->error_info;
}
}
// end of class CustomException
// end of file CustomException.php
Could anybody will explain me why it is throwing argument must be instance of exception ? Any help would be greatly appreciated.
My programming environment
PHP 7.0.1
Laravel 5.5
The exception handler in Laravel 5.5 checks if the exception has a report method, and if so, let the exception handle the reporting itself. This means that the handler will see your report method, and call $e->report();, but your report method requires a parameter.
This is done in Handler::report.
You either need to remove the parameter in your report method (it should be reporting itself; $this) if you want to use this functionality, or rename the method if you don't want Laravel to call it (and fail).
Relevant: Laravel 5.5 Adds Support for Custom Exception Reporting

Laravel global query scope throws max function nesting level reached

I'm trying to replace a query scope with a global query scope, but while my query scope works fine the global one causes the following exception:
Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_ERROR)
Maximum function nesting level of '100' reached, aborting!
here is my global scope apply() method
public function apply(Builder $builder)
{
$builder->whereHas('lifetime', function($query)
{
$query->whereNested(function($query)
{
$query->whereNull('lifetimes.days')
->orWhereRaw('NOW() < DATE_ADD(updated_at, INTERVAL lifetimes.days DAY)');
});
});
}
I just extracted the code in the original query scope and put it here.
The problem is with the whereHas() method, its callback never called and if i replaced it with another query building method like a where('col', 'value') there would be no error
In fact it indirectly calls getQuery() on the model then it is handled with the Model::__call() after that the global scopes be called again on the model, including mine and causes never ending recursive calls.
stack frames (starts from up and goes down):
Model::all()
Model->newQuery()
Model->applyGlobalScopes()
MyScope->apply()
Builder->whereHas()
Builder->has()
Relations\BelongsTo->getRelationCountQuery()
Relations\Relation->wrap()
MyModel->getQuery()
Model->__call()
Model->newQuery()
Model->applyGlobalScopes()
MyScope->apply()
Builder->whereHas()
....
They have fixed this. issue on github

Resources