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.
Related
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
I want to show a message in my blade where if my address, name or number is null say this fields are empty please fill them up, if there are filled already, just don't display anything.
Currrently I did like this, but in the blade.php. Previously I had already asked something similar but this time I want to show a message. I tried following the question that I asked but for some reason it keep showing the message "please fill them up" even though my data contain values inside the database.
Redirect to page when value is null in another table laravel
Here is my code:
<?php
$additional_info = DB::table('additional_informations')
->whereNull('address')
->orWhereNull('name')
->orWhereNull('number')
->get();
if( $additional_info->count())
echo "test";
?>
For a route like this :
Route::get('/userAddInfo/{id}/AddInfo','VerificationController#AddInfo');
You can get the user id in the method as parameter then use it in the query like this :
public function AddInfo($id) {
$additional_info = DB::table('additional_informations')
->where('user_id', $id)
->where(function ($query) {
$query->whereNull('EC_address')
->orWhereNull('EC_name')
->orWhereNull('EC_number');
})
->get();
return view('AddVerificationInfo',compact('id','additional_info'));
}
And in the view :
#if($additional_info->count())
"Please fill this up"
#endif
Assuming you are working on the index.blade.php page here how it goes
InformationController - Controller
public function index($id) {
$additional_info = DB::table('additional_informations')
->whereNull('address')
->orWhereNull('name')
->orWhereNull('number')
->where('id', '=', $id)
->first();
return view('index', compact('id','additional_info '));
}
index.blade.php - View
<div>
#if(count($additional_info) > 0)
Here are the rows that has Null values: <br/>
Please go back and fill up the values of
#if($additional_info->EC_name == null)
EC_name
#endif
#if($additional_info->EC_relationship== null)
EC_relationship
#endif
#if($additional_info->EC_address== null)
EC_address
#endif
// and so on. There is a shorter way but I'm not in my laptop right now. But this should work fine
#else
{{ $id }} has no Null data found
#endif
</div>
Tell me if you need more help on this part. Cheers
/* In controller */
$additional_info = DB::table('additional_informations')
->whereNull('address')
->orWhereNull('name')
->orWhereNull('number')
->get()->toArray(); // to convert output into array
/* In view (blade template) */
#if(!empty($additional_info))
echo "<pre>";
print_r($additional_info);
#else
echo "No data found";
#endif
You should move your logic into a controller, then pass a variable like $noAdditionalInfo to the view
Then you can use blade tags:
#if($noAdditionalInfo)
<!-- html goes here -->
#endIf
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
How to view the data from two or more Modal and view the that data just three data with descending to front-end ?
i was try with this code but not success..
class FrontController extends Controller
{
public function index()
{
$lowongans = Lowongan::orderBy('id', 'desc');
$agendas = Agenda::orderBy('id', 'desc')
->take(3)
->get();
return view('index', compact('lowongans','agendas'));
}}
in the index.blade i try to access the data
<div class="post-row1">
#foreach($agendas as $agenda)
{{$agenda->name}}
#endforeach
</div>
<div class="post-row2">
#foreach($lowongans as $lowongan)
{{$lowongan->name}}
#endforeach
</div>
its not success, just Agenda:: can access but not with the Lowongan::
You need to call get().
$lowongans = Lowongan::orderBy('id', 'desc')->get();
Laravel only prepares the query until you actually call methods like get, first, etc.
Hi I'm trying to query three tables from my client controller, a quick overview of my database, users clients projects tasks a user hasMany clients, projects and tasks and these projects and tasks also belongTo a client.
So I'm in the Client Controller and I want to query the logged in users clients projects, however when I try to do this I get thrown an undefined method error:
BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::user()
I'm not sure why this is occurring, I queried the clients projects separately and it works fine but when I add an additional layer it throws me the above error.
I'm a newbie on Laravel 4 so would appreciate some guidance to help rectify the error and help me understand where I'm going wrong.
My code is below:
ClientController.php
public function show($id)
{
$client = Client::find($id);
$client->load(array('projects' => function($query)
{
// With the clients for each project
$query->with('user');
}));
// Create an empty array
$associated = array();
// Loop through client projects
foreach($client->projects as $project):
// Loop through project users
foreach($project->user as $user):
// Check if the user is the same as the logged in user
if($user->id == Auth::user()->id){
// If yes add the $project to the $associated array
array_push($associated, $project);
}
endforeach;
endforeach;
// show the view
return View::make('clients.show')
->with('client', $client);
}
clients/show.blade.php
<?php $clients = $client->projects; ?>
#if (Auth::check())
#if (count($clients) > 0)
#foreach ($clients as $project)
<div class="one-third column">
<div class="projects">
<ul class="data">
<li><label>Project Name: </label><a class="btn btn-small btn-success" href="{{ URL::to('project/' . $project->id.'/show' ) }}"> {{ $project->project_name }}</a></li>
<li><label class="titletoggle">Project Brief <p>(click to toggle)</p></label><p class="brief">{{ $project->project_brief }}</p></li>
</ul>
<ul class='buttonslist'>
<li><button>Edit Project</button></li>
<li><button>Create Task</button></li>
<li><button>View Tasks</button></li>
</ul>
</div>
</div>
#endforeach
#else
<h3>You have no projects click here to create a project</h3>
#endif
#endif
The problem has to do with the way you are eager loading. Specifically this part.
$client->load(array('projects' => function($query)
{
// With the clients for each project
$query->with('user');
}));
The proper way to eager load these nested relationships would be.
$client->load(array(
'projects',
'projects.user',
));
Or more simply.
$client->load('projects.user');
Or you can set up the eager loading during the initial query.
$client = Client::with('projects.user')->find($id);
You also didn't mention that projects belongs to user. This relationship will need to be defined in the Project model.
class Project extends Eloquent {
public function user()
{
return $this->belongsTo('User');
}
}
The lack of this method is probably the cause of the error message. Eloquent will forward calls to undefined methods to it's internal query builder object. The query builder object doesn't have a user() method, so that's why you get that error.