What should I do to pass the values that you order by in patinate?
routes.php
$ondata = DB::table('official_news') -> orderBy('created_date', 'desc') -> get() -> paginate(7);
return View::make('main',array('ondata' => $ondata));
error
Call to a member function paginate() on a non-object
I need your help
This should do the trick:
$ondata = DB::table('official_news')->orderBy('created_date', 'desc')->paginate(7);
It will be better if you order it by the id because it is going to be faster from the database.
$posts = Post::orderBy('id', 'desc')->paginate(6);
https://laracasts.com/discuss/channels/laravel/combining-paginate-with-orderby
It works fine for Laravel 5.1.
You can use something like this in your controller file.
$transactions = Transaction::orderBy('name')->paginate(10);
return view('index', compact('transactions'));
If you are using scope
public function scopeSearch($query, $term)
{
$term = "%$term%";
$query->where(function ($query) use ($term) {
$query->where('name', 'like', $term)
.............
..............
->orWhere('pan_number', 'like', $term);
})->orderBy('created_at', 'DESC');
}
Related
Have a query, how I can filter results by translation relation (by name column)
$item = Cart::select('product_id','quantity')
->with(['product.translation:product_id,name','product.manufacturer:id,name'])
->where($cartWhere)
->get();
my model
Cart.php
public function product($language = null)
{
return $this->hasOne('App\Models\Product','id','product_id');
}
Product.php
public function translations()
{
return $this->hasMany('App\Models\ProductTranslation','product_id','id');
}
Update v1.0
do like this, but query takes too long time
$item = Cart::select('product_id','quantity')
->with(['product.translation', 'product.manufacturer:id,name'])
->where($cartWhere)
->when($search,function ($q) use ($search) {
$q->whereHas('product.translation', function (Builder $query) use ($search) {
$query->where('name', 'like', '%'.$search.'%');
$query->select('name');
});
}
)
->get() ;
Inside the array within your with() method, you can pass a function as a value.
Cart::select('product_id','quantity')
->with([
'product', function($query) {
$query->where($filteringAndConditionsHere);
}
]);
https://laravel.com/docs/7.x/eloquent-relationships#eager-loading
I am using the laraval model to query results from several tables. Now I do have this function:
public function index(Request $request)
{
$roles = Roles::withcount('permissions')
->withcount('users')
->orderBy('name')
->get();
return view('UserManagement::roles.overview', compact('roles'));
}
$request is filled with the filter I want to append. But how to set a filter including pagination as I can't append the Roles with ->paginate()
Maybe somebody can help me on this one?
Thanks to Luis for pointing me to the right direction:
$search = $request->has('search') ? $request->search : null;
$roles = Roles::withcount('permissions')
->withcount('users')
->when($search, function ($query, $search) {
return $query->where('slug', 'like', '%' . $search . '%');
})
->orderBy('name')
->paginate(20);
you need to use a conditional clause
$roles = Roles::withcount('permissions')
->withcount('users')
->when($request->field_you_want_to_filter, function ($query, $request) {
return $query->where('field_you_want_to_filter', $request->field_you_want_to_filter);
})
->orderBy('name')
->paginate(20);
I'm trying to build a search functionality in my laravel app. I want to search on the name of a tag that is related with a pivot table to my loops. But I get the error 'undefined variable : query'
This is my controller method :
public function search($query) {
$searchResult = Loop::whereHas('tags', function ($q) {
$q->where('name', 'LIKE', '%'. $query .'%');
})->get();
return Response::json($searchResult);
}
You should pass $query variable into a closure with use(), like this:
$searchResult = Loop::whereHas('tags', function ($q) use ($query) {
$q->where('name', 'LIKE', '%'. $query .'%');
})->get();
http://php.net/manual/en/functions.anonymous.php (check example #3)
I have this function to get the results from joining relationships. However, the constraints don't seem to be taking effect. If I comment out the constraints, it still it gives the same result set as the uncommented constraints.
public function singleCategory($type, $category)
{
$track = TrackType::with(['tracks.subgenres'], function($query, $category) {
$query->where('name','=', $category);
})->where('name', '=', $type)->get();
dd($track->toArray());
}
Any help would be really appreciated :)
The constraining closure is only passed one parameter: $query. The other parameter you're trying to access ($category) needs to be made available via the use keyword:
public function singleCategory($type, $category)
{
$track = TrackType::with(['tracks.subgenres'], function($query) use ($category) {
$query->where('name', '=', $category);
})->where('name', '=', $type)->get();
dd($track->toArray());
}
Edit
The above will not affect the TrackTypes that are returned, it will only limit the related subgenres that are eager loaded. Based on the comments, it seems as if you are trying to limit the TrackTypes returned to those that contain a certain subgenre. In this case, you need to use the whereHas method:
public function singleCategory($type, $category)
{
$track = TrackType::with('tracks.subgenres')
->whereHas('tracks.subgenres', function($query) use ($category) {
$query->where('name', '=', $category);
})
->where('name', '=', $type)
->get();
dd($track->toArray());
}
Edit 2
It sounds like your target result set is actually the tracks, and not the track types. If so, you probably want to start by querying the tracks, and add in your filter criteria from there (I don't know your exact model and relationship names, so adjust accordingly):
public function singleCategory($type, $category)
{
$track = Track::with(['tracktype', 'subgenres'])
->whereHas('tracktype', function($query) use ($type) {
$query->where('name', '=', $type);
})
->whereHas('subgenres', function($query) use ($category) {
$query->where('name', '=', $category);
})
->get();
dd($track->toArray());
}
If you really are looking to start with the TrackType, then in addition to adding the whereHas() to find the TrackType, you need to add a whereHas() to filter down the Tracks that are eager loaded:
public function singleCategory($type, $category)
{
$track = TrackType::with(['tracks' => function ($query) use ($category) {
$query->whereHas('subgenres', function($query) use ($category) {
$query->where('name', '=', $category);
});
}, 'tracks.subgenres'])
->whereHas('tracks.subgenres', function($query) use ($category) {
$query->where('name', '=', $category);
})
->where('name', '=', $type)
->get();
dd($track->toArray());
}
The first example is much cleaner. In plain English, the first example would sound like "get all tracks of a type and a subgenre". The second example would sound like "get the track type, but only if it has tracks in a certain subgenre; also, only load those tracks within that subgenre".
I have a scope method in my model named Book.
public function scopeBookAuthor($query, $input = array()){
if($input['book_author'] != ''){
return $query->where(function ($query) {
$query->where('book_author_last_name', 'LIKE', "%".$input['book_author']."%")
->orWhere('book_author_middle_name', 'LIKE', "%".$input['book_author']."%")
->orWhere('book_author_first_name', 'LIKE', "%".$input['book_author']."%");
});
}
}
An error occurred inside the function in the 3rd line. it says Undefined variable: input.
I tried to include the input variable as another parameter but it didn't work
return $query->where(function ($query, $input) {...
Is there a way to make this possible? thanks in advance.
There is a dirty hack:
return $query->where(function ($query) use ($input){