Laravel 5 - Where should I write this function, and how? - php

Goodevening,
I have an overview page where I display all my project items.
I have a model (Project.php) a controller (ProjectController) where I send the variable $project (this includes all the information for each project) to the specific view.
Note: Each project has a own row in the database. (Quit obvious I guess)
Now I also have a table 'tasks' related to a specific project. In my view I wanna display how much of the total tasks are 'done'. (This is doing with the column 'done' (true/false, boolean)).
Now because I have a foreach function in my view (to display each individuele project) I can make the function in the view. But the Laravel framework is there for reasons. And writing out a lot of php in a view isn't the right way.
But, where should I make this function? And how can I use it in my foreach (in the view). Ofcourse I can make a new foreach in my model or controller and send that variable to my view. But then I can't use that one in my view-foreach as things will get mixed up.
I don't know how/where I can set up a function like this on a clean way.
Kinds regards,
Dylan

You can create a function in app directory(forexample app/Helpers/), or wherever you want and name it yourHelperFunction.php.
After creating that file, Laravel won’t recognize the file until it is registered inside composer.json file. Add files array inside autoload section.
.
.
.
"autoload": {
"files":[
"app/Helpers/yourHelperFunction.php"
]
}
.
.
.
then do composer dump autoload and you are ready to use the function inside the blade

Use Blade engine, not Core PHP loops.
It is wise to use blade engine then core php codes.
You can see the documentation
Laravel 5 Blade Template

Thanks for the help. I've figured it out (I guess?).
I have this function in my Project Model.
public function getTaskDonePercentage($id) {
$tasks = Task::where('project_id', $id)->count();
$tasks_done = Task::where([
['project_id', $id],
['status', 'done']
])->count();
if ($tasks_done > 0) {
$calc = ($tasks_done / $tasks) * 100;
}
else {
$calc = '100';
}
return round($calc) . '%';
}
Then in my view I just call the function in the foreach like:
#foreach
{{ $project_item->getTaskDonePercentage($project_item->id) }}
#endforeach
Tbh I still wanna know if this approach is a good one. (Or, if not, why it isnt?).
Thanks!

Related

how to use DD() method in Laravel projects?

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) }}

How to paginate in laravel 8?

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.

Save blade templates to database rather than file

I want to save my blade templates to database, because the header and footer of each page is customizable for the user. I want to let my users create the layout themselves and then for each request from a given user, I want to serve the page, using the layout specified by that user.
The necessary variables that are passed by the controller are provided to them in the documentation.
Note: I trust my users. They are all stake-holders of the project and are programmers, so server side code execution is acceptable.
Although this is an old post but just in case someone stumbles across it like I did. I achieved similar while using the Laravel Framework, by saving the view in database such that, whenever I need to display the view, I retrieve it from DB, and load it into a file using the file_put_contents() php function and render it with the view() method. For example;
$blade = DB::table('pages')->where('name', 'index')->first();
file_put_contents('template.blade.php', $blade->view);
//Note if I also need to pass data to the view I can also pass it like so
//$data = ['page_title' => 'Testing Blade Compilation using views Saved in DB'];
// return view(template, $data);
return view('template');
While again in my own case for added security, I created base templates with the blade templating scheme & injected user created inputs into the template after sanitizing the generated input using HTMLPurifier and rendering the view. For example
$view = view('base.template')->render();
//similarly like the above I can load any data into the view like so
//$data = ['page_title' => 'Testing Blade Compilation using views Saved in DB'];
//$view = view('base.template', $data)->render();
$purifier = new HTMLPurifier(HTMLPurifier_Config::createDefault());
$with_purified_input = $purifier->purify($user_generated_input);
str_replace('view_variable', $with_purified_input, $view);
return $view;
I realised that I can improve security and caching if I just let them insert the static content only. The only thing I need to change is the main content, so I can just let them set a token where the content is to be placed. As is in the above answer by #huzaib-shafi , I did the following...
//In controller
$content = View::make('final',compact('data'));
$token = "<meta name='_token' content='" . csrf_token() ."'";
$scripts = View::make('final_scripts',compact('data'));
$view = str_replace_first("<%content%>", $content, $templateInDatabase);
$view = str_replace_first("<%token%>", $token, $view);
$view = str_replace_first("<%scripts%>", $scripts, $view);
return $view;
This enforces them to use bootstrap in their template, because I use bootstrap styles in my blade templates, but it is acceptable in my case.
I asked and answered a similar question some days ago. So far as I know, Blade doesn't process view content from database columns. Although you can use compileString() method of View. But you should have a look at the following questions.
Extend Blade Template from Database stored string
Let users create custom blade layouts / store in database
In Laravel 9 you can use blade Facades to render from DB:
use Illuminate\Support\Facades\Blade;
return Blade::render('Your Blade Content {{ $parameter1}}', ['parameter1' => 'Name']);

View composer runs multiple times, how to reduce to 1

I made a view composer in Laravel 5. When i use a wildcard *, to add something to all my views, it get's called at least twice. It runs when my master template is loaded, and again when my content page is included. This will give problems in the future, because it executes the query it does multiple times. I was able to fix the multiple querying by storing it in a static variable :
class StoreComposer {
static $composed;
public function __construct(StoreRepository $repository)
{
$this->store = $repository;
}
public function compose(View $view)
{
if(static::$composed)
{
return $view->with('store', static::$composed);
}
static::$composed = $this->store->pushCriteria( new WithCategories() )
->pushCriteria( new WithSettings() )
->applyCriteria()
->all();
$view->with('store', static::$composed);
}
}
My question is, is there a way to make sure it only runs once, no matter how many views i load, or is there another solution to this? The way i fixed it now doesn't feel right to me. Thanks!
Unfortunately no, there is no way to make it run once, due to the way View Composers are implemented. The Illuminate\View\View::renderContents() method is responsible for calling the composer bound to a view, and since any form of view rendering (Blade template inheritance or simple #include statements) executes that method, it means that when any view is rendered any composer bound to it gets triggered.
Since in your case you use a * wildcard to bind all views, if your page renders ten views, the composer will get executed ten times. However your approach looks like a good solution to solve this shortcoming.
You can use config here to resolve multiple times query run issue for example show below code.
public function compose(View $view)
{
if(!Config::has('composeVars'))
{
Config::set('composeVars') = [
'data' => User::all();
];
}
$view->with('*', Config::get('composeVars'));
}
Try this singleton solution or use cache https://laracasts.com/discuss/channels/laravel/executing-a-view-composer-only-once
On Laravel 5.6.38 works fine

Laravel 4 - Where to call View::share

all
I know View::share should be available anywhere within my application, A common place is in my routes.php file.
I am doing this.
1) User login system, after login successfully, share something for all the views.
2) when user click other pages, can use the shared variables.
Ok, my controller.
public function index() {
$data = array(
"pageTitle" => "index",
);
$privilegeMenu = $this->privilegeApi->getUserPrivilegeByUserId(Session::get('uid'));
$topMenus = $this->menuApi->getTopMenuById($privilegeMenu);
$subMenus = $this->menuApi->getSubMenuesById($privilegeMenu);
View::share('topMenus', $topMenus);
View::share('subMenus', $subMenus);
return View::make('home.index',$data);
}
So, because I am using View share, that it is to say, in every other views, I can use topMenus and subMenus now.
But when I click other pages, I got the error: Undefined variable: topMenus.
So, I am so confused what happened? I dive into View::share source code
Laravel have a class named Environment under namespace Illuminate\View.
protected $shared = array();
public function share($key, $value = null)
{
if ( ! is_array($key)) return $this->shared[$key] = $value;
foreach ($key as $innerKey => $innerValue)
{
$this->share($innerKey, $innerValue);
}
}
public function shared($key, $default = null)
{
return array_get($this->shared, $key, $default);
}
And I found when user login successfully, topMenus shared successfully. But when I click other pages, can't topMenus in shared.
It seems everything OK, I am confused. Any one knows ?
Thanks in advanced.
This is because your View::share statement is never ran for other routes. In Laravel 4.x, there are quite a few places you could put it where it would be ran every time, but these two are most commonly used:
The easiest and most simple way is to just add it at the end of your app/start/global.php which is ran at the start of every request, for all environments.
The other way is to create a new service provider, make sure it's autoloadable and add it to your app/config/app.php's providers array.
If all you want is share a few views, I'd say it's kinda of an overkill to create a whole new class just for that. If, however, you start noticing your app/start/global.php file is getting too cluttered, I'd recommend you start splitting stuff into service providers.
PS: In Laravel 5, app/start/global.php was removed and you're left only with the second option.

Categories