whats wrong why in blade don't want to show the LEVEL name
controller:
$streams=Stream::with('level')->get();
return view('streams.index',compact('streams'));
Stream Model:
public function level()
{
return $this->belongsTo('App\Models\Level', 'level_id');
}
Level Model:
public function stream()
{
return $this->hasMany('App\Models\Stream', 'level_id');
}
Blade Index:
#foreach($streams as $stream)
<tr>
<th scope="row">#</th>
<td>{{$stream->name}}</td>
<td>{{$stream->code}}</td>
<td>
{{$stream->level->name}}
</td>
<td>
<tr/>
#endforeach
Also tried:
#foreach($streams as $stream)
<tr>
<th scope="row">#</th>
<td>{{$stream->name}}</td>
<td>{{$stream->code}}</td>
#foreach($stream as $level)
<td>
{{$level->name}}
</td>
#endforeach
<td>
<tr/>
#endforeach
Error:
Trying to get property 'name' of non-object (View: C:\xampp\htdocs\project\sms\resources\views\streams\index.blade.php)
DD result:
[{"id":3,"name":"Class A","code":"GRC0001A","uuid":"X9HG9zc7ceTlhdfF1fN1wAer1cHP1MhfuM7GHBSqNogYSo3bsGmpTl06iJQyyKp3QMrkHe1VyiTxeKFa49wC7W5BY3E3kFZkpF1D","level_id":1,"created_at":"2020-06-14T10:58:28.000000Z","updated_at":"2020-06-14T11:39:54.000000Z","level":{"id":1,"name":"YEAR 7","code":"CODE1","uuid":"X9HG9zc7ceTlhdfF1fN1wAer1cHP1MhfuM7GHBSqNogYSo3bsGmpTl06iJQyyKp3QMrkHe1VyiTxeKFa49wC7W5BY3E3kFZkpF1D","created_at":"-000001-11-30T00:00:00.000000Z","updated_at":"-000001-11-30T00:00:00.000000Z"}},{"id":4,"name":"Class B","code":"GRC0001A","uuid":"gq2kZZikN76XEa4pQWsyAZBMxjKeHBJt0a840ZMSiuHGztuhYT0G6q5WcGgp8z6BD6nx0WSrrOTvEb4iQ0ewyB9Fa1M54CAv8HS2","level_id":null,"created_at":"2020-06-14T10:58:36.000000Z","updated_at":"2020-06-14T11:39:59.000000Z","level":null}]
Your first Blade template is almost correct.
The issue is that one or more of your records has no Level assigned (level_id=null), but you're still trying to pull the name property from $stream->level, which is null.
Simply add a check for $stream->level before trying to access/print its properties. For example:
#if($stream->level)
{{ $stream->level->name }}
#endif
or
{{ $stream->level ? $stream->level->name : 'No Level Attached' }}
or
{{ optional($stream->level)->name }}
etc.
Related
Im trying to make a list with game developers, and when you click on one of them you can see the games they have made. I only dont know how to get further....
This is what i have now:
Index.blade.php:
<table class="table">
<thead class="thead-dark">
<tr>
<th scope="col">#</th>
<th scope="col">Naam</th>
<th scope="col">Opgericht in:</th>
</tr>
</thead>
<tbody>
#foreach($gamedevs as $gamedev)
<tr>
<td>{{ $gamedev['id'] }} </td>
<td>{{ $gamedev['naam'] }}</td>
<td>{{ $gamedev['opgericht'] }}</td>
</tr>
#endforeach
</tbody>
I already tried some methods like these:
href="{{route('games.show', ['id'=>$gamedev->id])}}
and
href="/games/{{$gamedev['id']}}
listcontroller.php
public function index()
{
$gamedevs = Gamedevs::all();
return view("index", [ 'gamedevs' => $gamedevs]);
}
public function show(Gamedevs $gamedevs)
{
$gamedevs = Gamedevs::where($gamedevs)->first();
return view('pages.games.show')->with('Gamedevs',$gamedevs);
}
Never did this before.. so hope im on the right way :)
Laravel controller methods like show will use service container to resolve all the parameters you define there. Since you define a model, laravel will automatically find the one corresponding with explicit model binding.
So you wont have to write another query to show your Gamedev:
public function show(Gamedevs $gamedevs)
{
return view('pages.games.show')->with('Gamedevs',$gamedevs);
}
Sample route in web.php
Route::get('/games/{dev_id}','listController#show');
Sample Method in your listController.php
public function show($gamedevs)
{
#let's say your $gamedevs is your devs id so target their id's
$devs = Gamedevs::where('id',$gamedevs)->first();
return view('pages.games.show')->with([
'Gamedevs'=>$devs
]);
}
In your view
href="/games/{{$Gamedevs->id}}
I'm using the Discord API to get the members on a Discord guild to display 10 randoms users and display it in the view with blade using the foreach method.
I'm getting the JSON from the API, getting the members array of it and sclicing it to return only 10 members to send it from the controller to the view.
The problem is that, when I want to parse it and use an element of the array (avatar_url), it's not working and I'm getting this error message: Trying to get property 'avatar_url' of non-object
Here is my controller
class IndexController extends Controller {
...
/**
* Function to get 10 random players on fivem
* #return array
*/
private function getServerPlayers() {
$request = file_get_contents('https://discordapp.com/api/guilds/683766194884575355/widget.json');
$decoded = json_decode($request, true)['members'];
return array_slice($decoded, 0, 10);
}
/**
* Return index view
* #return Factory|View
*/
public function index() {
return view('index')->with('players', $this->getServerPlayers())->with('clients', $this->getServerClients())->with('members', $this->getDiscordMembers());
}
}
Here is my view
<div class="row">
<div class="col-lg-12">
<div class="tab-content" id="pills-tabContent">
<div class="tab-pane fade show active" id="pills-all-bets" role="tabpanel"
aria-labelledby="pills-all-bets-tab">
<div class="responsive-table">
<table class="table">
<thead>
<tr>
<th scope="col">Nom du joueur</th>
<th scope="col">Statut</th>
</tr>
</thead>
<tbody>
#foreach($players as $player)
<tr>
<td><img src="{{ $player->avatar_url }}">{{ $player->username }}</td>
<td>En ligne</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
I think that the problem is kind of dumb but I'm blocked on it, I need some help !
Thanks.
You should use avatar_url like this:
<td><img src="{{ $player['avatar_url'] }}">{{ $player['username'] }}</td>
because what you pass to view is an array, not an object.
The problem is that json_decode returns an array and not an object.
You can pass the arguments to the view using:
return view('index',[
'players' => $this->getServerPlayers(),
'clients' => $this->getServerClients(),
'members' => $this->getDiscordMembers(),
]);
And then display them in the view using:
<img src="{{ $player['avatar_url'] }}">{{ $player['username'] }}
I have 6 types of users in my application. For one view, I want to check if any one of the given two types of users are logged in. If they are, then show this item.
This is working
#can('admin-controller')
<div class="custom-submit-button-group">
View All Messages
</div>
#endcan
But, I want this to work. I want to show the div to these two user types.
#can('admin-controller' || 'general-controller')
<div class="custom-submit-button-group">
View All Messages
</div>
#endcan
But none of them can see this item.
I can use Auth::check() but I only wanted to use #can
Try #canany.
<table>
<tr>
<th>...</th>
#canany(['edit_post', 'delete_post'])
<th>Actions</th>
#endcanany
</tr>
<tr>
<td>...</td>
#canany(['edit_post', 'delete_post'])
<td>
#can('edit_post')
<button>Edit</button>
#endcan
#can('delete_post')
<button>Delete</button>
#endcan
</td>
#endcanany
</tr>
</table>
You can use Gate Facade like this:
#if(Gate::check('update-post') || Gate::check('update-post2'))
#endif
And you can define your permissions in App\Providers\AuthServiceProvider liek this:
public function boot()
{
$this->registerPolicies();
Gate::define('update-post', function ($user, $post) {
return $user->id == $post->user_id;
});
}
OR
#if($user->can('perm1') || $user->can('perm2'))
// do something
#endif
I have the variable $material_details->pricing=null I want to check the variable is set in laravel blade file.I have tried as
#isset($material_details->pricing)
<tr>
<td>price is not null</td>
</tr>
#endisset
but no luck .How to check the variable is set or not in laravel 5.3 blade file.
You can use either Null Coalescing or Elvis Operator
Null Coalescing Operator
{{ $material_details ?? 'second value' }} //object
{{ $material_details->property ?? 'second value' }} //Object Property
Elvis Operator (checks given value is empty or null)
{{ $material_details ?: 'default value' }} //Object
{{ $material_details->property ?: 'default value' }} //Object Property
Read on Elvis and Null coalescing operator.
Links:
Elvis: https://en.wikipedia.org/wiki/Elvis_operator
Null coalescing operator: https://en.wikipedia.org/wiki/Null_coalescing_operator
Try following code.
#if(is_null($material_details))
// whatever you need to do here
#else
Try this
#if(isset($material_details->pricing))
<tr>
<td>NOT NULL</td>
</tr>
#else
<tr>
<td>NULL</td>
</tr>
#endif
Or this
#if(empty($material_details->pricing))
<tr>
<td>NULL</td>
</tr>
#else
<tr>
<td>NOT NULL</td>
</tr>
#endif
Your code will only print data if the variable hold, some value.
Try something like following :
#if(isset($material_details->pricing))
<tr>
<td>price is not null</td>
</tr>
#else
<tr>
<td>null</td>
</tr>
#endif
You can do it by using laravel ternary operator as like below
{!! !empty($material_details->pricing) ? '<tr><td>price is not null</td></tr>' : '<tr><td>Empty</td</tr>' !!}
You can also use '#isset()' balde directive :
#isset($records)
// $records is defined and is not null...
#endisset
#empty($records)
// $records is "empty"...
#endempty
Please try this
#if($material_details->pricing != null)
<tr>
<td>price is not null</td>
</tr>
#endif
If are able to access your variable through {{ $material_details['pricing'] }} inside your blade file,
then this will work to check and append value:
{{ ($material_details['pricing'] == "null") ? "null value" : $material_details['pricing'] }}
To check and append elements :
#if($material_details['pricing'] != "null")
<tr>
<td>price is not null</td>
</tr>
#endif
You can simply use Laravel isEmpty() by chaining it to your collection on blade.
#if($data->quotes->isEmpty())
//Empty
#else
// Not Empty
#endif
More info on Laravel API website here.
I Building an Affiliate System for my Users. Registration with ID works fine and save all correct in my DB, but i cant see any Referrals that use my URL ID for Registration in my Example Blade.
Any one cant find the Error?
Controller:
$referal_id = $user->id;
$referer_id = $request['referal_id'];
$referalUserData = [
'referer_id' => $referer_id,
'referal_id' => $referal_id,
];
$insert = ReferralUser::insert($referalUserData);
Blade:
<tr>
<th>#</th>
<th>User Id#</th>
<th>User Name</th>
<th>Registration Time</th>
</tr>
</thead>
<tbody>
#if(!empty($referalUser))
#foreach($referalUser as $referData)
<tr>
<th scope="row">{{$referData->id}}</th>
<td>{{$referData->referal_id}}</td>
<td>#for($i=0;$i<count($referData['names']);$i++) {{$referData['names'][$i]['name']}} #endfor
</td>
<td> {{$referData->created_at}}<br/> <span class="label label-primary">
{{Carbon\Carbon::createFromTimestamp(strtotime($referData->created_at))->diffForHumans()}} </span> </td>
</tr>
#endforeach
#else
<div class="alert alert-noreferals">
<strong></strong><span>No referrals!</span>
</div><br/><br/>
#endif
Ignore the created_at Timestamp.
Thanks
Did you convert the $request object to an array?
$request['referal_id'];
That looks suspicious. Typically you would use
$request->input('referal_id')
or via dynamic properties
$request->referal_id
To access a given value.
Are you getting an error? Anything in the log files?