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
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
Please help me. The website works fine in the local host, but it is having error in the C panel. How to solve?
the path is:
app/config/app.php
you can use Process Component of Symfony that is already in Laravel
http://symfony.com/doc/current/components/process.html
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
inside your elcdetails use
$macAddr = new Process('getmac');
$macAddr->run();
// executes after the command finishes
if (!$macAddr->isSuccessful()) {
throw new ProcessFailedException($macAddr;
}
//or whatever you want to do with the output.
echo $macAddr->getOutput();
note that, if you are trying to get the client mac address, short answer is you cannot.
From your image, I'm supposing you defined the public function exec() function somewhere above or below in your controller. You cannot reference to it that way since its a method in your controller class.
So change it to this instead. $macAddr = $this->exec('getmac');
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 months ago.
Improve this question
I added apiResource routes in my api.php file. Although it shows all the resourceful routes for product/category, it is not showing the routes for PUT and DELETE methods in the case of products routes.
Showing 404 not found error.
Any help?
You can solve the missing parameter names using the parameters method.
Route::prefix('products')->group(function() {
Route::apiResource('/', \App\Http\Controllers\ProductController::class)
->parameters(['' => 'product']);
Route::apiResource('/categories', \App\Http\Controllers\CategoryController::class);
});
That should give you the following:
Note that it is a convention to make your resources plural rather than singular.
You can accomplish a lot using scoped nested resources and shallow nesting:
Route::apiResource('products', ProductController::class);
Route::apiResource('products.categories', ProductCategoryController::class);
Route::apiResource('products', ProductController::class);
Route::apiResource('products.categories', ProductCategoryController::class)->shallow();
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
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`
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.