In one of my pages I perform a database query like so..
function onStart()
{
$this['numz'] = Db::table('backend_users')->select('id')->count();
}
This works as expected and returns me a value that I can use in the page like so
There are a total of {{ numz }} ppl.
Now according to this, we should be able to do ...
function onStart()
{
$this['numz'] = Db::table('backend_users')->select('id')->remember(10)->count();
}
However when ever I try this, I get the following error;
Call to undefined method Illuminate\Database\Query\Builder::remember()
Any idea what the issue is or is this a bug?
This is a bug that has now been fixed in build 398+.
https://github.com/octobercms/october/issues/2639
Related
I have searched quite a bit but cannot get other solutions working for my error using pagination:
It errors on the line containing {{ $dishes->links }}
I am trying to paginate my dish.index page.
My DishController index function looks like so:
public function index()
{
$dishes = Dish::paginate(2);
return view('dishes.dish_index')->with('dishes', $dishes);
}
I have even attempted the method of following this gist however I don't think I understand enough about collections in order to remedy my issue.
I also attempted to use $dishes = Dish::all()->paginate(2); but that returns the same error.
I don't think it's directly related but I don't have a route setup with {page} either, currently I only have Route::resource('dish', DishController::class);. Seperate concern but possibly related?
Any help is appreciated!
Controller
public function index()
{
$dishes = Dish::paginate(2);
return view('dishes.dish_index', compact('dishes'));
}
View
{{ $dishes->links() }}
I know that for some it might be stupid or funny question (but I am newbie) but I need to find know how to properly use DD() method in laravel projects.
For example - I have got tasks to debug some code and functionality in my project (PHP laravel). And it always takes me for ever to find the exact file or folder or code where the problem is.
My mentor says to use DD() method to find things faster (but for learning purposes he didn't explain me a lot about how to actually use it and said to find out my self), but said that I should start with Route (we use backpack as well for our project). So after finding Route (custom.php file) which controller connects to my required route what should I do next? How do I implement dd() method (or as my mentor says dd('call here') method) to fast find what I should be looking for to solve my problem and complete my task? Where should I write this dd() and how should I write it?
Thank you for the answer in advance!
for example I have a:
public function create(): View
{
return view('xxxxxx. \[
//
//
\]);
}
and if I put dd() anywhere in the code, I get error message in my URL :(
first of all ,in Laravel we use dd() before return in order to read any variable.
in controller we often use two kinds of variables : collection(which we get its members via foreach) or singular variable (we get it via its name)for example:$var = 1; dd($var).
notice:
if you are using ajax response you will not be able to see dd() results in page ,you can see the result via network tab in your browser (if u inspect your page).
dd stands for "Dump and Die."
Laravel's dd() function can be defined as a helper function, which is used to dump a variable's contents to the browser and prevent the further script execution.
Example:
dd($users,$variable1,$var2);
You can use dd() in blade
#foreach($users as $user)
#dd($user)
OR
{{dd($user)}}
#endforeach
#dd($var1)
You can read this article, the have more example and comparison
https://shouts.dev/articles/laravel-dd-vs-dump-vs-vardump-vs-printr-with-example
As Laravel is following model-view-controller or MVC design pattern. First go to the route and check which controller is called in the URL with the related URL.
Then go to the controller. **dd**() function is basically a dump and die. you also can do this by **print** or **echo** function too.
Lets assume that I have a controller name ProductController where I have method name index.From where I need to show a list of products in a table.
// in controller
public function index()
{
$products = Products::all();
// here you think ,I need to check whether I am getting the output or
not.
dd( $products );
//Or echo $products;
return view ('product.list',compact('products'));
}
let's suppose you are getting everything but in view when you loop through the products you declare the wrong variable name or mistakenly do some spelling mistakes. and want to see the result.
In view just do the dd() method by the following way:
{{ dd($products) }}
I've followed this documnetation
(https://laravel.com/docs/8.x/pagination)
and made changes accordingly but still I am not able to paginate the required page.
This is my controller code:
public function showManageCourierAddress()
{
$viewdata = [];
$addressRepoObj = new \App\Repositories\Mongo\CourierAddressRepository();
$addressData = $addressRepoObj->getAddressList(0,(int)Session::get('organisation_id'));
$viewdata['addresslist'] = $addressData;
unset($addressData);
return view('frontend.admin.managecourieraddress', ['viewdata' => DB::table('viewdata')->paginate(15)]);
}
On the view laravel, I've added this :
{{ $viewdata->links() }}
Can anyone tell me where I'm going wrong? And what I should do?
DB::table('viewdata')->paginate(15)
returns LengthAwarePaginator instance, which implements these methods.
It is enought to call {{ $viewdata->links() }}, and it should return whole html with pagination buttons and links. You just write some styles for existing classes, or use methods as mentioned above to create your own html with your own structure.
If I did not understand your problem, please reply in comment.
I am trying to pass data from my controller to my view, but am getting an undefined variable error. usersID is a column in my MySQL table.
Here is the code in my controller
$arrayWithCount = DB :: table("users_has_activities")
-> where("usersID", "=", 19)
-> pluck("usersID");
$countNumber = sizeof($arrayWithCount);
return view('pages.progress', ['countNumber' => $countNumber]);
I have also tried the following return statement without any success
return view::make('pages.progress') -> with('countNumber', $countNumber);
I have also tried reversing the puck and where clauses without any success, I didn't have high hopes that reversing them would fix the problem but thought I would try it any way. Below is the relevant code in the blade file.
<?php echo $countNumber; ?>
This is the error I am currently getting
Undefined variable: countNumber
You code looks fine, if dd() doesn't stop execution of the controller, then another controller is executing. So double check your routes and controllers.
First sizeof should be sizeOf, and is simply an alias for count(). Most people would prefer count over sizeOf as sizeOf (in many languages) would indicate something related to size on disk.
Anywho, being that pluck returns a collection, you have access to count() directly from the collection.
You can probably simply do something like:
$countNumber = $arrayWithCount->count();
Sidenote: Unless there is a particular reason why you are using <?php ?>, in blade, it would be preferred to use {{ and }}.
I had all the controller code in a method I wasn't calling, so the variable was never passed to the blade file. The method was set up to be called on a button press, after fixing that everything works. Thanks Alexey for the help.
Have a controller function:
public function pageedit($id)
{
$page = Pages::where('pgeID','=',121)->get();
return view('admin.pageedit')->with('page',$page);
}
And a simple view:
#extends('admin')
#section('content')
#include('/partials/adminpageheader')
Edit Page {{ $page }}
#endsection
When I run this, I get the output as follows:
Intranet Web Control Panel (Pages)
Admin Home - Admin Home Page
Edit Page
[{"pgeID":"121","pgeActive":"0","pgeContent":"1","pgeMainLevel":"6","pgeLevel":"1","pgeParent":"6","pgeOrder":"3","pgeTitle":"Employee Assistance Program","pgeHeader":"Null","pgeBody":Employee Assistance Program that all employees can access 24\/7. The website, www.assist.com has an amazing amount of information available. If you are looking for training, articles, resources and more, check them out. The use","pgeNav":null,"pgeContents":"Null","pgeCallOut":"Null","pgeEditor":"Null","pgeTitleDRAFT":"Null","pgeTemplateDRAFT":null,"pgeNavDRAFT":null,"pgeContentsDRAFT":"Null","pgeCallOutDRAFT":"Null"}]
So I know it is pulling the data correctly. But when I try to display JUST the page title (pgeTitle) I get an undefined property error if I try {{ $page->pgeTitle }} and an undefined index error if I try {{ $page['pgeTitle'] }}
I know it has to be something simple I am forgetting, but I will be danged if I can figure out what it is. Thoughts?
Instead of using ->get() use ->first(). This will return a single instance as opposed to the collection, which is the difference between having to use an array accessor and not.
$page = Pages::where('pgeID','=',121)->first();
^
first==single
get==array collection
$page is an array, where the index is numeric as far as i can tell, so you should do:
{{$page[0]->pgeTitle}}
The same answer than Taxicala, but I'd suggest you to use ->first() method instead of get, since you're looking for only 1 row.
$page = Pages::where('pgeID','=',121)->first();