Use multiple queries in blade view laravel - php

Pretty new to Laravel and trying to figure out the ins and outs.
If I write one main query that selects everything in my table, can I write individual queries when selecting columns and rows from the table.
Example:
Main Query:
public function allTickets(){
$tickets = DB::table('tickets')->get();
return view('admin.index',compact('tickets'));
In my view:
//THIS WORKS. It gives me a count of all open and closed tickets
{{$tickets->where('status', '=', 'OPEN')->Count()}}
{{$tickets->where('status', '=', 'CLOSED')->Count()}}
However this does not work...
Error: Call to a member function where() on string (View: ...
#foreach ($tickets as $ts)
{{$ts->subject->where('status', '=', 'OPEN')}}
#endforeach
#foreach ($tickets as $ts)
{{$ts->subject->where('status', '=', 'CLOSED')}}
#endforeach
Is there a way to use that ONE main query and show all the subjects of the tickets that are open and closed, or will I need to write multiple queries in my controller to achieve this?

You can do like this with simple solution.
first write one and then another one with if statement
#foreach ($tickets as $ts)
#if($ts->status == 'OPEN')
{{$ts->subject}}
#endif
#endforeach
#foreach ($tickets as $ts)
#if($ts->status == 'CLOSED')
{{ $ts->subject }}
#endif
#endforeach

Related

laravel 8 join query table.column_name does not work in blade or controller

here is my code:
$data = Courses::select('courses.id','courses.standid', 'courses.publisher', 'courses.coverpic', 'courses.course_sts', 'standards.standtitle AS stitle', 'subjects.subtitle AS btitle')
->join('subjects', 'subjects.id', '=', 'courses.subjectid')
->join('standards', 'standards.id', '=', 'courses.standid')
->get();
and blade codes are:
#foreach ($data as $row)
<p>{{ $row->stitle }}</p>
#endforeach
the query works fine when i check dd($data) under attributes, but the stitle and btitle columns never shows inside blade as {{ $row->stitle }} or {{ $row->btitle }}
what am I missing or how to collect the joining table column into the blade file?
cheers
Thanks for response. It was the redirect method that taking me back to history page rather then the post update status.
it was return redirect (the_view_name)->with(vars)
instead of return view (the_view_name)->with(vars)
my first experience to ask a question at stackoverflow and resolved by myself.

how to limit data inside foreach value if there is condition applied?

In my blade template, I have applied foreach loop . And the data from foreach loop is from relation that I want to show. In that single loop I have to show three different type of data by applying IF condition. From my backend i have passed whole collection without limiting or pagination to front. And i want to limit data in my blade template.
blade view
#foreach($ft->menus as $menu)
#if($menu->foodtype_id == $ft->id)
//want to limti data here
// for every food type i want to show 6 data
#endif
#endforeach
controller page
$data['food_type'] = FoodType::with('menus')->get();
if i understood you correctly, you want to show 6 relationship rows for each food type. you have to check for current key in the loop then.
#foreach ($data['food_type'] as $ft)
#foreach ($ft->menus as $key => $menu)
#if ($key < 6)
{{ $menu->attribute }}
#else
#break
#endif
#endforeach
#endforeach
this will show you the first six rows from the relationship. and when it reaches the limit, it will break the foreach loop.
you can query the relationship too.
#foreach ($ft->menus()->limit(6)->get() as $menu)
{{ $menu->attribute }}
#endforeach

Laravel 5.4: Avoid html in the Controllers and DB queries in the blade

I have a problem I can not solve. I have a foreach that prints me an HTML every time it finds value in the database, and it all works.
However, I would like to avoid putting html in the controller.php file.
At the moment I did:
$html_console='';
if($article->id_game > '0'){
$prel_console = \DB::table('info_game')
->where('id_game', '=', $article->id_game)
->get();
foreach($prel_console as $name_console)
{
$name_console_game = \DB::table('console')
->where('id', '=', $name_console->id_console)
->first();
$html_console.='<span class="label">'. $name_console_game->abb_cat.'</span>' ;
}
}
While in the blade:
{!! $html_console !!}
I tried to do this in the blade:
#foreach ($prel_console as $name_console)
<span class="label margin-top-5 font-size-10">{{ $name_console_game->abb_cat }}</span>
#endforeach
If I put the foreach in the blade, how do I deal with the query "name_console_game"
If you have a one to many relation between info_game table which should have a InfoGame model and console table with Console model then your could do something like this:
controller:
public function someMethod()
{
// assuming that you already have an $article object
$infoGame = InfoGame::where('id_game', $article->id_game)->get();
return view('some.view', compact('infoGame'));
}
view location views/some/view/blade.php
#foreach($infoGame->console as $name_console_game)
<span>{{ $name_console_game->abb_cat }}</span>
#endforeach

Laravel 5.3 - HasMany Relationship not working with Join statement

I am trying to retrieve symbols with their comments using hasMany in laravel 5.3
Symbol.php
public function comments() {
return $this->hasMany('App\Comment');
}
Comment.php
public function symbol() {
return $this->belongsTo('App\Symbol');
}
when I run:
$symbols = Symbol::with('comments')->paginate(100);
I get the correct output (lists all symbols with their comments)
#foreach ($symbols as $s)
{{ $s->name }}
#foreach ($s->comments as $c)
{{ $c->body }}
#endforeach
#endforeach
but when I add a join to the statement:
$symbols = Symbol::with('comments')
->join('ranks', 'symbols.id', '=', 'ranks.symbol_id')
->join('prices', 'symbols.id', '=', 'prices.symbol_id')
->paginate(100);
The foreach loop has no comments for every symbol. Any idea why the join would be causing this?
When you are doing joins like this, attributes with the same names will be overwritten if not selected. So select the attributes you need for your code, and nothing else. As shown below.
$symbols = Symbol::with('comments')
->join('ranks', 'symbols.id', '=', 'ranks.symbol_id')
->join('prices', 'symbols.id', '=', 'prices.symbol_id')
->select('symbols.*', 'ranks.importantAttribute', 'prices.importantAttribute')
->paginate(100);
Basicly i think your ids are being overwritten, by the two joins because they also have id fields, i have had a similar problem doing joins, and it breaks relations if the id is overwritten.
And you have to be carefull, all fields who shares names can be overwritten and parsed wrong into the models.

Laravel Fluent Query - property of non-object

I am working on displaying user active jobs and pending interviews on a dashboard. I can get active jobs to work, but when I try to display pending interviews for the jobs, I get the error: "trying to get error of non-object." The error is for the line: $jobInterviews->id
Here is my dashboard view:
#if (! $active)
<p> You don't have any active projects. Click here to post one! </p>
#else
#foreach($active as $job)
<div class="media-body">
display $job name/description
</div>
#if ($jobInterviews->id == $active->id)
<table class="table table-striped">
display $jobInterviews details
</table>
#else
<p></p>
#endif
</div> <!-- .media-body -->
#endforeach
#endif
</div> <!-- .media -->
I have run through a #foreach loop with $active and $jobInterviews separately and both work fine. But when I nest the #if ($jobInterviews->id == $active->id), I get the error "attempting to display property of non object" related to $jobInterviews->id
Here is my controller method:
public function getdashboard()
{
//reading the user information
$arrPageData['user'] = Sentry::getUser();
//reading the job interviews
$arrPageData['jobInterviews'] = JobInterview::readCurrentInterviews($this->userID);
//reading the active jobs
$arrPageData['active'] = Job::activeJobs($this->userID);
return View::make('clients.dashboard', $arrPageData);
}
and finally, here are my query statements:
public static function readCurrentInterviews($jobID){
return DB::table('jobs')
->join('job_interviews', 'job_interviews.job_id', '=', 'jobs.id')
->join('contractors', 'contractors.user_id', '=', 'job_interviews.send_to')
->where('jobs.user_id', '=', $jobID)
->where('job_interviews.status', '=', 'awaiting response')
->orderBy('job_interviews.created_at', 'desc')
->select('jobs.id','jobs.title', 'contractors.user_id', 'contractors.contact_name', 'job_interviews.status', 'jobs.created_at')
->get();
}
public static function activeJobs($contractorId){
return DB::table('jobs')
->where('user_id', '=', $contractorId)
->select('id','title', 'description', 'created_at')
->get();
}
If anyone knows why this non-object error is being thrown, I would really appreciate the help understanding it. Thank you in advance.
Just change following:
#if ($jobInterviews->id == $active->id)
To this:
#if ($jobInterviews->id == $job->id)
Here $active is a an array of models.
Also make sure that $jobInterviews is not the array of models and if it's also an array of models then $jobInterviews->id will not work. probably it's:
foreach($jobInterviews as $jobInterview)
...
foreach($active as $job)
#if ($jobInterview->id == $job->id)
...
#endforeach
#endforeach
In your view the $jobInterviews and $active both are an array of multiple models and using $jobInterviews->id or $active->id will not work, instead you have to select an individual item from them.

Categories