laravel 5.4 - blade; Function name must be a string, -
i new laravel , using 5.4, php 7.0
i have 3 tables: users, roles; profiles. able write each tables fine, when trying read profile table using blade, getting error: "function name must string"
in blade, have following code
@foreach(auth()->user()->profiles $profile) profile id is: {{ $profile()->id }} !<br> - @endforeach @foreach(auth()->user()->roles $role) role is: {{ $role->name }} !<br> @endforeach
* first 1 gives error, second works fine *
* if put ( ) profiles *
@foreach(auth()->user()->profiles() $profile) profile id is: {{ $profile()->id }} !<br> - @endforeach
* not give error why ? *
i using 2 pivot table: 'role_users'
{ schema::create('role_users', function (blueprint $table) { $table->unsignedinteger('user_id'); $table->unsignedinteger('role_id'); $table->timestamps(); $table->unique(['user_id','role_id']); $table->foreign('user_id')->references('id')->on('users')->ondelete('cascade') ; $table->foreign('role_id')->references('id')->on('roles')->ondelete('cascade') ;});
second one: 'profiles_users'
schema::create('profiles_users', function (blueprint $table) { $table->increments('id'); $table->unsignedinteger('user_id'); $table->unsignedinteger('profile_id'); $table->timestamps(); $table->unique(['user_id','profile_id']); $table->foreign('user_id')->references('id')->on('users')->ondelete('cascade') ; $table->foreign('profile_id')->references('id')->on('profiles')->ondelete('cascade');
* in users model * both tables link following way:
public function profiles() { return $this->belongstomany(profile::class, 'profiles_users'); } public function roles() { return $this->belongstomany(role::class, 'role_users'); }
* in profile model * user table link following way:
public function users() { return $this->belongsto(user::class,'profiles_users'); }
* register controller has create function, end of function, make attach *
$user->roles()->attach($data['role']); // key link role_users table; works $user->profiles()->attach($profile['id']); // key link role_users table
to write data table, attach works fine. * there wrong, when try read data profile table ? *
protected function create(array $data) { $user = user::create([ 'username' => $data['username'], // 'email' => $data['email'], 'password' => bcrypt($data['password']), ]); $profile = profile::create([ // works sava profile information 'firstname' => $data['firstname'], 'lastname' => $data['lastname'], 'nickname' => $data['nickname'], 'middlename' => $data['middlename'], ]); $user->roles()->attach($data['role']); // key link role_users table; works $user->profiles()->attach($profile['id']); // key link role_users table return $user; return $profile;
Comments
Post a Comment