Laravel 5.4, user cannot see dashboard if they don't own anything - laravel

The problem is, is that when I sign a new user up (in my application a 'charity owner'), they are redirected to their dashboard.
On the dashboard, displays the all the charities that the user owns and any donations made to them in another table.
However, because the user has not added their charity to the database yet I am getting undefined variables.
#forelse($ownedCharities as $owned)
<td> {{ $owned->name }} </td>
<td> Not Available </td>
<td> <p id="desc"> {{ $owned->description }} </p> <p style="color: #000;" class="seeMore"> See more... </p> </td>
<a href="#"> <button class="btn btn-danger btnPopover"
data-toggle="popover" data-placement="top"> Delete </button> </a> </td>
<td> <button class="btn btn-warning"> Edit </button> </td>
</tr>
#empty
<p> No charities owned </p>
#endforelse
I am using Blade's #forelse

You can use php functions in blade like to see if they are or not defined or has some value
#isset($records)
// $records is defined and is not null
#endisset
#empty($records)
// $records is "empty"...
#endempty

You can use Null coalescing operator which is introduces in PHP 7
$records ?? ""

Related

Laravel - Trying to get property 'name' of non-object

Im making a view that shows the users and the companies they work for.
In my User model i return the view like this:
return view('users.index', ['users' => $model->all()]);
The model $model->all() is my User model.
Then in my index.blade file i loop through the data like this:
#foreach ($users as $user)
<tr>
<td>{{ $user->name }}</td>
<td>
{{ $user->email }}
</td>
<td>{{ $user->created_at->format('d/m/Y H:i') }}</td>
<td>{{ $user->company->name }}</td>
<td class="text-right">
<div class="dropdown">
<a class="btn btn-sm btn-icon-only text-light" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fas fa-ellipsis-v"></i>
</a>
<div class="dropdown-menu dropdown-menu-right dropdown-menu-arrow">
#if ($user->id != auth()->id())
<form action="{{ route('user.destroy', $user) }}" method="post">
#csrf
#method('delete')
<a class="dropdown-item" href="{{ route('user.edit', $user) }}">{{ __('Edit') }}</a>
<button type="button" class="dropdown-item" onclick="confirm('{{ __("Are you sure you want to delete this user?") }}') ? this.parentElement.submit() : ''">
{{ __('Delete') }}
</button>
</form>
#else
<a class="dropdown-item" href="{{ route('profile.edit') }}">{{ __('Edit') }}</a>
#endif
</div>
</div>
</td>
</tr>
#endforeach
But when i try to show $user->company->name i get the following error:
Trying to get property 'name' of non-object
I tried debugging by using dd($user->company) and it showed all the company information of the company connected to the user. Then i tried dd($user->company->name) and it showed the company name.
I dont understand why i cant just show $user-company->name without getting the error but when i dd($user->company->name) it shows the company name.
Can someone point me in the right direction.
It probably shows an error because one of your users doesn't have a company. The first one of your loop has one, that's why when you dd($user->company), you see a company. But it doesn't mean the second or third has one. You can do a if statement in order to catch any null company. Something like
if($user->company == null){
dd($user);
}
Alternatively, you can just do dd($users->pluck('company','id'));, which will show you a collection with the user id as key and related company (or null) as value.
You can use optional() helper to solve this. There are users who don't have companies. SO you can display them like this optional($user->company)->name and you will not get an error if a user does not have a company
If you are trying to get relationship object value might be chances are there you will get this type of error. Due to there is some user who doesn't have company detail.
Just try below code.
{{ #$user->company->name }}

RobotFramework : How to get information using 'following-sibling'

I want to recover the value of the account status "RF-Account" as shown in the picture.
For this, I target an xpath to retrieve the value just to the right of it with this code in RobotFramework :
${initialStatus}= Get Value //td[text()='${testAccount}']/following-sibling::span[#style=""]
Here is the html code of the information to retrieve:
<span class="text-bold ng-binding text-green" ng-class="{'text-green' : tenant.status == 'ACTIVE', 'text-muted' : tenant.status == 'INACTIVE'}" style="">ACTIVE</span>
And the html snippet that contains whole table :
<tbody ng-if="!isEmpty" class="body-table ng-scope" style="height: 193px;">
<!-- ngRepeat: tenant in tenants track by $index --><tr ng-repeat="tenant in tenants track by $index" class="ng-scope" style="">
<td class="ng-binding">RF-Accoun</td>
<td>
<span class="text-bold ng-binding text-green" ng-class="{'text-green' : tenant.status == 'ACTIVE', 'text-muted' : tenant.status == 'INACTIVE'}">ACTIVE</span>
</td>
<td style="width:118px">
<div class="pull-right">
<a name="editBtn" type="submit" class="btn-icon" ng-click="openCreateEditModal(tenant)">
<span class="icon-i-edit fs"></span>
</a>
<a name="deleteBtn" type="submit" class="btn-icon" ng-click="deleteTenant(tenant)">
<span class="icon-i-trash fs"></span>
</a>
</div>
</td>
</tr><!-- end ngRepeat: tenant in tenants track by $index --><tr ng-repeat="tenant in tenants track by $index" class="ng-scope">
<td class="ng-binding">RF-Account</td>
<td>
<span class="text-bold ng-binding text-green" ng-class="{'text-green' : tenant.status == 'ACTIVE', 'text-muted' : tenant.status == 'INACTIVE'}">ACTIVE</span>
</td>
<td style="width:118px">
<div class="pull-right">
<a name="editBtn" type="submit" class="btn-icon" ng-click="openCreateEditModal(tenant)">
<span class="icon-i-edit fs"></span>
</a>
<a name="deleteBtn" type="submit" class="btn-icon" ng-click="deleteTenant(tenant)">
<span class="icon-i-trash fs"></span>
</a>
</div>
</td>
</tr><!-- end ngRepeat: tenant in tenants track by $index --><tr ng-repeat="tenant in tenants track by $index" class="ng-scope">
<td class="ng-binding">RF-Account-bis</td>
<td>
<span class="text-bold ng-binding text-green" ng-class="{'text-green' : tenant.status == 'ACTIVE', 'text-muted' : tenant.status == 'INACTIVE'}">ACTIVE</span>
</td>
<td style="width:118px">
<div class="pull-right">
<a name="editBtn" type="submit" class="btn-icon" ng-click="openCreateEditModal(tenant)">
<span class="icon-i-edit fs"></span>
</a>
<a name="deleteBtn" type="submit" class="btn-icon" ng-click="deleteTenant(tenant)">
<span class="icon-i-trash fs"></span>
</a>
</div>
</td>
</tr><!-- end ngRepeat: tenant in tenants track by $index -->
</tbody>
It doesn't work. Could you, please, correct me for it to work ?
First, you should use keyword Get Text, because Get Value return value attribute of element. In your case your element do not have this attribute, so I assume that you want text.
Second, span elements from html snippet containing whole table differ from span element that your provided separately. They do not contain style attribute.
Finally the most probable solution to your problem is:
Get Text //td[text()='${testAccount}']/following-sibling::td/span

laravel delete form is not working

#extends('layouts.app')
#section('content')
<div class="row">
<div style="float:left;" class="col-md-8">
<h3>Currency</h3>
</div>
<div style="float:right;" class="col-md-4">
<i class="fa fa-edit" style="color:white;"></i>Add New
<input type="text" id="search" name="search" placeholder="search" class="form-control-sm col-md-8 ml-1" style="float:right;"><br><br>
</div>
</div>
#if(count($currencies) > 0 )
<table class="table table-striped table-sm" >
<thead>
<tr><th scope="col">ID</th><th scope="col">Currency</th><th scope="col">Country</th><th scope="col" colspan="2" style="text-align:right">Actions</th></tr>
</thead>
#foreach($currencies as $currency)
<tr><td scope="row">{{$currency->id}}</td>
<td>{{$currency->title}}</td>
<td>{{$currency->country}}</td>
{!!Form::open(['action'=>['CurrencyController#destroy', $currency->id], 'method'=>'POST'])!!}
{{Form::hidden('_method','DELETE')}}
#csrf
<td><button type="submit" class="btn btn-danger btn-sm" style="float:right"><i class="fa fa-remove" style="color:white;"></i>Delete</button>
</i>Edit</td></tr>
{!!Form::close() !!}
#endforeach
</table>
{{$currencies->links()}}
#else
<p> No Data Available </p>
#endif
#endsection
Hi all, The delete form is not submitting in this code and I tried so many thing but I could not figure out the issue. Please help me with this.
It looks to me that your html is wrong, you open, close your form and place #csrf outside your td tag while submit button is inside it
Try to move your form definition inside td tag like:
<td>
{!!Form::open(['action'=>['CurrencyController#destroy', $currency->id], 'method'=>'POST'])!!}
{{Form::hidden('_method','DELETE')}}
#csrf
<button type="submit" class="btn btn-danger btn-sm" style="float:right"><i class="fa fa-remove" style="color:white;"></i>Delete</button>
</i>Edit
{!!Form::close() !!}
</td>
Try changing the method in the actual form tag to DELETE? By default if you are using something like route::resource in your routes, it will automatically assign the DELETE method to the route, not the POST.
I can see you're specifying the delete method in a hidden element anyway, but its worth a try.
{!!Form::open(['action'=>['CurrencyController#destroy', $currency->id], 'method'=>'POST'])!!}
If you're never quite sure, run php artisan route:list in your terminal inside your project root directory, and it will give you a full list of the routes and methods for your application.

if condition in laravel according to database columns

I have a day column in a database and I want to display the other columns according to the day.
#foreach($specialism as $item)
#if($item->day ==="saturday")
<td>
<a class="btn btn-primary btn-lg" href="{{url('button',['id'=>$item->id])}}">
{{$item->name}}<br>
{{$item->day}}<br>
{{$item->from}}<br>
{{$item->to}}
</a>
</td>
#elseif($item->day === "sunday")
<td>
<a class="btn btn-primary btn-lg" href="{{url('button',['id'=>$item->id])}}">
{{$item->name}}<br>
{{$item->day}}<br>
{{$item->from}}<br>
{{$item->to}}
</a>
</td>
#elseif($item->day === "monday")
<td>
<a class="btn btn-primary btn-lg" href="{{url('button',['id'=>$item->id])}}">
{{$item->name}}<br>
{{$item->day}}<br>
{{$item->from}}<br>
{{$item->to}}
</a>
</td>
But it doesn't work. What's the problem??
You should sort the data before you get to the foreach loop and then just echo out the html.
If you use a date field with a carbon instances you also have a nummeric representation of the weekday so you could use that, like:
$collection->sortBy(function($a, $b){
return $a->date->day <=> $b->date->day; // date === carbon instance
});
If you only have a string representation you should get the weekday number by using either Carbon or PHP date, strtotime or DateTime and generate the number.
https://carbon.nesbot.com/docs/#api-getters

Not working below angularjs code

<tr data-ng-repeat="myTrack in myTrackList">
<td class="project-status" data-ng-switch="myTrack.opened">
<span data-ng-switch-when="true" class="label label-primary">Active</span>
<span data-ng-switch-when="false" class="label label-default">Inactive</span>
</td>
<td class="project-title"><a
id="{{myTrack.trackId}},{{myTrack.file.fileId}}"
href="getDocumentByFileId?fileId={{myTrack.file.fileId}}&trackId={{myTrack.trackId}}">{{myTrack.trackName}}</a>
<br /><small>Description: {{myTrack.description}}</small>
<br /> <small>Created: {{myTrack.createdTime}}</small></td>
<td class="project-people" colspan="2">
<div class="project-people" ng-repeat="persons in myTrack.personTrack">
<a href=""><img alt="image" title="persons.firstName"
class="img-circle" src="persons.profilePictureUrl"></a>
</div>
</td>
<td class="project-actions"><a href="#"
class="btn btn-white btn-sm"><i class="fa fa-folder"></i>
View </a></td>
</tr>
I taken list of objects and in single object there is set of user with their profile picture. I used ng-repeat in ng-repeat for repeating users set from list of objects but my inner ng-repeat not working correctly. please help me.
As for the code bellow :
<div class="project-people" ng-repeat="persons in myTrack.personTrack">
<img alt="image" title="persons.firstName" class="img-circle" src="persons.profilePictureUrl">
</div>
The browser do not know that you are using angular js variable for title or src. So you will need.
<div class="project-people" ng-repeat="persons in myTrack.personTrack">
<img alt="image" ng-attr-title="{{persons.firstName}}" class="img-circle" ng-src="{{persons.profilePictureUrl}}">
</div>
So you will need to include the ng tags when you want to use the angular js for tags.

Resources