I'm using Laravel 5.2 and returning an array result set to my view by using the following
return view('home')->with('devices', $devices)
I've attempted to loop through my array data by using the following in blade
#foreach($devices as $device)
{{ $device[name] }} has
{{ $device[views] }}
#endforeach
Using $device[name] throws Use of undefined constant name - assumed 'name'
I've also tried looping through the result like this
#foreach($devices as $device)
{{ $device->name }} has
{{ $device->views }}
#endforeach
You are sending it like constant not as string. Replace it like this:
#foreach($devices as $device)
{{ $device['name'] }}
{{ $device['views'] }}
#endforeach
Related
I try to ech the object of a laravel many-to-many relation in blade:
When I try to find the created_at with $product->id=47 and echo it out like so:
{{ $inquiry->products->find($product->id)->created_at }}
I get the error: "Trying to get property 'created_at' of non-object..."
When I remove the $product->id and change it to "47" everything works find:
{{ $inquiry->products->find(47)->created_at }}
But I need to query with the variable $product->id.
Any ideas what's wrong here?
Here is a longer part of my code:
#foreach ($upgrades as $upgrade)
{{ $upgrade->id }} //Echos exactly as 47
//List all upgrades
<select>
#if($inquiry->products->where('id', $upgrade->id)->count() == 1)
<option> {{ $inquiry->products->find($upgrade->id)->pivot->duration }}
#endif
</select>
#endforeach
I have a dataset like below inside a cell named rolls.
[
"142650",
"142651",
"142603",
"142604"
]
I have an array named $rooms, where each room has cell called rolls.
#foreach($rooms as $room)
{{ $room->rolls }}
#endforeach
The code above displaying data like below in view
["142650", "142651", "142603", "142604", "142605"]
But I want to display like below...
142650, 142651, 142603, 142604, 142605
I have tried this
#foreach($rooms as $room)
#foreach($room->rolls as $roll)
{{ $roll }}
#endforeach
#endforeach
But getting error like below
Invalid argument supplied for foreach()
I think the cleanest solution, without changing controller is
#foreach($rooms as $room)
<h1>{{ $room->name }}</h1>
#foreach(json_decode($room->rolls) as $roll)
{{ $roll }}
#endforeach
#endforeach
Try not to use #php or <?php ?> in your blade templates
This is the solution I have found, I have not changed anything in my controller, this is what I have done in the view.
#foreach($rooms as $room)
<h1>{{ $room->name }}</h1>
<?php $rolls = json_decode($room->rolls); ?>
#foreach($rolls as $roll)
{{ $roll }}#if(!$loop->last), #endif
#endforeach
#endforeach
When I try to use $student->links() I see this error :
Facade\Ignition\Exceptions\ViewException
Call to undefined method App\Student::links()
I checked the controller, model etc but all of them seem OK... How can I fix this?
(I tried this code both on my Macbook and VPS -CentOS7- but same problem occurs)
That part of my view looks like this:
</tr>
#endforeach
</tbody>
</table>
{{ $student->links() }}
</div>
#endsection
Change
{{ $student->links() }}
to
{{ $students->links() }}
(use plural form).
You need to paginate in your backend code. $students = App\Student::paginate(15);
And then you can access the links()
<div class="container">
#foreach ($students as $student)
{{ $student->name }}
#endforeach
</div>
{{ $students->links() }}
I am fetching records with pagination. The first page is working fine but when I click on page number 2 then it return following error
Undefined offset: 0
I am trying following script in controller
$Jobs = Jobs::orderBy('job_id', 'asc')->paginate(5);
return view('veteran.job-posting.index',['Jobs'=>$Jobs]);
In the blade following code I am trying
#if($Jobs)
#foreach($Jobs as $Job)
{{ ucfirst(trans($Job->job_title)) }}
#endforeach
#endif
For pagination I am using following script
<div class="pagination-test">
{{ $Jobs->links() }}
</div>
Please guide me why it does returning error when I click on page number 2. I would like to appreciate
After Request For More Detail from #pseudoanime
For more detail about error
2/2) ErrorException
Undefined offset: 0 (View: E:\xampp\htdocs\project\project-sub-root\resources\views\index.blade.php)
Try adding this outside of the loop
{{ $Jobs->links() }}
Can you try to add Pagination like this.
#if(count($Jobs) > 0)
#foreach($Jobs as $Job)
{{ ucfirst(trans($Job->job_title)) }}
#endforeach
{{ $Jobs->links() }}
#endif
With your CSS class
#if(count($Jobs) > 0)
#foreach($Jobs as $Job)
{{ ucfirst(trans($Job->job_title)) }}
#endforeach
<div class="pagination-test">
{{ $Jobs->links() }}
</div>
#endif
Edited Update
Try With With in controller
return view('veteran.job-posting.index')->with('Jobs',$Jobs);
Update your controller's function return and use compact function for to pass jobs to view.
$jobs = Job::orderBy("id", "asc")->paginate(5);
return view("jobs", compact("jobs"));
You need to add this for pagination in blade.
<ul class="pagination" >
{!! $Jobs->render() !!}
</ul>
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