Support the ongoing development of Laravel.io →
Views Eloquent
Last updated 1 year ago.
0

Can you post the full error with the stack trace? It's hard to find out right now which property does not exists.

PS: I've formatted your post a little bit with code blocks.

0
C:\laragon\www\orhrepo\orhrepo\storage\framework\views\c2fbb4d41c9ba44b1e257164633b56a57e6bf99d.php
<!doctype>
<html lang="en">
<head>
    
    <title>View Users</title>
</head>
<body>
    
<?php if($user_type === 1): ?>
 
<?php echo e(App\Models\Customer::find($user_id)->firstname); ?>
 
 
<?php elseif($user_type === 2): ?>
 
<?php echo e(App\Models\Merchant::find($user_id)->firstname); ?>
 
 
<?php endif; ?>
</body>
</html>
 
 
Arguments
"Trying to get property of non-object (View: C:\laragon\www\orhrepo\orhrepo\resources\views\admin\view_user.blade.php)"

l hope thats what you mean by stacktrace.thanks

0

Hmm can you perhaps look at C:\laragon\www\orhrepo\orhrepo\storage\logs\laravel.log and see if you have a more detailed error there?

0
code text here
```Environment & details:
GET Data empty
POST Data empty
Files empty
Cookies
XSRF-TOKEN	
"eyJpdiI6Illlc0gxTGJtbVU4ZCsyYWV3aXFCQmc9PSIsInZhbHVlIjoiNEl4dnRTSm52QzNBZlRJaUJLMndHRDN6VmJFeEowTFZIaDkzaTU3bVJHd01jd0cxa3BpcmhCTFwvMSsyZWFhb0sycjNjSHRlQUVOMXhq ▶"
laravel_session	
"eyJpdiI6ImlRclI0V1wvM2hqSytJclZlcmVXenZRPT0iLCJ2YWx1ZSI6ImhCbWhLOUJqTCtjZkhLZ1hMQ1wvTTN0V29jOWYyR1hzaGVyUTltaHFVU2hPNWdjVkFHOXh6V3dSazJEVjVcL3E2R0t2ajZ4UHdqSTVB ▶"
Session empty
Server/Request Data
DOCUMENT_ROOT	
"C:\laragon\www\orhrepo\orhrepo\public"
REMOTE_ADDR	
"127.0.0.1"
REMOTE_PORT	
"63349"
SERVER_SOFTWARE	
"PHP 7.1.12 Development Server"
SERVER_PROTOCOL	
"HTTP/1.1"
SERVER_NAME	
"127.0.0.1"
SERVER_PORT	
"8000"
REQUEST_URI	
"/user_view/61"
REQUEST_METHOD	
"GET"
SCRIPT_NAME	
"/index.php"
SCRIPT_FILENAME	
"C:\laragon\www\orhrepo\orhrepo\public\index.php"
PATH_INFO	
"/user_view/61"
PHP_SELF	
"/index.php/user_view/61"
HTTP_HOST	
"127.0.0.1:8000"
HTTP_CONNECTION	
"keep-alive"
HTTP_UPGRADE_INSECURE_REQUESTS	
"1"
HTTP_USER_AGENT	
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
HTTP_ACCEPT	
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
HTTP_REFERER	
"http://127.0.0.1:8000/users";
HTTP_ACCEPT_ENCODING	
"gzip, deflate, br"
HTTP_ACCEPT_LANGUAGE	
"en-GB,en;q=0.9,en-US;q=0.8,cs;q=0.7"
HTTP_COOKIE	
"XSRF-TOKEN=eyJpdiI6Illlc0gxTGJtbVU4ZCsyYWV3aXFCQmc9PSIsInZhbHVlIjoiNEl4dnRTSm52QzNBZlRJaUJLMndHRDN6VmJFeEowTFZIaDkzaTU3bVJHd01jd0cxa3BpcmhCTFwvMSsyZWFhb0sycjNjS ▶"
REQUEST_TIME_FLOAT	
1522063561.606
REQUEST_TIME	
1522063561
Environment Variables empty
Registered Handlers
0. Whoops\Handler\PrettyPageHandler```
code text here
Last updated 6 years ago.
0

I think it's a good start to check if each property you're referring to in your view is actually a DB column on your user table. Also you're doing this somewhere in your view:

App\Models\Merchant::find($user->id)->firstname

Are you sure that you can "find" a Merchant by a user id? If, for example, a merchant can't be found then you can't call ->firstname. You'll get the error from above if you try to do that.

0

When l run App\Models\Merchant::find(1)->firstname. it runs perfectly. but when l use

$user->id

it gives the error above.But the is the whole view page

<!doctype>
<html lang="en">
<head>
	
	<title>View Users</title>
</head>
<body>
	
@if($user_type === 1)

{{ App\Models\Customer::find($user_id) }}

@elseif($user_type === 2)

{{ App\Models\Merchant::find($user_id) }}

@endif
</body>
</html>


For the customer class it works fine but for the merchant class it gives an the error

0

That's because $user->id isn't the same as $merchant->id or $customer->id. Don't those models have their own primary identifier?

Do those tables have an explicit user_id column? Otherwise you need to do this: Merchant::where('user_id', $user->id)->first().

0

This is the User Model

<?php

/**
 * Created by Reliese Model.
 * Date: Fri, 16 Feb 2018 18:55:16 +0000.
 */

namespace App\Models;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

/**
 * Class User
 * 
 * @property int $id
 * @property int $user_type
 * @property string $username
 * @property string $password
 * @property \Carbon\Carbon $last_login
 * @property string $remember_token
 *
 * @package App\Models
 */

class User extends Model implements
	AuthenticatableContract,
	AuthorizableContract,
	CanResetPasswordContract
{
	use Authenticatable, Authorizable, CanResetPassword;
	public $timestamps = false;

	protected $casts = [
		'user_type' => 'int'
	];

	protected $dates = [
		'last_login'
	];

	protected $hidden = [
		'password',
		'remember_token'
	];

	protected $fillable = [
		'user_type',
		'username',
		'password',
		'last_login',
		'remember_token',
		'settings'
	];
	public function toString()
	{
		return "hello";
	}

	public function customer()
	{
		return $this->hasOne('App\Models\Customer');
	}

	public function merchant()
	{
		return $this->hasOne('App\Models\Merchant');
	}

	public function admin(){

		return $this->hasOne('App\Models\Admin');
	}

 

	
}

This is the Merchant Model

<?php

/**
 * Created by Reliese Model.
 * Date: Fri, 16 Feb 2018 18:55:16 +0000.
 */

namespace App\Models;

use Reliese\Database\Eloquent\Model as Eloquent;

/**
 * Class Merchant
 * 
 * @property int $id
 * @property int $user_id
 * @property string $store_name
 * @property string $store_name_url
 * @property string $title
 * @property string $firstname
 * @property string $lastname
 * @property string $bvn_mobile_no
 * @property string $street
 * @property string $city
 * @property string $state
 * @property string $country
 * @property int $status
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 *
 * @package App\Models
 */
class Merchant extends Eloquent
{
	protected $casts = [
		'user_id' => 'int',
		'status' => 'int'
	];

	protected $fillable = [
		'user_id',
		'store_name',
		'store_name_url',
		'title',
		'firstname',
		'lastname',
		'bvn_mobile_no',
		'street',
		'city',
		'state',
		'country',
		'status'
	];

	public function user(){

		return $this->belongsTo('App\Models\User');
	}

	public function name(){
		return $this->firstname . ' ' . $this->lastname;
	}
    
    public function service(){
    	return $this->hasMany('App\Models\Service');
    }
}

The merchant table has a user_id column and the above model define the relationship

Last updated 6 years ago.
0

Yeah, just as I thought. Try Merchant::where('user_id', $user->id)->first() instead of Merchant::find($user->id) :)

0

Thanks alot vints. it works perfectly.. but please can you explain why did is..

0

So the idea is that each model has a primary key (id). But you link the models to each other with other columns. For example, Merchant model has a column user_id which is linked to the user table's id column. When you try to do Merchant::find it will search the id column on the merchants table. But you want to search the user_id column instead because you're passing $user->id.

Hope that explains! Feel free to ask more questions.

0

Meaning this query

App/Models/Customer::find($user->id)->firstname;

searches the customer id column not the user_id column in the customer table

0

Exactly. You'll need to do App/Models/Customer::where('user_id', $user->id)->first(); instead.

0

Btw you might want to take a look at foreign keys which help you prevent these things: https://laracasts.com/lessons/foreign-key-constraints

0

thanks Dries..l certianly will do some reseach on that.Thank you

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Austin austin77 Joined 26 Mar 2018

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.