is forelse remove in laravel 4.2.4? - laravel-4

I was trying to use the #forelse in laravel but it give me this error
Undefined variable: data
is #forelse remove from laravel 4.2.4? because that's the version that i am using. This is my code
in my view
#forelse ($result as $data)
<tr><td> $data->name </td></tr>
#empty
<tr><td>No name match</td></tr>
#endforelse
in my controller
$result = User::find(1)->get();
return View::make('view')->with('result', $result);

I don't think #forelse has been around since Laravel 3. I could be wrong. I know it was removed at some point though. You need to use a standard #if and #foreach now.
#if (empty($result))
<tr><td>No name match</td></tr>
#else
#foreach ($result as $data)
<tr><td> $data->name </td></tr>
#endforeach
#endif
UPDATE
As pointed out by Antonio it was brought back and is available in v4.2.7+, so you'll need to update if you want it.

I found the same thing. In the laracast video on Laravel 5 it is mentioned. Although right after he explains that a better practice is using the if else with the count function:
#if (count($result))
has result
#ifelse
has no result
#endif

Related

How to iterate nested data in laravel blade view?

I have below the draft implementation. The spec is to show the parent information and children-grandchildren information.
#foreach ($parent as $children)
<x-icons.chevron-right>
<h1>{{ $parent->name }}</h1>
#if($person->has('children'))
// go back at the top for nested for each
#endif
#endforeach
One solution that you may recommend is to create an iterative function. But my problem for that, it does not completely render my component-icon chevron-right.
#php
function showHTML($person) {
$html = '';
foreach($person as $children) {
$html .= `
<x-icons.chevron-right>
<h1>$person->name</h1>
`;
if ($person->has('children')) {
$html .= showHTML($person->children);
}
}
return $html;
}
#endphp
{!! showHTML($person) !!}
Just wondering if you guys have other solution for this to show nested with a component icon? I would appreciate any answer.
I encountered the same thing, and it did not take me long to figure out that we can re-iterate a component.
In my case, I just created a root component and re-iterated it if it had children.
So I don't need to loop it any longer.
So my usage would like this below:
<x-parent person="$person"></x-parent>
And my root component parent.blade.php. So If there is person has children I just re-iterate the component and pass his children as a prop:
#props(['person'])
<x-icons.chevron-right>
<h1>{{ $person->name }}</h1>
#if($person->has('children'))
<x-parent person="$person->children"></x-parent>
#endif
You can try => value on your foreach, that will fetch data array value from backend.
Or can you show your code on backend (controller), that can easy to help you
#foreach ($parent as $children => $value)
<x-icons.chevron-right>
<h1>{{ $parent->name }}</h1>
<h3>{{ $value->data }}<h3>
#endforeach

Handling errors when variable is null - Laravel

I'm querying a model in a method in my controller to get all messages.
public function index(){
$messages = Message::where('sender_id', Auth::user()->id)->orWhere('recipient_id', Auth::user()->id)->get();
return view('/pages/message/index', compact('messages'));
}
If the model is null and has now entries, I get an error of 'can get method of non object or something like that.
What's the best way to handle errors like this. Ideallly in the controller
#if($messages)
//Codes
#endif
if collection in loop. Forelse statement is cool for that.
#forelse ($messages as $message)
<li>{{ $message->content }}</li>
#empty
<p>There is no messages</p>
#endforelse
in your view
#if(count($messages)>0)
//your code
#endif
or
#if(!empty($messages))
//your code
#endif

How to set null if relations doesn't exist?

In Model I have:
public function publisher()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
In template blade I try to show data User:
#foreach($announcement->offers as $key => $item)
<img src="{{url($item->publisher->photo)}}">
#endforeach
The Problem is that if there is no data in the User table, the program crashes because I can not get the property $item->publisher->photo.
How to fix it?
You can do something like this:
#if (empty($item->publisher))
It's empty
#else
<img src="{{ url($item->publisher->photo) }}">
#endif
Working on memory here, but you could use #forelse/ #empty. These directives can be used if the object your'e iterating over is empty:
#forelse($announcement->offers as $key => $item)
<img src="{{url($item->publisher->photo)}}">
#empty
//default image or message?
#endforelse
https://laravel.com/docs/5.4/blade#loops

What is the best way to delete a record and then return to same view with slug and data?

I have an app that has articles to which comments can be attached. I now want to give the user the function to delete a comment. What I am struggling with is then returning the user to the same view after the delete.
I could do this in js but am trying to see if I can avoid it.
The article link is like this myapp/articles/1. The slug (1) being the article id. I pass along with this all comments with the same article id.
In my article view I have;
#if (count($comments) > 1)
#foreach ($comments as $comment)
<p><i class="fa fa-trash" aria-hidden="true"></i> {{ $comment['details'] }}</p>
#endforeach
#endif
In my controller I have;
public function deleteComment($id)
{
$comment= Comment::where('id', $id)->first();
$comment->delete();
return view ('myapp/articles/'); //This is where I am puzzled. How do I return user to myapp/articles/1?
}
Suggestion?
Thanks!

Laravel 5.1 - compare Carbon dates

Here I need to check does auction for article in my app is live so I write in Article model:
public function scopeCloseauction($query){
$query->where(Carbon::parse('to')->subDays('auction_end'),'>',Carbon::now());
}
and view I have:
#if ($article->Closeauction())
<td>
auction is live
</td>
#else
<td>
auction is closed
</td>
#endif
but I have a problem becouse I got error:
UPDATE:
I also try:
in model to add function:
public function isLive($to,$auction_end) {
$first = Carbon::create($to).subDays($auction_end);
$second = Carbon::now();
return ($first->lte($second));
}
and in view:
#if ($article->isLive($article->to,$article->auction_end))
<td>
live
</td>
#else
<td>
closed
</td>
#endif
but now give me this error:
ErrorException in Carbon.php line 425: Unexpected data found.
Unexpected data found. Unexpected data found. Trailing data (View:
C:\wamp\www\project\resources\views\articles\index.blade.php)
You can do something add such function into your Article model:
public function isLive()
{
$first = Carbon::parse($this->to)->subDays($this->auction_end);
$second = Carbon::now();
return $first->lte($second);
}
and now in your view you can use:
#if ($article->isLive())
<td>
live
</td>
#else
<td>
closed
</td>
#endif
I think your problem is here : Carbon::parse('to'). What do you want to do here ? the Carbon::parse method try to convert a string date into a Carbon date. See the doc. I dont think that Carbon can parse the string 'to'. If you want to check the diff between dates, you should have a look to the appropriated methods like diffInDays.

Resources