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();
Related
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)
I have a laravel project with dynamic forms (created from db) and for each of this forms I need to create an XML for API with different style.
How can I create them automatically or from an template? I mean I could create them manually but this would blow the model up with hundred of code lines e.g.
if($myvar['xyz'] == "foo")
$xml .= "<SOMENODE>$myvar['xyz']</SOMENODE>";
}
But some of the elements also have child elements...
Is there an more elegant way?
Thanks
Regards
I created a Laravel package to simplify XML responses for APIs: https://github.com/mtownsend5512/response-xml
With the package it's as easy as setting up an array or collection to specify the format you want. Here's a simple example using the package:
$products = Product::all();
$array = [
'status' => 'success',
'data' => $products
];
return response()->xml($array);
That's a very simple example. If you need a more complete response transformer setup, I recommend you use Spatie's Laravel Fractal package with my own. It will help you format the data in exactly the way you want and you'll get perfectly valid xml.
This should give you everything you need to get going.
i am working on a project that need to work with spatial object in mysql and i choose redbeanphp as ORM system in this project. i found redbeanphp so terrific and awesome.
i search in google and found this link that say bindFunc method can help me in working with spatial object but i cannot find any example for using this method.
how this method work and how can i use this method?
According to the database part of that website you linked:
As of RedBeanPHP 4.1 you can bind an SQL function to a column. This is
useful for wrapping values when reading from / writing to the
database. For instance, to use MySQL spatial data types you need to
prepare the columns like this:
R::bindFunc( 'read', 'location.point', 'asText' );
R::bindFunc( 'write', 'location.point', 'GeomFromText' );
$location = R::dispense( 'location' );
$location->point = 'POINT(14 6)';
//inserts using GeomFromText() function
R::store( $location );
//to unbind a function, pass NULL:
R::bindFunc( 'read', 'location.point', NULL );
This function was added to support spatial datatypes in MySQL as shown above. The documentation isn't amazing, so I recommend looking at the source code on Github if you want to dig in deeper.
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.
Consider this code taken from here.
public function getIndex()
{
$posts = Post::orderBy('id','desc')->paginate(10);
// For Laravel 4.2 use getFactory() instead of getEnvironment() method.
$posts->getEnvironment()->setViewName('pagination::simple');
$this->layout->title = 'Home Page | Laravel 4 Blog';
$this->layout->main = View::make('home')->nest('content','index',compact('posts'));
}
As I understand it, pagination limits the number of rows, so I think paginate(10) means select first ten rows in the database. But I absolutely don't understand this.
// For Laravel 4.2 use getFactory() instead of getEnvironment() method.
$posts->getEnvironment()->setViewName('pagination::simple');
or
$posts->getFactory()->setViewName('pagination::simple');
And everything below. Mainly I don't understand what factory means and how it relates to pagination. I went to the laravel docs on Illuminate\Pagination\Factory and Illuminate\View\View but I can't find the meaning of factory. Can anyone explain the code above?
You are essentially setting how the pagination is output in HTML by selecting a specific paginator view, this allows you to have more than one type in an application or use different to the default.
Using multiple pagination types in the same application
Sometimes, you may want to use different pagination types across your
application. By default, Laravel will use the type specified in your
app/config/view.php file, so you need to override this setting when
you wish to use another type. Here is how to do so.
// This code should be in a controller or a route Closure.
// Let’s use the good old example of a list of blog posts.
$articles = Article::paginate(5);
Paginator::setViewName('pagination::simple');
/*
Alternatively, you could also use this to achieve the same result:
$articles->getEnvironment()->setViewName('pagination::simple');
For those who would like to know what’s happening under the hood, here is a more
detailed explanation:
1. Calling paginate() on an Eloquent model or a query builder will return an
instance of \Illuminate\Pagination\Paginator
2. Then, we need to get the related \Illuminate\Pagination\Environment of this
paginator via the well-named getEnvironment() method.
3. Finally, we can specify the pagination type we need. The default value is
'pagination::slider'.
The pagination types that are available by default are located in the
vendor/laravel/framework/src/Illuminate/Pagination/views directory.
*/
Source: http://laravel-tricks.com/tricks/using-multiple-pagination-types-in-the-same-application