Here is my code in laravel,
use Illuminate\Pagination\LengthAwarePaginator;
$result = DB::connection('mysql')->select("select * from login_master")->paginate(5);
Its showing Call to a member function paginate() on array. How to user paginate() in this code.
Try this:
DB::connection('mysql')->table("login_master")->paginate(5);
If using Eloquent:
LoginMaster::paginate(5);
Related
I am trying to fetch the the id and the name of the categories which is related to my services. A service has many categories and a categories belongs to a services. However when I try to get the id and the name as an array to return it gives me this error.
array_key_exists(): The first argument should be either a string or an
integer.
Here is my method or function.
public function getCategories($idService)
{
$service = Service::findOrFail($idService);
return $service->categories->get(['id','name']);;
}
and here is the defined route.
Route::get('service/{service}/categories', 'ServiceController#getCategories');
I tried to look and browse for it but can't find any solution at all.
Use pluck() method instead
return $service->categories->pluck('id','name');
The name of the parameter has to be equal to the wildcard and you need to use pluck() as mentioned in another comment, in your case:
public function getCategories($service)
{
$service = Service::findOrFail($service);
return $service->categories->pluck(['id','name']);
}
If service is a model you can also use eloquent:
public function getCategories(Service $service)
{
return $service->categories->pluck(['id','name']);
}
i guess it related to with eager loading..need to use eager loading to fetch the relationship.. then use laravel collection if you want to filter more
public function getCategories($idService)
{
return Service::with(['categories' => function ($query) {
$query->select('id', 'name');
}])->findOrFail($idService);
}
This question already has answers here:
Property [title] does not exist on this collection instance
(8 answers)
Closed 5 years ago.
I'm using Laravel 5.5.
I'm trying to get an object instance with the Query Builder of Laravel.
public function functionName(Request $request) {
$seance = Seance::where('id_seance', '=', $request->idSeance)->get();
return $seance->type_seance;
}
My model : Seance(id_seance, type_seance, capacite_seance, niveau_seance, avec_coach, date_seance, heure_seance).
However I get this error :
Exception: Property [type_seance] does not exist on this collection
instance
Thank's for help!
Get() method return a collection object it's like an array, you need to use first method to get the first object:
public function functionName(Request $request) {
$seance = Seance::where('id_seance', '=', $request->idSeance)->first();
return $seance->type_seance;
}
You can use find() method too but id_seance should be the primary key of your table:
$seance = Seance::find($request->idSeance);
return $seance->type_seance;
Use first() instead of get() to get an object instead of collection of objects:
$seance = Seance::where('id_seance', $request->idSeance)->first();
The difference between these methods is explained here.
When using the get() method, it will return a collection rather than an instance of the model. I suggest using thefirst() method instead.
Better yet, useSeance::find($request->idSeance) or Seance::findOrFail($request->idSeance). This assumes that you've defined your primary key on your model correctly.
I try to get the data array as following:
$coupons = $user->coupons::where('is_activated_flg', 1)->where('is_used_flg', 0)->lists('amount');
I have this error:
Non-static method Illuminate\Support\Collection::where() should not be called statically
Could you tell me what the problem is?
You might want to change it into something like this:
$couponQuery = $user->coupons()->where('is_activated_flg', 1)->where('is_used_flg', 0);
$couponCollection = $couponQuery->get();
..or combined:
$coupons = $user->coupons()->where('is_activated_flg', 1)->where('is_used_flg', 0)->get();
If you are using Laravel 5.1 or slightly below you might want to consider using pluck instead of lists:
https://laravel.com/docs/5.1/collections#method-pluck
$plucked = $collection->pluck('name');
$plucked->all();
Try to send an array of where clauses
from official docs
$users = DB::table('users')->where([
['status','1'],
['subscribed','<>','1'],
])->get();
https://laravel.com/docs/5.2/queries#selects
I'd probably define an eloquent relationship in my user model.
/**
* User can have many coupons
*/
public function coupons()
{
return $this->hasMany('App\Coupons')->where('is_activated_flg', 1)->where('is_used_flg', 0);
}
and then call it like
$user = User::findOrFail($id);
$coupons = $user->coupons;
I have a issue related to Laravel5 Manual Pagination.
I need to process an array of query builder result and some other arrays and make a pagination for that.
controller.
public function searchCategory($id){
$arr = DB::table('business_businesscatagories')->lists('fk_business_id');
return Paginator::make($arr, count($arr), 2);
}
Namespaces..
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
But I got the error like below..
FatalErrorException in IndexController.php line 122:
Call to undefined method Illuminate\Pagination\LengthAwarePaginator::make()
How can I solve this issue....
I examined Illuminate\Pagination\LengthAwarePaginator and was able to confirm that it doesn't have make method.
However you can achieve this by using its constructor method.
__construct(mixed $items, int $perPage, int|null $currentPage = null, array $options = array())
Since you already have the correct namespace specified:
use Illuminate\Pagination\LengthAwarePaginator as Paginator;
You could simply do this:
$paginator = new Paginator($items, $total, $per_page);
I am very new to php and yii2, i am trying to read all data records by active record
The code below give error Call to a member function offset() on a non-object
$cmylist = ClassInfo::find()->all();
$pages = new Pagination(['totalCount' => count($cmylist)]);
$models = $cmylist->offset($pages->offset)->limit($pages->limit)->all();
Seems cmylist is a array and i can not call offset and count on it
This is really made me crazy, thanks for your help
Of course, if You call all() it's create final result. Call ->offset() before ->all()
just see
http://www.yiiframework.com/doc-2.0/yii-data-pagination.html
Calling all() returns array of result models, You need to adjust Your code this way:
$cmylist = ClassInfo::find(); //activeQuery instance
$pages = new Pagination(['totalCount' => $cmylist->count()]);
$models = $cmylist->offset($pages->offset)->limit($pages->limit)->all();