So, I'm going to moving on from PHP native to Laravel framework.
See the code below, it's must be error, but this would tell you what I want. I'm confused to the IF statement in Laravel based my database.
<td>
{{
#if($book->status==1)
echo 'Available';
#elseif($book->status==2)
echo 'Lost';
#else
echo 'Not Available';
#endif
}}
</td>
Is that possible to print out the name based on database, example: if the value is 1, then I'll print out 'Lost' on my website? Thank you.
<td>
#if ($book->status == 1)
Available
#elseif ($book->status == 2)
Lost
#else
Not Available
#endif
</td>
A better place for this would be in the book model itself:
public function getAvailabilityAttribute()
{
if ($this->status == 1) return 'Available';
if ($this->status == 2) return 'Lost';
return 'Not Available';
}
Then you could use it directly in your view:
<td>{{ $book->availability }}</td>
Use like this:
<td>
#if($book->status==1)
{{ 'Available' }}
#elseif($book->status==2)
{{ 'Lost' }}
#else
{{ 'Not Available' }}
#endif
</td>
Related
In a laravel blade in third line of this code I want to check if parent_id exists in id column or not
please help me!
I'm using laravel 9
#if ($category->parent_id == 0)
no parent
#if ($category->parent_id)
no parent
#else
{{ $category->parent->name }}
#endif
I corrected it this way:
#elseif (empty($category->parent))
Using exists() function for parent()
Not that exists function works just with single relations (belongsTo, hasOne)
// This will run SQL query // returns boolean
$category->parent()->exists(); // Don't forget parentheses for parent()
If you want to save performance and not calling sql query
count($category->parent); // returns 0 if not exist
Balde:
you can use the empty() to check if empty.
#if ($category->parent == 0)
no parent
#elseif (empty($category->parent))
<p>no parent</p>
#else
{{ $category->parent->name }}
#endif
or ?? operator
{{ $category->parent_id ?? 'no parent' }}
You can use the is empty in twig as below:
{% if category is empty %}
<p> No parent </p>
{% endif %}
You can try by using isset() function
#if ($category->parent_id == 0)
no parent
#if (!isset($category->parent_id))
no parent
#else
{{ $category->parent->name }}
#endif
I want to pass the parameter $questions to view, but it gives the following error:
ErrorException (E_ERROR)
Undefined variable: questions (View: C:\Users\Krishan\Documents\GitHub\GroupProject\lcurve\resources\views\quizz\questions\index.blade.php)
This is my controller index function part:
public function index()
{
$questions = Question::all();
return view('quizz/questions.index', compact('questions'));
}
This is a part Of my view:
<tbody>
#if (count($questions_options) > 0)
#foreach ($questions_options as $questions_option)
<tr data-entry-id="{{ $questions_option->id }}">
<td></td>
<td>{{ $questions_option->question->question_text or '' }}</td>
<td>{{ $questions_option->option }}</td>
<td>{{ $questions_option->correct == 1 ? 'Yes' : 'No' }}</td>
<td>
View-->
<!--Edit-->
{!! Form::open(array(
'style' => 'display: inline-block;',
'method' => 'DELETE',
'onsubmit' => "return confirm('".trans("quickadmin.are_you_sure")."');",
'route' => ['questions_options.destroy', $questions_option->id])) !!}
{!! Form::submit(trans('quickadmin.delete'), array('class' => 'btn btn-xs btn-danger')) !!}
{!! Form::close() !!}
</td>
</tr>
#endforeach
#else
<tr>
<td colspan="5">no_entries_in_table</td>
</tr>
#endif
</tbody>
enter image description here
where is questions_options coming from? You are passing questions. So your for loop should be
#if (count($questions) > 0)
#foreach ($questions as $question)
//rest of your code
#endforeach
#endif
and your return view part can be return view(quizz.questions.index, compact('questions'))
Firstly, the error message you have mention should be shown. The error message should be:
Undefined variable: questions_options (View:C:\Users\Krishan\Do........
Because you are passing questions to view but you are accessing question_options in view. So, it should say question_options in undefined in view.
Besides, do you know you can avoid this count check? You can use laravel's forelse tag here a below:
#forelse($questions as $question)
//Your table goes here
#empty
<tr>
<td colspan="5">no_entries_in_table</td>
</tr>
#endforelse
I am trying to order the out but based on two criteria's, the dawn one should come at the top and then the up and last the no active once. I am trying to do it in the view in laravel. this is my code. it is able to sort them based on up or dawn but not on active.
#foreach($notes->sortBy('values') as $notification)
<tbody>
<tr>
<td> {{ $notification->website_url }} </td>
#if($notification->status('health') == 'up')
<td> {{ $notification->status('health') }} <span class="up"></span></td>
#else
<td> {{ $notification->status('health') }} <span class="down"></span></td>
#endif
<td> </td>
<td class="<?php echo $notification->active == 1 ? 'active' : '' ?>">
{{ $notification->active == 1 ? 'Yes' : 'No' }}
</td>
<td>
<a href="{{ route('notifications.edit', [$notification->id]) }}">
<input type="submit" name="showmore" value="show more"/>
</a>
</td>
<td></td>
</tr>
</tbody>
#endforeach
Argument of sortBy() method can be actually a function, so you can use custom value as a sort criteria. Preferably in the Controller.
$notes = $notes->sortBy(function ($note) {
return ($note->active ? '0' : '1') . ($note->status('health') == 'down' ? '0' : '1');
});
And $notes will be sorted by this value in the following order:
00 for down active
01 for up active
10 for down not active
11 for up not active
To be sincere though I'm not really sure if I understand what order you want to achieve. If that is the case just adjust the return value of sorting function.
I have the following relationships
a movie has many episodes
an user has a watchlist
a watchlist has many movies
I get my episodes with eloquent
$latestshows = episode::with('movies') ->where('category', 'tvshow')->take(10) ->get();
I then display it with
#foreach($latestshows as $show)
#if (Auth::guest())
<tr>
#else
<tr class="{{ Auth::user()-> watchlist **...** ? 'alert-danger' : '' }}">
#endif
<td>{{ $show->movie->title }}</td>
<td>{{ $show->number }}</td>
<td>{{ $show->created_at }}</td>
</tr>
#endforeach
How do I check if the logged in used has the show in watchlist? I want to display it with a different color in that case.
Sorry if this is a silly question, I'm just a beginner and experimenting.
You can use in_array or array_diff (Both native PHP functions) to see which movies are the same, or you can use the ->diff() or ->has() methods from the Collection objects that you are dealing with.
<td>
<a ... {{ Auth::user()->watchlist->movies->find($show->movies->id) ? 'alert-danger' : '' }}>
{{ $show->movie->title }}
</a>
</td>
This code gave me the expected result
{{ Auth::user()->watchlist->movies->find($show->movies->id) ? 'alert-danger' : '' }}
Thank you Oscar, you were my inspiration :P
Hi I am trying to check the variable is already set or not using blade version. But the raw php is working but the blade version is not. Any help?
controller:
public function viewRegistrationForm()
{
$usersType = UsersType::all();
return View::make('search')->with('usersType',$usersType);
}
view:
{{ $usersType or '' }}
it shows the error :
Undefined variable: usersType (View: C:\xampp\htdocs\clubhub\app\views\search.blade.php)
{{ $usersType or '' }} is working fine. The problem here is your foreach loop:
#foreach( $usersType as $type )
<input type="checkbox" class='default-checkbox'> <span>{{ $type->type }}</span>
#endforeach
I suggest you put this in an #if():
#if(isset($usersType))
#foreach( $usersType as $type )
<input type="checkbox" class='default-checkbox'> <span>{{ $type->type }}</span>
#endforeach
#endif
You can also use #forelse. Simple and easy.
#forelse ($users as $user)
<li>{{ $user->name }}</li>
#empty
<p>No users</p>
#endforelse
#isset($usersType)
// $usersType is defined and is not null...
#endisset
For a detailed explanation refer documentation:
In addition to the conditional directives already discussed, the #isset and #empty directives may be used as convenient shortcuts for their respective PHP functions
Use ?? , 'or' not supported in updated version.
{{ $usersType or '' }} ❎
{{ $usersType ?? '' }} ✅
Use 3 curly braces if you want to echo
{{{ $usersType or '' }}}
On Controller
$data = ModelName::select('name')->get()->toArray();
return view('viewtemplatename')->with('yourVariableName', $data);
On Blade file
#if(isset($yourVariableName))
//do you work here
#endif
You can use the ternary operator easily:
{{ $usersType ? $usersType : '' }}
#forelse ($users as $user)
<li>{{ $user->name }}</li>
#empty
<p>No users</p>
#endforelse
Use ?? instead or {{ $usersType ?? '' }}
I solved this using the optional() helper. Using the example here it would be:
{{ optional($usersType) }}
A more complicated example would be if, like me, say you are trying to access a property of a null object (ie. $users->type) in a view that is using old() helper.
value="{{ old('type', optional($users)->type }}"
Important to note that the brackets go around the object variable and not the whole thing if trying to access a property of the object.
https://laravel.com/docs/5.8/helpers#method-optional