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
Related
app/http/controller/FirstController.php
public function delhi_property()
{
$sql = DB::table('property')->where('city', 'Delhi')->orWhere('city', 'New Delhi')->get();
return view('index',['results'=>$sql]);
}
resources/views/index.blade.php
<h3>({{ $results->count() }}) Properties</h3>
routes/web.php
Route::get('/','FirstController#delhi_property');
I am new in laravel 5.4 Here, what am I doing I simply run a query as I mention above in my Controller and want to print numbers of rows in my view file but when I check it shows an error i.e.
Undefined variable: result (View: D:\xampp\htdocs\real_estate\resources\views\index.blade.php)
So how can I solve this problem? Please help me.
You are returning the query result as "results", so in the view you need to access it as "$results".
In the error it says undefined variable result.
There is no "s" in it.
So refer to the code line that error returns and check whether variable naming is correct.
In controller:
public function delhi_property()
{
$data = DB::table('property')->where('city', 'Delhi')->orWhere('city', 'New Delhi')->get();
return view('index', compact('data'));
}
In blade file:
<h3>( {{ $data->count() }} ) Properties</h3>
OR
<h3>( {{ count($data) }} ) Properties</h3>
To count all rows in a query is a function for example:
count($results);
I am modifying your query a bit, will achieve the same result efficiently
public function delhi_property()
{
$cityResults = DB::table('property')->whereIn('city', ['Delhi','New Delhi'])->get();
return view('index',compact('cityResults'));
}
In your view you can access the count as following:
<h3>{{ $cityResults->count() }} Properties</h3>
In case you are using this to calculate and display the total count on a Bootstrap badge, try the following directly in the sidebar view:
<span class="badge badge-info right">{{ DB::table('property')->where('city', 'Delhi')->orWhere('city', 'New Delhi')->count() }}</span>
You can use larave's default function to count rows like example given below.
public function delhi_property()
{
$result = DB::table('property')->where('city', 'Delhi')->orWhere('city', 'New Delhi')->get();
$total_result = count($result);
return view('index',compact(['result','total_result']));
}
In view you can print number of rows in variable $result.
Was wondering if someone could check this out and let me know what I'm doing wrong. I'm trying to use paginate on a database query and render it in my view.
Is my DB query incorrect? I was following the docs to create it. Also read that I needed to remove ->get() when using paginate.
This is what is giving the error on my view: {{$item->paginate(4)}}, same happens if I use {{$item->links(4)}}
Everything renders fine if I remove paginate from the query in the controller..?
Here is what I'm working with.
Controller:
public function index()
{
// $news = DB::table('news')->orderBy('id', 'DESC')->paginate(4);
$news = DB::table('news')->select('id','title','description','listing_image','created_at','updated_at')->orderBy('id', 'DESC')->paginate(4);
return view('news.index',compact('news'));
}
View: (Getting error: Call to undefined method stdClass::paginate())
#if ($news->count())
#foreach($news as $item)
...html
#if ($item->listing_image)
...more html
#else
#endif
... more html
#endforeach
{{$item->paginate(4)}}
#endif
change {{$item->paginate(4)}} to {{ $news->links() }}
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
I have a form that a user enters and takes the entry and queries the database and returns to a view. I have managed to get one query working but when trying to work on another query it returns an undefined variable error in the view. See below:
Routes
Route::get('/shopsales', 'shopsalescontroller#index');
Controller
class shopsalescontroller extends Controller
{
public function index()
{
$storeNum = request('storeNum');
$result = shopsales::where('StoreNumber','=',$storeNum)
->get();
return view('shopsales',compact('result'));
}
}
shopsales view
<section>
<center><h1><STRONG>Query Data</STRONG></h1></center>
<ul>
#foreach ($result as $results)
<li>Report Name = {{ $results->ReportName}} | Report ID = {{ $results->ReportID}} | Store Number = {{ $results->StoreNumber}} | Store Name = {{ $results->StoreName}} | Week Number = {{ $results->WeekNumber}} |
Year = {{ $results->Year}} | PerfumeName = {{ $results->PerfumeName}} | Units Sold = {{ $results->UnitsSold}} | Sales = {{ $results->Sales}}
</li>
<br>
#endforeach
</ul>
</section>
I have used the exact code for the query that is working, struggling to understand why this is not.
try this
in App\Route
Route::resource('/shopsales','ShopSalecontroller');
This routes the following actions: index, create, store, show, edit, update and destroy.
add this function to shopsales model
public function scopeNumber($query, $number){
if($number != null){
$query->where('storeNumber','=', "$number");
}
}
in ShopSalesController index:
public function index(Request $request){
$result = shopsales::get()->number($request->storeNumber)->all();
return view('shopsales',compact('result'));
}
Remember to have in the index view a form with the field storeNumber
public function index()
{
$storeNum = request('storeNum');
$result = shopsales::where('StoreNumber','=',$storeNum)
->get();
return view('shopsales',['result'=>compact('result')]);
}
Change your controller code like above.
Also check if $result is not null.
Try something like this
public function storeCheckout(Request $request) {
$storeNum=$request->get('storeNum');
$result = shopsales::where('StoreNumber','=',$storeNum)
->get();
return view('shopsales',compact('result'));
}
Solved, though sorry for wasting your time, some of my views should of had a capital letter in them, fixed that cleared the view cache and now i have the queries.
in action index
change var $result to $results
in view index
#foreach ($results as $result)
#endforeach
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.