I have a method to get an author and display it on a form. It also retrieves the number of quotes attributed to that author. Being pedantic I do not wish to have "1 quotes" so I wrote the following in my layout:
#extends('layouts.dashboardmaster')
$quotetext = "quotes";
if ( $quotes == 1)
{ $quotetext = "quote"; }
#section('pageheading')
<div class="well">
<h2><i class = "fa fa-quote-left"></i> edit author ({{$author->author}}), ({{ number_format($quotes,0) }} {{}}$quotetext }}) </h2>
</div>
#endsection
but I am now getting an error saying
Undefined variable: quotetext
It is clearly defined and I have also tried replacing {{ with
What am I doing wrong?
needs to be
<?php $quotetext = "quotes";
if ( $quotes == 1)
{ $quotetext = "quote"; }
?>
Your view needs to look like this:
#extends('layouts.dashboardmaster')
<?php $quotetext = "quotes"; ?>
#if($quotetext == 1)
<?php $quotetext = "quote"; ?>
#endif
#section('pageheading')
<div class="well">
<h2><i class = "fa fa-quote-left"></i> edit author ({{$author->author}}), ({{ number_format($quotes,0) }} {{$quotetext }}) </h2>
</div>
#endsection
you didn't put php codes in <?php tag.
put them in php tag and everything will be fine
<?php
$quotetext = "quotes";
if ( $quotes == 1){
$quotetext = "quote";
}
?>
from the docs https://laravel.com/docs/5.2/blade
{{ isset($name) ? $name : 'Default' }}
so
{{ isset($quotetext) ?: "quotes" }}
#if ($quotes == 1)
{{ $quotetext = "quote"; }}
#endif
but i think u will get an error about $quotes aswell, anyway remember this is blade not normal php
Use localization provided by Laravel. See Laravel Docs for more information.
Just as an example, add this to your language file (for example resources/lang/en/lang.php
return [
'quote' => 'quote|quotes',
]
Then use this in your view:
{{ trans_choice('lang.quote', $count) }}
Related
I have an old database filled with info and now i want to display Category names from that DB and getting this error.
Here is my controller
public function forums(){
$cats = Forum_cats::all();
return view ('lapas.forums.index')->with('cats', $cats);
}
}
here is my view
#if(count($cats >1))
#foreach($cats as $cati)
<div class = "well">
<h3>{{$cati->description}}</hr>
</div>
#endforeach
#else
#endif
and here is screen of DB structure
http://prntscr.com/mg5nk1
Ask for more info if needed!
It looks like your operator is misplaced:
#if(count($cats) > 1)
#foreach($cats as $cati)
<div class = "well">
<h3>{{$cati->description}}</hr>
</div>
#endforeach
#else
#endif
It looks like you're trying to loop through these $cats in a Blade template. You might also try forelse:
#forelse($cats as $cati)
<div class = "well">
<h3>{{$cati->description}}</hr>
</div>
#empty
{{-- Action if there are none --}}
#endforelse
Edit: Docs here: https://laravel.com/docs/5.4/blade#loops
Your if is wrong. Try :
#if($cats->count() > 1)
I am working on a web application using laravel 5.3, I got one problem.
I want to loop through this array.
What I tried is:
Controller:
public function postSearch(Request $request)
{
$categoryid = $request->category_id;
$locationid = $request->location_id;
$category = Category::where('id', $categoryid)->first();
$cities = Location::where('id', $locationid)->pluck('city');
$cityname = $cities[0];
$alllocations = [];
$ratecards = [];
$locations = [];
$father = [];
$i = 0;
$filteredlocations = $category->locations->where('city', $cityname)->all();
foreach($filteredlocations as $location){
$alllocations[$i] = $location->getaddetails;
$ratecards[$i] = $location->ratecard;
$i++;
}
$father[0] = $alllocations;
$father[1] = $ratecards;
return view('ads', compact('father'));
}
View is here:
#foreach($father as $key => $f)
#foreach($f as $key => $arr)
#if (isset($arr->adName))
<div class="post">
{{Html::image($arr->path,'Ad Picture',array('class' => 'ad-image'))}}
<h2 class="post-title">{{ $arr->adName }}</h2>
<p class="description">{{ $arr->description }} </p>
<div class="post-footer">
<span class="categories">
<ul class="cats">
<li class="btn btn-default">Price :
{{ $f[$key]->monthly_rate }}
</li>
<li class="btn btn-default">Status:
#if($arr->status == 1)
<p>Active</p>
#else
<p>Not-Active</p>
#endif
</li>
<li>view details</li>
</ul>
</span>
</div>
</div>
#endif
#endforeach
#endforeach
Problem:
Okay problem is when I try to get {{ $f[$key]->monthly_rate }} which is the 2nd array i.e RateCards I get nothing. I want to get the Rate Card array and from that array mothly_rate which is a field in there.
Thanks
PS : {{ $f[$key]->monthly_rate }} this return nothings in view.
The problem is that $f[$key] is an array of two objects.
That means that the properties youre looking for are inside each of those items, like this:
$f[$key][0]->monthly_rate
$f[$key][1]->monthly_rate
You will have to loop those objects to get their properties values.
My model code
how we can call this function in blade.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class BasicModel extends Model
{
public static function get_product_count($id){
$query = "select COUNT(sub_id) AS count FROM products WHERE products.sub_id = $id";
print_r($query);
return $query->row_array();
}
}
My view.blade.php code
count in foreach loop or show in all category
#foreach ($r as $row)
<li class="grid-item type-rent">
<div class="property-block">
<img src="{{ URL::to('/template/images/background-images/sub-category-images/' .$row->sub_cat_images. '')}}" alt=""> <!-- <span class="images-count"><i class="fa fa-picture-o"></i> 2</span> <span class="badges">Rent</span> -->
<div class="property-info">
<h4>{{ ucwords(substr($row->sub_cat_name, 0, 22)) }}</h4>
<span class="location">NYC</span>
<div class="price"><strong>Items</strong><span>
<!-- start count code from here -->
$data = $this->BasicModel->count {{ ($row->sub_id) }}
echo $data['count'];
</span></div>
</div>
<!-- <div class="property-amenities clearfix"> <span class="area"><strong>5000</strong>Area</span> <span class="baths"><strong>3</strong>Baths</span> <span class="beds"><strong>3</strong>Beds</span> <span class="parking"><strong>1</strong>Parking</span> </div> -->
</div>
</li>
#endforeach
My BasicController Code
public function grid(Request $request, $id)
{
if ($id == 1) {
$r = DB::table('sub_category')->select('*')->where('cat_id', $id)
->where('sub_status', '1')->orderBy('sub_id', 'asc')->get();
$name = DB::table('category')->where('cat_id', $id)->get();
return view('buy-and-sell/grid', compact('r','name','count'));
}
image for your help
image for your help
problem in this image please solve the problem
I think you are closing the PHP tag in the wrong place. Change to this:
<?php print_r($count);
$data = $this->BasicController->count {{ ($row->sub_id) }}
echo $data['count'];//here is the rightplace to close your PHP ?>
</span></div>
All PHP code needs to stay between the <?php and ?>
Looking to your image, apparently, it's printing the $count and then it's printing the plain text , so you have 80$data = ...
If you counted your rows in controller by the code below
$count = DB::table('products')->where('sub_id', $id)->count();
then why wrote so many code in html for counting?
Remove all code form counting form your html and just use this {{ $count }}
I am new to Laravel and I am trying to call the destroy function in the controller passing in two parameters but I get the error: exception missing argument 2 for GradoController::destroy().
I have grades from 1 (first) through 6 (sixth) and each has subjects associated with it. The grades are listed on the page in tabs and by clicking the tab you get a list of the subjects that go with the grade. I put a delete button next to each subject so the user can click delete and detach the subject from the grade.
I am trying to pass both the gradeId and the subjectId to the destroy method in GradoController
My view file index.blade.php:
<html>
<head>
{{HTML::script('assets/js/jquery-1.11.1.js'); }}
{{HTML::script('assets/js/jquery-ui.js'); }}
{{HTML::script('assets/js/jquery-ui.min.js'); }}
{{HTML::style('assets/css/jquery-ui.min.css'); }}
<script>
$(function() {
$( '#tabs' ).tabs();
});
</script>
</head>
<body>
<h1>Lista Materias por Grado</h1>
<?php
$year = date("Y");
$month = date("m");
if($month < 8){
$year = $year - 1;
} ?>
<h2>{{$year}}-{{$year+1}}</h2>
<div id="tabs">
<ul> <?php $i=1; ?>
#foreach($grados as $grado)
<li>Grado:{{ $grado->name_grados}} </li>
<?php $i++; ?>
#endforeach
</ul>
<?php $i = 1; ?>
#foreach($grados as $grado)
<div id="tabs-{{$i}}" >
#if($grado->subject->count())
#foreach($grado->subject as $subject)
<li> {{ $subject->name_subjects }}
{{Form::open(array('method' => 'DELETE', 'route' => array('grados.destroy', $grado->id_grados, $subject->id_subjects))) }}
{{ Form::submit('Delete',array('class' => 'btn btn-danger')) }}
</li>
#endforeach
#else
No hay materias asociadas
#endif
</br></br>
{{ link_to_route('grados.edit', 'Agregar Materias',
array($grado->id_grados), array('class' => 'btn btn-info')) }}
<?php $i++; ?>
</div>
#endforeach
</div>
My GradoController's destroy function:
public function destroy($gradoId, $subjectId)
{
$grado = Grado::find($gradoId);
$year = 2014;
$grado->subject()->detach($subjectId, array('year_grado_subject'=>$year));
return Redirect::route('grados.index');
}
In routes I have:
Route::get('grados/{id_grados}/{id_subjects}/destroy', 'GradoController#destroy');
Route::resource('grados', 'GradoController');
Thanks!
What I can imagine is that the Form uses the action via
Route::resource('grados', 'GradoController');
and the "default DELETE" (see http://laravel.com/docs/4.2/controllers#restful-resource-controllers) only uses one parameter (/grados/{parameter}), so the second parameter gets lost and is not passed on to the Controller action. This is because there is no route set on
Route::get('grados/{id_grados}/{id_subjects}/destroy','GradoController#destroy');
AND it doesn't use Route::delete but rather Route::get.
So changing it to
Route::delete('grados/{id_grados}/{id_subjects}/destroy',['as' => 'grados.destroy', 'uses' => 'GradoController#destroy']);
will hopefully do the trick. Plus I would avoid defining the same routes twice. E.g. you can use this:
Route::resource('grados', 'GradoController', array('except' => array('destroy')));
In working with laravel blade templates, what's the approved way to manage variables in output?
For example, I'm working on a view that shows upcoming chores / tasks for each farmer. The pivot table holds a due_at datetime field for the task, and I'd like to change the class of the item depending on whether it's overdue, done, etc.
#foreach ($farmer->tasks as $task)
#if ($task->pivot->due_at) < date(now))
$style = 'alert alert-danger';
#elseif ($task->pivot->due_at) > date(now))
$style = 'alert alert-success';
#else
$style = '';
#endif
<div class="list-group-item {{ $style }}">{{$task->name}} <span class="glyphicon glyphicon-calendar"> {{ $task->pivot->due_at }}</span> <span class="glyphicon glyphicon-pencil"></span><span class="glyphicon glyphicon-trash"></span></div>
#endforeach
This example throws an error: Undefined variable: style (View: /home/vagrant/Code/app/views/farmers/show.blade.php)
I don't see an obvious way to do simple code blocks to set variables like I'd do in a "normal" PHP view to define the class to apply to the task item by doing some basic calculations on the due_at value.
Should this logic be moved to a helper function or something?
Assume due_at is a timestamp.
#foreach ($farmer->tasks as $task)
#if (Carbon::parse($task->pivot->due_at) < Carbon::now())
<?php $style = 'alert alert-danger'; ?>
#elseif (Carbon::parse($task->pivot->due_at) > Carbon::now())
<?php $style = 'alert alert-success'; ?>
#else
<?php $style = ''; ?>
#endif
<div class="list-group-item {{ $style }}">{{$task->name}} <span class="glyphicon glyphicon-calendar"> {{ $task->pivot->due_at }}</span> <span class="glyphicon glyphicon-pencil"></span><span class="glyphicon glyphicon-trash"></span></div>
#endforeach
#Anam Your answer works, but I will use following method.
#user101289 Assuming that you have default layout and it yields content section. So to declare and use variables I would suggest you use vars section in your inner template file and declare all your variables all at once on the top. And then use it. Since we will not yield the section vars, it will not going to print it.
This will help you to keep track of variables used and its standard method to declare all variables on top and use in rest of the program:
#extends('layouts.default') /* Your default layout template file. */
#section("vars")
{{ $yourVar = 'Your value' }}
#endsection
#section("content") /* The content section which we will print */
// Your other HTML and FORM code and you can use variables defined in **vars** section
#endsection
#stop