Factories Not Found in Laravel 6 - php

I'm stuck... I'm trying to get a set of factories working in Laravel 6, an continuously get the following error:
InvalidArgumentException with message 'Unable to locate factory with name [default] [User].'
There's several other posts about this on here and around the web, and I've tried all of those solutions. My code for the factories is as follows:
<?php
/** #var \Illuminate\Database\Eloquent\Factory $factory */
use App\Thread;
use Faker\Generator as Faker;
$factory->define(Thread::class, function (Faker $faker) {
return [
'user_id' => function () {
return factory('App\User')->create()->id;
},
'title' => $faker->sentence,
'body' => $faker->paragraph
];
});
I'm using php artisan tinker to try and generate DB entries so I can work on the rest of my models etc. The command I'm using is as follows:
factory('Thread',50)->create()
I've tried specifying the name space in the factory call, I've tried putting it in double quotes, I've tried adding the '::class' at the end. The controllers are there, the models are there, the migrations are working as expected, but every time I run the factory in tinker, it won't give it to me. Most of the documentation that I've found is referencing older versions of Laravel as well, so I'm not sure if they are providing me with correct syntax.
It's worth noting, the stock provided UserFactory doesn't work either. This is a fresh download of Laravel 6 that I did today to go through a course over at Laracasts. The course is older, focused on Laravel 5.4, but I can figure out the conversion... I just can't get this to work to save my life and I can't figure out why.
I've got the whole project uploaded here if you want to view additional code: https://github.com/aarpie100/laracasts_forum
Help?

Try sending Thread::class instead of Thread as a string. If you are sending as string it should be 'App\Thread' I think.

Factory function When you call
the class name you should write it like this (Thread :: class)

Related

Symfony registering services with ContainerControllerResolver

I am trying to use the Symfony dependency injection component and I was just wondering if anyone would be able to help with the registering of the services.
I have recently moved to ContainerControllerResolver opposed to ControllerResolver so that my routes correctly instantiate the dependencies.
This is working well enough however I have ran into an issue when a class is needed more than once.
So for ContainerControllerResolver to work you seem to have to use the full class path. Where as when you use ControllerResolver you can set your own unique string ID.
I can't see anyway to set a unique ID when using ContainerControllerResolver and you don't seem able to pass arguments to the Reference class.
Is there anyway I could rewrite the below so they are seperate instances? The ControllerResolver way of being able to set unique IDs makes sense to me but I'm just a little lost when you have to pass the full class.
$containerBuilder->register(App\Models\Database::class, App\Models\Database::class)->setArguments([
DB1, DB1USER, DB1PASS
]);
$containerBuilder->register(App\Models\News::class, App\Models\News::class)->setArguments([
new Reference(App\Models\Database::class)
]);
$containerBuilder->register(App\Models\Database::class, App\Models\Database::class)->setArguments([
DB2, DB2USER, DB2PASS
]);
$containerBuilder->register(App\Models\Reports::class, App\Models\Reports::class)->setArguments([
new Reference(App\Models\Database::class)
]);
I think I got the wrong gist of things from another post. They mentioned it is more ideal to use the controller name in the ID when using the ContainerControllerResolver. I've been able to use service IDs in the first param of the register and I've just updated my routes _controller param to go to that same name rather than the class namespace path:
$routes = new RouteCollection();
$routes->add('news', new Route('/news', [
'_controller' => 'controller.news::newsOutput',
]));

Phalcon pdo excpetion model first

I have problem with phalcon framework namely with models methods...
As you know models has included methods find() and findFirst()
I have generated model with phalcon-dev tools and now I am trying to do Model::find on it but I am getting an exception but dont know why...
There is some more informations (e.g stacktrace) :
http://exception.mateuszmarzecki.pl/
You can try change methods in model file
public static function find($parameters = array())
{
return self::find($parameters);
}
Does not look like your passing it the right parms.
SELECT FROM `nacionality`
Notice that your not selecting any fields from the database, and that is why your getting the Exception.
So... after some time of debugging I've found the problem...
For the next generation... if you don't want to lose a week as I did. Just read carefully your application config.
Problems occurs because I missed table and column annotations as well.
In my application config I have something like:
$metaData->setStrategy(new \Engine\Db\Model\Annotations\Metadata());
so Phalcon was looking for annotations in my model files, more info about this you can find there:
https://forum.phalconphp.com/discussion/1933/column-types-for-model-annotations
Happy New Year

Using $input->all() instead of Input::all() Laravel-5

I'm trying to use $input->all() as opposed to Input::all() in Laravel-5, however it doesn't seem to like it, even though I am passing the Input reference to the function, like so:
/**
* Search for a specified resource.
*
* #return Response
*/
public function search(Booking $booking, Input $input)
{
dd($input->all()); // this doesn't work
dd(Input::all()); // this DOES work
}
The error I get is:
Call to undefined method Illuminate\Support\Facades\Input::all()
Does anyone have a solution to this problem?
I don't think you're supposed to inject Facades into your Controllers. Input is a facade for Illuminate\Http\Request and it's service container binding is request. So according to the documentation, in Laravel 5 you can do Request::all() and in Laravel 5.1 you can do $request->all()
http://laravel.com/docs/5.0/requests#retrieving-input
http://laravel.com/docs/5.1/requests#retrieving-input
EDIT: This post gives some more in-depth information: https://stackoverflow.com/a/29961400/2433843
EDIT3: I think it would be great if someone could explain WHY exactly you can't inject Facades into your Controllers. I understand DI and Facades are two different things entirely, and L5+ is pushing the developers towards DI. I just don't exactly understand why injecting a facade wouldn't work, since it points towards another class, and it works when you do not inject it. Not to forget Facades and Aliases are two seperate things too. I hope someone can elaborate on this.
One more important thing about using Request or Input to access the User Input is the version of Laravel that you are using.
In the Laravel 4.2 and prior, you could have access the Input::all(), Input::get() but from Laravel 5 onwards, it's been suggested to use the Input via Request facade
Ref: https://laravel.com/docs/5.2/requests
In case if you want to make use of Input in Laravel 5.0 and onwards, then you need to add this facade in the config/app.php file under the aliases section as 'Input' => Illuminate\Support\Facades\Input::class
Once you add the facade under alias, you should start using the 'Input::all()'
Hope this helps some others, who are having the confusion as whether to use 'Input' or 'Request' for Laravel 5.0 onwards.

Using text search with MongoDB and Laravel

I'm using jenssegers/laravel-mongodb package for using MongoDB with Laravel:
And I'm trying to implement text search with MongoDB using this command:
db.place.find({$text:{$search:"hotel"}})
But I don't know how to translate this query to using it with above package and Laravel.
Well, how funny, I just found out the answer. Here is the code to get the result from text search:
\DB::collection('place')->raw()->find(array('$text'=>array('$search'=>'hotel')))->getNext();
You can use whereRaw method.
Place::whereRaw(['$text' => ['$search' => 'hotel']])->get();
You can even make use of laravel query scope for defining common sets of constraints that you may easily re-use throughout your application.
Place Model
public function scopeContains($query, $search)
{
return $query->whereRaw(['$text' => ['$search' => $search]]);
}
Place Controller
Place::contains('hotel')->get();

Laravel 4 does not extend Eloquent class

I have a Product.php file in app/models folder. Here is the content of the file:
class Product extends Eloquent{
protected $table = 'productinfo';
}
However, when I tried to run $product = Product::all();, Laravel say Call to undefined method Product::all(). It seems like Eloquent class is not extended.
I don't see anything I do wrong here. Why does it not working? Could it be any other configuration in Laravel which I mess up?
--update--
After renaming the Product.php file, it is now no longer showing error message. But the problem is, it does not properly return the result from database. Instead, the returned result is {"incrementing":true,"timestamps":true,"exists":true}. Not the result from database.
So, I tried to run a clean new Laravel project from composer and the only files that I modified is the config/database.php file to set it work with MySQL database, route.php file, and the models/Products.php file. When I browse it on browser, again it shows the same result: {"incrementing":true,"timestamps":true,"exists":true}, not database result.
I really don't know what causing it. I have tried with it in one my experiment Laravel project folder which has lots of junk in it, and it works perfectly fine here.
Do you know why it doesn't work? How to solve it?
Thank you.

Categories