I'm trying to retrieve a user's answer. I have this query :
$data = Category::where('id', $id)->with(
[
'questions' => function ($query) {
$query->with(['answers' => function ($query) {
$query->where( 'user_id', auth()->user()->id )->first();
} ])->orderBy('position', 'ASC');
}
]
)->first();
I would like to collect only the answer (a user can only have one answer to a question)
But laravel returns an array "answers"
How to have only the answer, without this array? Thank you !
You can define another relationship called answer and make it a hasOne. This way you only get 1 result, however note that without an order (order by) the result can change from request to request.
Related
Assume the following, I have a:
Course Model:
belongsToMany User Model
belongsToMany Exam Model
HasMany Question Model
HasMany Answers Model
Course Exam Results Table
id
user_id
course_id
exam_id
question_id
answer_id
Is there a better way to get the results without having to directly query the table?
I am currently doing the following:
public function displayResult($user, $course, $exam) {
$course_exam_results = \App\CourseExamResult::where([
'user_id' => $user->id,
'course_id' => $course->id,
'exam_id' => $exam->id
])
->get();
dd($course_exam_results);
}
Would love to do something better like auth()->user()->course(??)->exam(??)->results. I thought about using laravel hasManyThrough but I don't think I can make it work in this case as it only accepts 2 Models while I might have to call 3-5 models at a time.
Thank you in advance.
If you want to use multiple wheres in Laravel Eloquent, you must do like example below:
You must give to where() an array.
Every search has two arguments so it is an array in an array.
$course_exam_results = \App\CourseExamResult::where([
['user_id', auth()->user()->id],
['course_id', $course->id],
['exam_id', $exam->id]
])
->firstOrFail();
I need to OrderBy a column with collection.
I need to orderBy(updated_at, 'desc') all posts which owned by current logged user.
Here is my code :
$posts = auth()->user()->posts->sortByDesc('updated_at');
Here is User model :
class User extends Authenticatable
{
public function posts()
{
return $this->hasMany(Post::class);
}
}
It doesn't return any errors also doesn't sort !
Any helps would be great appreciated.
P.S:
I know I can achieve this with :
$posts = Post::where('user_id', auth()->user()->id)->orderBy('updated_at', 'desc')->get();
But I would like to do the same thing with collections.
So this is how you sort with SQL:
$posts = auth()->user()->posts()->orderBy('updated_at', 'DESC');
And with collections:
$posts = auth()->user()->posts->sortByDesc('updated_at');
I've tested the 2nd one and it works as intended for me.
Documentation: https://laravel.com/docs/6.x/collections#method-sortbydesc
*Its available since Laravel 5.1
#devk is right. What I wrote in the first post is correct.
The problem was in DataTables in the the view.
It needed to add this line to the Datatables options:
"order": [[ 5, 'desc' ]], // 5 is the `updated_at` column (the sixth column in my case)
So this is working fine :
$posts = auth()->user()->posts->sortByDesc('updated_at');
Try adding the following to your User model
public function posts_sortedByDesc(){
return $this->hasMany(Post::class)->sortByDesc('updated_at');
}
Then get the posts by calling posts_sortedByDesc instead of posts
I have the following relationships:
TheEpisodeJob hasOne TheEpisode
TheEpisodeJob hasMany TheJobs
I am successfuly retrieving all TheEpisodesJobs and TheSeriesEpisodes with all the fields in database (including sensitive information) using this command:
$jobs = TheEpisodeJob::with('TheEpisode')->get();
I would like to limit TheEpisode fields shown only for this case (public $hidden will not work)
EDIT
Let's say I need only title and description field from TheEpisode.
How can I achieve that?
As #Buglinjo pointed out you can scope the relationship when eager loading, however, if you're going to be doing this to only select specific columns you must included the related column in the select so that Eloquent knows which Model to attach the related data to.
This should give you what you want:
$jobs = TheEpisodeJob::with(['TheEpisode' => function ($query) {
$query->select('jobID', 'title', 'description');
}])->get();
Furthermore, if you then wanted to to get rid of the jobID as well you could do something like:
$jobs->transform(function ($job) {
$job->TheEpisode->transform(function ($item) {
unset($item->jobID);
return $item;
});
return $job;
});
Hope this helps!
As far as I understood you, you want to limit the results according to some more parameters. If I am right, you should add more queries, like:
->where, ->orwhere, ->select, ->whereNull
Here is the link for more queries. Hope it will help )
I saw an update, so then you need
->pluck('title', 'description');
for more information, go to the link above
You should do like this:
$jobs = TheEpisodeJob::with(['TheEpisode' => function($q){
$q->get(['title', 'description']);
//or
$q->pluck('title', 'description');
}])->get();
Note: pluck is getting as array not as Eloquent Object.
I'm trying to give ability on user to see his orders. How can I query the database.. I'm trying something like this but I got empty page.. I mean nothing from database. May be my query ins't correct.
This is my controller
public function viewOrders() {
$user_order = self::$user->user_id;
$orders = Order::where('user_id', '=', $user_order);
return View::make('site.users.orders', [
'orders' => $orders
]);
}
Am I getting correctly user_id here? I'm not sure...
Update: I have this in my User model
public function orders() {
return $this->hasMany('Order', 'user_id', 'user_id');
}
Ok, so based on your route+button+model do it like this
$orders = self::$user->orders()->orderBy('order_id', 'asc')->get();
return View::make('site.users.orders', [
'orders' => $orders
]);
this should work.. You can remove orderBy clause if you don't need it.
If you have Authentication set properly you can do the following.
public function viewOrders(){
$user = Auth::user();
return view('site.users.orders',[
'orders' => $user->orders
]);
}
When you use the relationship without using the calling parentheses you get a collection of models which are queried if they're not already loaded. This is called lazy loading, if you want to load a relationship before accessing it you can use eager loading. In this case, it is not necessary though.
I am wanting to limit a controller's function's result to only pass certain columns into the view.
It is necessary because it will be used within an API, and so I need the results to be as streamlined as possible.
I have done this successfully with the following function:
public function getIndex()
{
$alerts = Criteria::select('id', 'user_id', 'coordinate_id', 'alert_name')
->with(['coordinate' => function($q){
$q->select('name', 'id');
}])
->get();
}
So it only returns id, user_id and coordinate_id from the criteria table.
However on the function below, I am using a has query (to access a relationship), and thus, using with afterwards to limit the columns, but it's still returning all:
public function getMatches()
{
$matches = Criteria::select('id')
->has('alerts')
->with(['alerts' => function ($q){
$q->select('id', 'headline', 'price_value', 'price_type');
}])
->with('alerts.user.companies')
->get();
}
But, for example, it's still returning the description column, which is in the alert's table. The with query proceeding the has query clearly isn't working (but it's presenting no errors).
Also, the ->with('alerts.user.companies') query, is returning everything within the user's table, which is also unnecessary. How can I return just the companies table data, that's related to the user, who's related to the alert?
Your help would be greatly appreciated.
Depending what you want to achieve, you could use $hidden property to hide columns you don't want to return as json or arrays.
In your Alert model you could do:
protected $hidden = ['description'];
And this way description field won't be returned.
If it's not the way for you (sometimes you want to return description) you could create extra relationships where you limit fields from database.
You could for example create the following relationship:
public function alertsSimple() {
return $this->hasMany('Alert')->select('id', 'headline', 'price_value', 'price_type', 'criteria_id');
}
Also maybe in your select the problem is that you don't use foreign key at all. You could also try with:
$q->select('id', 'headline', 'price_value', 'price_type','criteria_id');
instead of
$q->select('id', 'headline', 'price_value', 'price_type');