Summation method laravel [closed] - laravel

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I have two fields, views, which just counts views, and add_to_views, a number that we add to views.
I need to display the sum of these numbers in the blade. Of course, you can just do this and everything will work.
{{ $article->views + $article->add_to_views }} Views
But I want to make the summation function separately and output the already ready number
Here is what I am trying to do, in the Article model itself I create a method
public function getTotalViews() {
return $this->views + $this->add_to_views;
}
Further in the controller I call it
$article = Article::where('id')->first();
$article->getTotalViews();
And I bring to the blade
{{ $article->getTotalViews() }} Views
But I get the error
Call to a member function getTotalViews() on null

You're missing the ID for the Article that you're trying to get so it is null.
It should actually be something like:
$article = Article::where('id', 1)->first();
Or alternatively, you could do
$article = Article::find(1);
For a longer explanation on the find method:
https://laravel.com/docs/8.x/eloquent#retrieving-single-models

Related

How to solve Call to undefined method App\model\post\::links() error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I am using laravel paginator and I have this code in my blade
if (post -> count()) For each (post as post) {!!$post->title!!} {!!$post->description!!} #endforeach {{$post->links()}}
Am getting Call to undefined method App\model\post::links() error
When I inspect in the console the issue am getting is Audit usage of navigator.userAgent, navigator.appversion, and navigator.platform.
How can I solve the issue?
You seem to have an error in your foreach. You wrote For each (post as post) but you probably need something more like foreach($posts as $post). Then, to display the pagination links, use {{ $posts->links() }} (note that the links() method is called on the collection variable, not the individual model).
Check out https://laravel.com/docs/9.x/pagination#displaying-pagination-results

Query (complicated) or just store in another column? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to "build" a users' models' likes. For example, if someone liked a users' thread, reply, post, then I could grab how many users liked that users "post" occurences within the application.
Currently, my favorite model takes into account the authenticated users who did the liking.
Would it be easier for me to just add the liked_id to represent the liked user?
I feel like a query would be a little troublesome. What is best practice? Would there be anything wrong with me just adding a liked_id? Would it be just another way to achieve the same thing?
First of all, you should read about Eloquent relationships, but in general you should make your models as below:
Like Model:
public function post(){
$this->belongsTo('Post','post_id');
}
Post Model:
public function likes(){
$this->hasMany('Like','post_id');
}
Then you can use it:
$posts = Post::with('likes')->get();
So this is what I have come up with (on the User Model)
public function liked()
{
return $this->hasMany(Thread::class)->whereHas('favorites', function ($q) { $q->where('threads.user_id', $this->id); });
}
However this covers on the threads. If I want to cover the replies, various other types of comments, it seems an individual query on each of these "tables" would be necessary.
I'm not sure I want to pursue this route, as it would be tons of queries to get the number of likes the user likes... I think a column would work better.
EDIT: Nevermind, this is not really functional

Laravel is there any other way to display data from controller to blade? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Suppose I only selected one specific row from the database,
and returned that result to view.blade.php, now I want to display that single row of data. How would I do it without using #foreach is there any other function in laravel that i can use?
you can use first() to get an object instead of collection. so you need not to loop through to get object property.
controller
public function functionName($parameter)
{
$value = Model::where('field_name', $parameter)->first();
return view('view-name', compact('value'));
}
view blade
{{ $value->property }}
Suppose you have a record stored in $data variable now
you can simply display it on your view template like
`echo $data->image; //without using any #foreach loop`

ASP.NET BOILERPLATE: javascript to call application service [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
ASP.NET BOILERPLATE: I have a table Asset for which I have created AssetApplicationService with base class CRUDAsync; I have wired up AssetController, & appropriate DTO's, I am able to retrieve data and show on the list as similar to user list.
when I click on Create New Asset,I am able to bring the modal dailog as well until now it is all good, I have copied the user\index.js and tailored to call my asset application service from javascript...this is where i am struck
var _assetService = abp.services.app.asset; //line1
var _$modal = $('#AssetCreateModal');
var _$form = _$modal.find('form');
line 1 returns me undefined when I debug through alert(_assetService), not sure why...where as abp.services.app.user, role works fine.
AssetApplicationService must be implemented by IApplicationService.
public interface IAssetApplicationService : IApplicationService
{
}
public class AssetApplicationService : IAssetApplicationService
{
}
Note: I've replied your previous question. Please mark the answer as solution if that worked for you.

How to tweak FOSUserBundle for an AJAX-only use? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm using Symfony on my backend, accessed only by a JS frontend with AJAX requests.
The issue I'm having is that the return codes sent by the FOSUserBundle does not coincide with what I'm expecting for 'user management' actions.
For instance, when for instance a user tries to register on the frontend :
What I get:
Registration success --> Redirection to '.../confirmed'
Registration failure --> 200 (re-displaying the form page)
What I would like to have:
Registration success --> 202 Added
Registration failure --> 500 or else
Do you know how to archieve that, especially given that, in FOSUserBundle, there are no events to hook on to for failures?
You can modify the file RegistrationController.php in the path /vendor/friendsofsymfony/user-bundle/Controller/
In this file, you will find a method called registerAction. Simply add the following code in your form validation part.
if ($form->isValid()) {
$event = new FormEvent($form, $request);
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
$userManager->updateUser($user);
if (null === $response = $event->getResponse()) {
$url = $this->generateUrl('fos_user_registration_confirmed');
$response = new RedirectResponse($url);
}
$dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
return $response;
}
else
{
return new JsonResponse(array('status' => 'failure'), 500);
}
Keep in mind this is a simple case of invalid form validation that I have provided. You can do other type of modifications in this part.
After a few analysis, I decided to rewrite entirely what I needed from scratch, since it was a very small subset of FOSUserBundle.
This solves the problem, allowing to return specific responses with the desired HTTP codes, at any stage of the processes.

Resources