Laravel: Function name must be a string - laravel

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

Related

Laravel Illuminate\Database\Eloquent\Collection::createToken does not exist

After I create a new user I try to create token via
$user = User::where('user_id', '=', $userid)->get();
$user->createToken('name')->accessToken;
Then i got the following error:
Method Illuminate\Database\Eloquent\Collection::createToken does not
exist.
Thanks
You are calling for a collection of users when you use the get() method, which won't have the createToken method (which is exactly what the error message is telling you).
You need to call for a single User model:
$user = User::find($userid);
And then, assuming you have the createToken method on your User model, this should work.
EDIT per comments:
You may have some other issue that is preventing the creation of the token in addition to the original issue of the collection vs an object. Try to create the token first:
$user = User::find($userid);
$token = $user->createToken('name');
Then if you will either get an error (if your createToken method is incorrect, or the parameter 'name' is not correct, etc), or you will have the token, and can then draw the accessToken from the new variable, $token.
Like this:
$accessToken = $token->accessToken
Either way, this will give you the diagnostics to bug hunt.

Having trouble with Model Bind - Laravel Breadcrumbs

Having trouble with Model Bind on davejamesmiller/laravel-breadcrumbs.
I'll try to be brief, but if you need more data just ask =D
This is my controller/action signature:
public function edit(Request $request, Vertical $vertical, UserMacro $macro)
And this is my BC for the corresponding route:
Breadcrumbs::register('macro-edit', function (Generator $breadcrumbs, $vertical, $macro) {
$breadcrumbs->parent('macro-index');
$breadcrumbs->push($macro->name, route('macro-edit', [$vertical->_id, $macro]));
});
I'm getting the string ID on $vertical and $macro, breaking on $macro->name. If I add Hints as the action have I get aType error.
Trying to get property of non-object (View: /.../resources/views/layouts/app.blade.php) (View: /.../resources/views/layouts/app.blade.php)
Type error: Argument 2 passed to DaveJamesMiller\Breadcrumbs\BreadcrumbsServiceProvider::{closure}() must be an instance of App\Vertical, string given, called in /.../vendor/davejamesmiller/laravel-breadcrumbs/src/BreadcrumbsGenerator.php on line 68 (View: /.../resources/views/layouts/app.blade.php) (View: /.../resources/views/layouts/app.blade.php)
I didn't analyze core code of library, so i don't know why controllers work, but breadcrumbs don't. Recipe to working model binding is use proper route naming convention.
Breadcrumbs::for('messages.show', function($trail, \App\Models\MassMessage $massMessage) {
$trail->parent('Index');
$trail->push('Show', route('messages.show', $massMessage));
});
Route::get('messages/{massMessage}', 'MessageController#show')->name('messages.show');
// error (controllers are fine)
Route::get('mass-mmessages/{massMessage}', 'MessageController#show')->name('messages.show');
// works both
The same problem is with resource route.
edit:
I was wrong. Controller also doesn't work. It not raise error but it pass empty Eloquent object. In my case i needed to change $massMessage variable's name into $message in both places and works fine, now.

Property [specifications] does not exist on this collection instance

SOLVED: Answer below.
I upgraded my Laravel project from 5.3 to 5.4 and then 5.5.
Only thing that is broken at the moment is when I go to a product edit page I get error:
Property [specifications] does not exist on this collection instance.
Exception:
public function __get($key)
{
if (! in_array($key, static::$proxies)) {
throw new Exception("Property [{$key}] does not exist on this collection instance.");
}
return new HigherOrderCollectionProxy($this, $key);
}
Which is caused by this line in the blade template:
#if($categories->specifications->first())
$categories variable is passed to view from ProductController like this:
$categories = Category::with('specifications.attributes')->find($product->getCategoryId());
What has changed in 5.4/5.5 that could have broken this line of code?
For some reason I had to add ->first() to $categories before accessing specifications.
$categories->first()->specifications->first()
On the same view this $product->categories->first() works fine and is returned the same way as categories in controller, but doesn't require another ->first(). No idea why.

Laravel Dataset Parsing Error

I need to parse this dataset I already but I tried several ways but it doesnt work.
{"id":2,"area_id":"1","payment_term_id":"1","payment_type_id":"1","trade_channel_id":"1","salesman_id":1,"customer_name":"xxx","contact_number":"1231212","fax_number":"1231232","home_address":"asd","delivery_address":"dasd","email_address":"test#edit.com","created_by":"4","updated_by":"4","is_active":"1","created_at":"2017-10-17 06:02:59","updated_at":"2017-10-17 06:13:22"}
I tried this one but This error says that
(1/1) ErrorException
Trying to get property of non-object
$details = $request->get('details');
return $details->id;
I also tried this one
$details = $request->get('details');
return $details['id'];
(1/1) ErrorException
Illegal string offset 'id'
I'm using laravel 5.4
Since the data comes from the request, it a JSON and not a collection or an object. So, you need to do something like this:
return json_decode($request->details)->id;
To use as array like $details['id'] try this :
$details = json_decode($request->details, true);
echo $details['id'];
To retrieve the JSON payload in your controller, use:
$details = json_decode($request->details)->id);
dd($details);
instead of
$details = $request->get('details');
return $details['id'];

Missing argument 1 for App\Http\Controllers\ProductsController::edit()

Im working on editing of an already saved product and i am getting the following error message on my browser
ErrorException in ProductsController.php line 88:
Missing argument 1 for App\Http\Controllers\ProductsController::edit()
in ProductsController.php line 88
My route controller is as below:
Route::get('productsedit', array('as'=> '/productsedit','uses'=>'ProductsController#edit'));
The function is as below
public function edit($id)
{
//find the post in the db and sav it as a variable
$product = Products:: findOrFail($id);
//return view and pass in the var previously created
return view('/productsedit')->withProducts($product);
}
Where iam i going wrong
In your edit method on your ProductsController you require a parameter ($id), but you don't have that value in your route . Which is what this error is saying.
[Missing argument 1 for
App\Http\Controllers\ProductsController::edit()]
Your route:
Route::get('productsedit', array('as'=> '/productsedit','uses'=>'ProductsController#edit'));
Will have to change to something like this:
Route::get('products/{$id}/edit', 'ProductsController#edit');
When calling the route in your view it will have to look something like this:
'products/{{$product->id}}/edit'
Extra:
You might want to take a look at Resource controllers since you are not really following restfull practices when it comes to your routes
https://laravel.com/docs/5.4/controllers#resource-controllers
your should give route id
Route::get('productsedit/{id}', array('as'=> '/productsedit','uses'=>'ProductsController#edit'));
and in your function should like this
`public function edit($id = null)
{
//find the post in the db and sav it as a variable
$product = Products:: findOrFail($id);
//return view and pass in the var previously created
return view('/productsedit')->withProducts($product);
}`
hope this work !!!

Resources