I have the following error.
Too few arguments to function Illuminate\Support\Collection::get()
public function upteamLogs(Request $request)
{
$logs = DB::table('upteam_product_logs');
if ($request->from_date != '' && $request->to_date != '') {
$logs->whereBetween('created_at',
array($request->from_date, $request->to_date));
}
$logs->orderBy('id', 'desc')->paginate(30)->get();
return view("product-inventory.upteam_logs", compact('logs'));
}
the method paginate and simplePaginate of the laravel Illuminate\Database\Query\Builder class return respectively and instance classes which have implemented Illuminate\Contracts\Pagination\LengthAwarePaginator or Illuminate\Contracts\Pagination\Paginator interface and those interfaces havent define get method that's main reason of the error.
You should just return the reassign the result of the paginate call directly to the $log variable like this
$logs = $logs->orderBy('id', 'desc')->paginate(30);
This will fix your error and you can loop through logs into your product-inventory.upteam_logs view without any problem
Related
I'm trying to dynamically load models based on what is being passed to it.
class MyController extends Controller{
$modelClass = "";
public function __construct(){
$this->setModel($modelName);
}
public function index(){
$this->data['rows'] = $this->modelClass::all();
return view('index', $this->data);
}
protected function setModel($modelName){
if(!class_exists('\App\Models\\'.ucfirst($modelName)) || empty($modelName))
{
return view('page500');
}else{
$this->modelClass = '\App\Models\\'.ucfirst($modelName);
}
}
}
I kept getting the error message "FatalThrowableError
Class '' not found". I expect laravel to stop execution once a view have been returned but the execution isn't loading the page500 view and isn't stopping the execution.
Your setModel function returns a view to it's callee function, __construct. And that's all it does.
To immediately raise certain error - use abort function with error code as argument:
if(!class_exists('\App\Models\\'.ucfirst($modelName)) || empty($modelName))
{
abort(500);
}
To modify error page layout - follow this instruction. Or simply create file resources/views/errors/500.blade.php.
My service is like this :
public function delete($store_id)
{
$result = $this->favorite_repository->delete($store_id);
dd($result);
}
My repository is like this :
public function delete($store_id)
{
$data = self::where('favoritable_id', $store_id)->delete();
return $data;
}
There exist error :
Missing argument 1 for App\Repositories\FavoriteRepository::delete(),
called in
C:\xampp\htdocs\mysystem\app\Repositories\FavoriteRepository.php on
line 45 and defined
Can you help me?
UPDATE
The delete function in the EloquentRepository is like this :
public function delete($id)
{
// Find the given instance
$deleted = false;
$instance = $id instanceof Model ? $id : $this->find($id);
if ($instance) {
// Delete the instance
$deleted = $instance->delete();
// Fire the deleted event
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.deleted', [$this, $instance]);
}
return [
$deleted,
$instance,
];
}
Seems like you are using this pakage: nilportugues/eloquent-repository
If that is the case then you need to change the repository code to this:
public function delete($store_id)
{
return $this->remove($store_id);
}
Have you checked which instance is returning your self::where('favoritable_id', $store_id)? It seems it is returning your EloquentRepository instance, instead of Model instance.
The difference is that EloquentRepository's delete method is delete($id), the Model's delete method is delete().
So you either need to get Model instance to use ->delete(), or use ->delete($id) on yours
I've got a problem with model's method with parameters. I've got 2 Page and ContentTranslation. I want to get ContentTranslation based on language, so I've got following method in Page model:
public function contentTranslations($language) {
return $this->hasMany('App\ContentTranslation')->where('language', $language);
}
and then I've got a method in PageController for getting the pages based on language:
public function getPagesByLanguage($language)
{
$pages = Page::orderBy('position', 'asc')->get();
foreach ($pages as $page) {
$page->contentTranslations($language);
}
$return $pages;
}
I'm returning my results in JSON format (I'm using Laravel as an api). But this approach doesn't work. It throws me error:
Call to undefined method Illuminate\Database\Query\Builder::contentTranslations()
Some time ago, I wanted to return contentTranslations for all languages, so I haven't got a parameter in contentTranslations model method and everything worked fine after calling $page->contentTranslations; in foreach loop. Why I've got the trouble with passing parameter into method?
You should use eager loading to avoid N+1 query problem:
public function getPagesByLanguage($language)
{
return Page::with(['contentTranslations' => function($q) use ($language) {
$q->where('language', $language);
}])
->orderBy('position', 'asc')
->get();
}
Also change your relation to:
public function contentTranslations() {
return $this->hasMany('App\ContentTranslation');
}
In Laravel 4.x I have a Customer Eloqeunt Model that has a relationship to a Customer_tel Eloquent Model:
class Customer extends Eloquent
{
public function tel()
{
return $this->hasMany('Customer_tel');
}
}
The Customer_tel table has a boolean column 'main'.
When I make an instance of the Customer Class in a view, then I can filter out the main number with the filter() method:
$Customer = Customer::find(1);
$Customer->tel->filter(function($tel)
{
if ($tel->main == true)
{
return true;
}
})->shift()->tel
But when I try to make a function in the class with the filter() method:
public function mainTel()
{
return $this->tel()->filter(function($tel)
{
if ($tel->main == true)
{
return true;
}
})->shift()->tel;
}
When I try to reference it in the view $Customer->mainTel, then it gives me an error "Call to undefined method Illuminate\Database\Query\Builder::filter()".
Why can't I filter the instance only outside of the class but not in the class? And is there a right way to do it?
Calling the method (tel()) returns the HasMany instance, upon which you can then call the query builder methods. Using Eloquent's magic properties, you can short-circuit it. So $customer->tel is equivalent to $customer->tel()->get(), which returns a collection. That is why it's working for you in your first example.
See the docs for more info.
A better option would be to do it in the query itself:
return $this->tel()->where('main', true)->pluck('tel');
Also note that you can create your own magic properties in Eloquent:
class Customer extends Eloquent {
public function tel()
{
return $this->hasMany('Customer_tel');
}
public function getMainTelAttribute()
{
return $this->tel()->where('main', true)->pluck('tel');
}
}
Now when you have a $customer model, you can call your magic method directly:
$tel = $customer::find(1)->main_tel;
Is it possible to pass, somehow, a parameter to a relationship function?
I have currently the following:
public function achievements()
{
return $this->belongsToMany('Achievable', 'user_achievements')->withPivot('value', 'unlocked_at')->orderBy('pivot_unlocked_at', 'desc');
}
The problem is that, in some cases, it does not fetch the unlocked_at column and it returns an error.
I have tried to do something like:
public function achievements($orderBy = true)
{
$result = $this->belongsToMany (...)
if($orderBy) return $result->orderBy(...)
return $result;
}
And call it as:
$member->achievements(false)->(...)
But this does not work. Is there a way to pass parameters into that function or any way to check if the pivot_unlocked_at is being used?
Well what I've did was just adding new attribute to my model and then add the my condition to that attirbute,simply did this.
Class Foo extends Eloquent {
protected $strSlug;
public function Relations(){
return $this->belongsTo('Relation','relation_id')->whereSlug($this->strSlug);
}
}
Class FooController extends BaseController {
private $objFoo;
public function __construct(Foo $foo){
$this->objFoo = $foo
}
public function getPage($strSlug){
$this->objFoo->strSlug = $strSlug;
$arrData = Foo::with('Relations')->get();
//some other stuff,page render,etc....
}
}
You can simply create a scope and then when necessary add it to a builder instance.
Example:
User.php
public function achievements()
{
return $this->hasMany(Achievement::class);
}
Achievement.php
public function scopeOrdered(Builder $builder)
{
return $builder->orderBy(conditions);
}
then when using:
//returns unordered collection
$user->achievements()->get();
//returns ordered collection
$user->achievements()->ordered()->get();
You can read more about scopes at Eloquent documentation.
You can do more simple, and secure:
When you call the relation function with the parentesis Laravel will return just the query, you will need to add the get() or first() to retrieve the results
public function achievements($orderBy = true)
{
if($orderBy)
$this->belongsToMany(...)->orderBy(...)->get();
else
return $this->belongsToMany(...)->get();
}
And then you can call it like:
$member->achievements(false);
Works for the latest version of Laravel.
Had to solve this another was as on Laravel 5.3 none of the other solutions worked for me. Here goes:
Instantiate a model:
$foo = new Foo();
Set the new attribute
$foo->setAttribute('orderBy',true);
Then use the setModel method when querying the data
Foo::setModel($foo)->where(...)
This will all you to access the attribute from the relations method
public function achievements()
{
if($this->orderBy)
$this->belongsToMany(...)->orderBy(...)->get();
else
return $this->belongsToMany(...)->get();
}