I am trying to show my variables values to view page, but it's showing the error of trying to get property of non-object. Please someone help me.
Here is my Controller-
public function myAffiliates(Request $request) {
if ($user_id = Sentinel::getUser()->id) {
$affiliates = DB::table('affiliate_users')
->where('affiliate_users.user_id', $user_id)
->first();
}
//dd($affiliates);
return view('dashboard.affiliates.show',['affiliates' => $affiliates]);
}
And I wrote to my view page like this-
#foreach ($affiliates as $affiliate)
<ul>
<li>First Upline - {{ $affiliate->first_upline_id }} Pts,</li>
<li>Second Upline - {{ $affiliate->second_upline_id }} Pts,</li>
<li>Third Upline - {{ $affiliate->third_upline_id }} Pts.</li>
</ul>
#endforeach
use first() to retrieve a single object
use get() to retrieve collection and iterate it on your blade
You are trying to iterate on object's properties try this
<ul>
<li>First Upline - {{ $affiliates->first_upline_id }} Pts,</li>
<li>Second Upline - {{ $affiliates->second_upline_id }} Pts,</li>
<li>Third Upline - {{ $affiliates->third_upline_id }} Pts.</li>
</ul>
Related
Basically I have a function within a controller which is
public function get_all_clubs($city)
{
$city = ucwords($city);
$city_des = Town::where('town', $city)->value('town_desc');
$clubs = Club::get()->where('city', $city)->where('profile_enabled', '1');
$events = Event::where('club_id', $clubs->first()->id)->get();
$lowercase_city = Str::lower($city);
return view('frontend.pages.allclubs', compact('clubs', 'lowercase_city', 'city_des', 'events', 'flag'));
}
The events and the club tables are related using an id as you might have figured out. I have been able to get the events associated to a city based on the club_id. In my view I am echoing each event using the
#foreach Laravel syntax. The problem however is I need to create an hyperlink for each event on on my view. The routes are structred in the way below:
<a href="{{ route('event.show_event', [$club->slug, $event->slug]) }}">
Effectively, I need to pass the $event->slug which I already have and also the $club->slug which is what my question is, how do I get hold of this variable during my #foreach loop. Please note I am an intermediate Laravel/php developer.
My view is as follows
#foreach ($events as $event)
#if ( ( (strtotime((str_replace('/', '-', $event->date)))) >= (strtotime("today")) ) && ($event->event_private))
#php $flag='1'; #endphp
<li>
<div class="widget-posts-body">
<h6 class="widget-posts-title"> {{$event->title}} </h6>
</div>
</li>
#endif
#if ( ($loop->index) == '10' )
#break
#endif
#endforeach
#if($flag != '1')
<li>
<p> There are no events live at this time. Please check back later</p>
</li>
#endif
make a one to one relation in event model to club like this:
/**
* #return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function club()
{
return $this->hasOne(Club::class);
}
then call it in view like this:
<a href="{{ route('event.show_event', [$event->club->slug, $event->slug]) }}">
What is the most effective way for returning data from two tables in on view?
Like an employee check the vehicle for each order.
Route::get('orderVehicle',"adminController#orderVehicle");
public function orderVehicle(Request $reques){
$orders = new Order;
$vehicles = new Vehicle; $orders->id; $vehicles->id; return view('adminVeiw.orderVehicle',compact('orders','vehicles')); }
#foreach($orders as $or) {{ $or->id }} #endforeach {{ $vehicles->id }}
And the error is
"Trying to get property 'id' of non-object (View:
/var/www/html/full-Restaurant-App-Using-Laravel/resources/views/adminVeiw/orderVehicle.blade.php)"
So any suggestions?
do this if you don't wanna change your code in controller
#foreach($orders as $or)
#if(!empty($or->id))
{{ $or->id }}
#endif
#endforeach
#if(!empty($vahicles->id))
{{ $vehicles->id }}
#endif
in this case you won't get error but i don't know how you wanna this works for you,
i hope it helps
public function orderVehicle()
{
$orders = Order::create();
$vehicles = Vehicle::create();
return view('adminVeiw.orderVehicle', compact('orders', 'vehicles'));
}
'm trying to get non duplicated in My blade tasks using this Code
I have Edited My Question So i added Controller and full blade Code
My Controller
$posts2 = Path::with(['pathtags' => function ($q) use ($TagArray)
{$q->with(['Tasks' => function ($q) use ($TagArray) {$q->has('tasktags', '=', 2)
->with('tasktags');
}]);
}])->where('id', '=', 1)->get();
My Blade
#foreach ($posts2 as $item)
<h2> {{$item->name}}</h2>
#foreach ($item->pathtags as $Tag)
#foreach ($Tag->Tasks as $Task)
#php $a=array(); #endphp
#if (in_array($Task->task_name,$a))
<li> Task :: {{ $Task->task_name }} </li>
#php
array_push($a,"$Task->task_name");
#endphp
#else {
<li> Task :: Not Found </li>
}
#endif
#endforeach
#endforeach
#endforeach
you are emptying the array in each iteration. move the initialization of array before the foreach loop. also, the whole logic is wrong. you are checking if the item exists in the array, and if so, add it again.
#php $a=array(); #endphp
#foreach ($Tag->Tasks as $Task)
#if (!in_array($Task->task_name,$a))
<li> Task :: {{ $Task->task_name }} </li>
#php
array_push($a,$Task->task_name);
#endphp
#else
<li> Task :: Duplicated </li>
#endif
#endforeach
Change this
array_push($a,"$Task->task_name");
with
array_push($a, $Task->task_name);
// or
$a[] = $Task->task_name;
In stock PHP you can use array_count_values to get the count of each array items.
Then use array_diff or array_intersect to get the different unique or duplicated items.
Array_keys return the values from original array.
$arr = ["one", "one", "two", "two", "three"];
$count = array_count_values($arr);
echo "duplicates \n";
var_dump(array_keys(array_diff($count, [1])));
echo "uniques \n";
var_dump(array_keys(array_intersect($count, [1])));
https://3v4l.org/DKIbZ
I'm having trouble outputting a list of key value pairs in Laravel blade.
My show.blade.php template is:
#extends('layouts.app')
#section('content')
<ul>
#foreach ($datapoint as $key => $value)
<li>{{ $key, $value }}</li>
#endforeach
</ul>
{{ $datapoint }}
#endsection
And my controller which displays this template is:
<?php
namespace App\Http\Controllers;
use App\Hyfes;
use Illuminate\Http\Request;
class CaptainController extends Controller
{
public function getLiveData ()
{
$datapoint = Hyfes::find(1);
return view('captain.show')->with('datapoint', $datapoint);
}
}
The data output to the view in the browser is this:
incrementing
exists
wasRecentlyCreated
timestamps
{"date":"08/07/2017","time":"12:02:25","boat_name":"cyclone","current_location":"North Greenwich Pier","status":"docked","embarked":26,"disembarked":2,"people_on_board":24,"current_speed":0,"hotel_power":231,"battery_power_demand":342,"battery_output_power":23,"engine_output_power":12,"battery_power_stored":100,"battery_depth_of_discharge":323,"fuel_consumption_rate":7,"nox":432,"co2":213,"battery_state_of_charge":100,"id":1}
Ideally, I would like to see a list of bullet points showing e.g.:
date: 8/07/2017
time: 12:00:01
boat_name: cyclone
The function Hyfes::find(1) will return an object of the type App\Hyfes and not an array. By looping over the key value pairs you are getting all properties. Laravel adds a lot of extra properties for keeping track of the object.
A better way to do this is using the Laravel getAttributes function. This will only get the attributes loaded from the database.
Like so:
#foreach ($datapoint->getAttributes() as $key => $value)
<li>{{ $key }}: {{ $value }}</li>
#endforeach
You are trying to iterate over a non array variable in your template. You have to be explicit about the values of the objects you would like to see in your template.
#extends('layouts.app')
#section('content')
<ul>
<li>date: {{ $datapoint->date }}</li>
<li>time: {{ $datapoint->time }}</li>
<!-- and so on -->
</ul>
#endsection
I am trying to pass a set of rows from a controller to a view:
$items = Items::where('group', '=', $group);
return view('listing', [
"group" => $group,
"exists" => $items->exists(),
"items" => $items->get(),
]);
And in my view:
#if (!$exists)
Nothing
#else
<ul id="items">
#foreach ($items as $item)
<li>
{{ $item->user }}: {{ $item->content }}
</li>
#endforeach
</ul>
#endif
The view will only return 1 item though. The length of $items is 1. If I count($items) in the controller, I get the expected number of items.
How can I pass an object to my view from a controller?
My final solution:
Controller
$items = Items::where('group', '=', $group);
return view('listing', [
"group" => $group,
"items" => $items->get(),
"exists" => $items->exists(), // Important! Has to go after!
]);
And in my view:
#if (!$exists)
Nothing
#else
<ul id="items">
#foreach ($items as $item)
<li>
{{ $item->user }}: {{ $item->content }}
</li>
#endforeach
</ul>
#endif
The issue is the call to exists() before the call to get(). The call to exists() is modifying your query builder to add a limit(1) to it. Therefore, when you call get() after exists(), the query builder still has the limit(1) attached.
Your updated solution works because you removed the call to exists().
However, the call to get() should still be done in the Controller. The view should only be passed the collection of objects, not the query builder.
I believe if you put get() at the end it will work.
$items = Items::where('group', '=', $group)->get();
You are just pulling back the model object and not the data. That is why the count is 1 when you are expecting 4.
Edited per comment
I think the get where you have it might be causing some funkiness.
$items = Items::where('group', '=', $group)->get();
return view('listing', [
"group" => $group,
"items" => $items,
]);
#if (!$items->exists())
Nothing
#else
<ul id="items">
#foreach ($items as $item)
<li>
{{ $item->user }}: {{ $item->content }}
</li>
#endforeach
</ul>
#endif
You might put a dd($items->toArray()); after you query Items to see what you get.