Reputation: 129
I keep getting this error when I pass a user(venue) object through a controller into my view, I've tried encoding it, and decoding it to no avail. I've also tried finding the user in the view instead of in the controller by just passing the id of the user through, but I get the same error? People in other questions seem to have no problem doing it with this syntax or similar, am I doing something wrong?
htmlspecialchars() expects parameter 1 to be string, object given
Controller:
public function index($venue)
{
$venue = \App\venue::findOrFail($venue);
return view('index.venue')->with(['venue'=>$venue]);
}
I'm trying to access it in views like this:
<div class="venueName">
<h4>{{ $venue->name }}</h4>
</div>
<p>{{ $venue->addresse }}</p>
<p>{{ $venue->bio }}</p>
<img src="{{ url($venue->galleryOne) }}" alt="">
@foreach ($venue->item as $item)...
this is the dd($venue):
venue {#247 ▼
#guard: "venue"
#fillable: array:5 [▶]
#hidden: array:2 [▶]
#connection: "mysql"
#table: "venues"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:11 [▶]
#original: array:11 [▶]
#changes: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [▶]
#rememberTokenName: "remember_token"
}
Any Help would be fantastic! Thankyou :)
Upvotes: 1
Views: 765
Reputation: 4201
You will add get method after findOrFail method,if exist data .... if not exist data or null ,you must control if(empty($venue)) in view
$venue = \App\venue::findOrFail($venue)->get();
return view('index.venue')->with(['venue'=>$venue]);
Upvotes: 0
Reputation: 129
Thanks to everyone for figuring out it must be a result of a call to an object variable in the view. Solution: Some of the variables were "NULL" which is processed as an object, and causes the error. I changed the default values in the database from NULL to an empty string, and it worked fine :).
Upvotes: 1
Reputation: 19
One of the items in one of the objects is an object too and the {{ }}
requires a string to be the variable inside there. Check all items returned by objects.
Upvotes: 1