laravel setting up an profile page for the users error Property [user] does not exist on this collection instance










0















Im trying to set up a page where an user can add a description about him self for example what kind of hobbies he or she is interrested in so i made a sepperate table from the users, so theres an USER table and a PROFILE table this is how both tables looks



user table



 public function up()
{
Schema::create('users', function (Blueprint $table)
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
);


profile table



 public function up()

Schema::create('profiles', function (Blueprint $table)
$table->increments('id');
$table->integer('user_id');
$table->string('email')->unique();
$table->string('firstname');
$table->string('lastname');
$table->integer('age');
$table->integer('birthdate');
$table->text('bio');
$table->timestamps();
);



so whit in mine models i set a relationship to the profile and user whit belognsto()
function in laravel and hasone() this how the model looks



user.php



public function profile()



return $this->hasOne(Profile::class);




profile.php



public function user()



return $this->belongsTo(User::class);




but I get an error when I trying adding this to the blade for example



 $profile->user 


its says the variable is unable to be found, so did I not set the relation ships right in laravel that its giving the error or is it someting else



the error is [Property [user] does not exist on this collection instance.]



ProfileController.php



public function index()


$profile = Profile::all();

return view ('profile.show',compact('profile'));











share|improve this question






















  • Can you perform the reverse? $user->profile? Can you please dump the contents of profile?

    – user10341554
    Nov 14 '18 at 17:16











  • it says Undefined variable: user

    – InterstingJavaLearner
    Nov 14 '18 at 17:27











  • Try to add foreign keys to your profiles table. like $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdated('cascade');

    – Vahe Shak
    Nov 14 '18 at 17:28












  • Okay, are you defining user or profile anywhere? Do you get the signed-in user anywhere?

    – user10341554
    Nov 14 '18 at 17:29











  • yes the user can register or login you want me to add that part to or?

    – InterstingJavaLearner
    Nov 14 '18 at 17:34
















0















Im trying to set up a page where an user can add a description about him self for example what kind of hobbies he or she is interrested in so i made a sepperate table from the users, so theres an USER table and a PROFILE table this is how both tables looks



user table



 public function up()
{
Schema::create('users', function (Blueprint $table)
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
);


profile table



 public function up()

Schema::create('profiles', function (Blueprint $table)
$table->increments('id');
$table->integer('user_id');
$table->string('email')->unique();
$table->string('firstname');
$table->string('lastname');
$table->integer('age');
$table->integer('birthdate');
$table->text('bio');
$table->timestamps();
);



so whit in mine models i set a relationship to the profile and user whit belognsto()
function in laravel and hasone() this how the model looks



user.php



public function profile()



return $this->hasOne(Profile::class);




profile.php



public function user()



return $this->belongsTo(User::class);




but I get an error when I trying adding this to the blade for example



 $profile->user 


its says the variable is unable to be found, so did I not set the relation ships right in laravel that its giving the error or is it someting else



the error is [Property [user] does not exist on this collection instance.]



ProfileController.php



public function index()


$profile = Profile::all();

return view ('profile.show',compact('profile'));











share|improve this question






















  • Can you perform the reverse? $user->profile? Can you please dump the contents of profile?

    – user10341554
    Nov 14 '18 at 17:16











  • it says Undefined variable: user

    – InterstingJavaLearner
    Nov 14 '18 at 17:27











  • Try to add foreign keys to your profiles table. like $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdated('cascade');

    – Vahe Shak
    Nov 14 '18 at 17:28












  • Okay, are you defining user or profile anywhere? Do you get the signed-in user anywhere?

    – user10341554
    Nov 14 '18 at 17:29











  • yes the user can register or login you want me to add that part to or?

    – InterstingJavaLearner
    Nov 14 '18 at 17:34














0












0








0








Im trying to set up a page where an user can add a description about him self for example what kind of hobbies he or she is interrested in so i made a sepperate table from the users, so theres an USER table and a PROFILE table this is how both tables looks



user table



 public function up()
{
Schema::create('users', function (Blueprint $table)
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
);


profile table



 public function up()

Schema::create('profiles', function (Blueprint $table)
$table->increments('id');
$table->integer('user_id');
$table->string('email')->unique();
$table->string('firstname');
$table->string('lastname');
$table->integer('age');
$table->integer('birthdate');
$table->text('bio');
$table->timestamps();
);



so whit in mine models i set a relationship to the profile and user whit belognsto()
function in laravel and hasone() this how the model looks



user.php



public function profile()



return $this->hasOne(Profile::class);




profile.php



public function user()



return $this->belongsTo(User::class);




but I get an error when I trying adding this to the blade for example



 $profile->user 


its says the variable is unable to be found, so did I not set the relation ships right in laravel that its giving the error or is it someting else



the error is [Property [user] does not exist on this collection instance.]



ProfileController.php



public function index()


$profile = Profile::all();

return view ('profile.show',compact('profile'));











share|improve this question














Im trying to set up a page where an user can add a description about him self for example what kind of hobbies he or she is interrested in so i made a sepperate table from the users, so theres an USER table and a PROFILE table this is how both tables looks



user table



 public function up()
{
Schema::create('users', function (Blueprint $table)
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
);


profile table



 public function up()

Schema::create('profiles', function (Blueprint $table)
$table->increments('id');
$table->integer('user_id');
$table->string('email')->unique();
$table->string('firstname');
$table->string('lastname');
$table->integer('age');
$table->integer('birthdate');
$table->text('bio');
$table->timestamps();
);



so whit in mine models i set a relationship to the profile and user whit belognsto()
function in laravel and hasone() this how the model looks



user.php



public function profile()



return $this->hasOne(Profile::class);




profile.php



public function user()



return $this->belongsTo(User::class);




but I get an error when I trying adding this to the blade for example



 $profile->user 


its says the variable is unable to be found, so did I not set the relation ships right in laravel that its giving the error or is it someting else



the error is [Property [user] does not exist on this collection instance.]



ProfileController.php



public function index()


$profile = Profile::all();

return view ('profile.show',compact('profile'));








php laravel laravel-5.7






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 17:13









InterstingJavaLearnerInterstingJavaLearner

377




377












  • Can you perform the reverse? $user->profile? Can you please dump the contents of profile?

    – user10341554
    Nov 14 '18 at 17:16











  • it says Undefined variable: user

    – InterstingJavaLearner
    Nov 14 '18 at 17:27











  • Try to add foreign keys to your profiles table. like $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdated('cascade');

    – Vahe Shak
    Nov 14 '18 at 17:28












  • Okay, are you defining user or profile anywhere? Do you get the signed-in user anywhere?

    – user10341554
    Nov 14 '18 at 17:29











  • yes the user can register or login you want me to add that part to or?

    – InterstingJavaLearner
    Nov 14 '18 at 17:34


















  • Can you perform the reverse? $user->profile? Can you please dump the contents of profile?

    – user10341554
    Nov 14 '18 at 17:16











  • it says Undefined variable: user

    – InterstingJavaLearner
    Nov 14 '18 at 17:27











  • Try to add foreign keys to your profiles table. like $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdated('cascade');

    – Vahe Shak
    Nov 14 '18 at 17:28












  • Okay, are you defining user or profile anywhere? Do you get the signed-in user anywhere?

    – user10341554
    Nov 14 '18 at 17:29











  • yes the user can register or login you want me to add that part to or?

    – InterstingJavaLearner
    Nov 14 '18 at 17:34

















Can you perform the reverse? $user->profile? Can you please dump the contents of profile?

– user10341554
Nov 14 '18 at 17:16





Can you perform the reverse? $user->profile? Can you please dump the contents of profile?

– user10341554
Nov 14 '18 at 17:16













it says Undefined variable: user

– InterstingJavaLearner
Nov 14 '18 at 17:27





it says Undefined variable: user

– InterstingJavaLearner
Nov 14 '18 at 17:27













Try to add foreign keys to your profiles table. like $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdated('cascade');

– Vahe Shak
Nov 14 '18 at 17:28






Try to add foreign keys to your profiles table. like $table->integer('user_id')->unsigned(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdated('cascade');

– Vahe Shak
Nov 14 '18 at 17:28














Okay, are you defining user or profile anywhere? Do you get the signed-in user anywhere?

– user10341554
Nov 14 '18 at 17:29





Okay, are you defining user or profile anywhere? Do you get the signed-in user anywhere?

– user10341554
Nov 14 '18 at 17:29













yes the user can register or login you want me to add that part to or?

– InterstingJavaLearner
Nov 14 '18 at 17:34






yes the user can register or login you want me to add that part to or?

– InterstingJavaLearner
Nov 14 '18 at 17:34













1 Answer
1






active

oldest

votes


















0














Just as Vahe Shak already mentioned, your profile table needs a foreign key to show user_id is related to id in the users table. Your profile table migration needed to have:



$table->integer('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');


But editing the migration would not effectively make the changes. Use a new migration



php artisan make:migration add_foreign_to_profile


Then the migration should be looking like this:



public function up()

Schema::table('profiles', function(Blueprint $table)

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
);


public function down()

Schema::table('profiles', function(Blueprint $table)

$table->dropForeign('user_id');
);



Then you can run php artisan migrate






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305510%2flaravel-setting-up-an-profile-page-for-the-users-error-property-user-does-not%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Just as Vahe Shak already mentioned, your profile table needs a foreign key to show user_id is related to id in the users table. Your profile table migration needed to have:



    $table->integer('user_id');
    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');


    But editing the migration would not effectively make the changes. Use a new migration



    php artisan make:migration add_foreign_to_profile


    Then the migration should be looking like this:



    public function up()

    Schema::table('profiles', function(Blueprint $table)

    $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    );


    public function down()

    Schema::table('profiles', function(Blueprint $table)

    $table->dropForeign('user_id');
    );



    Then you can run php artisan migrate






    share|improve this answer



























      0














      Just as Vahe Shak already mentioned, your profile table needs a foreign key to show user_id is related to id in the users table. Your profile table migration needed to have:



      $table->integer('user_id');
      $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');


      But editing the migration would not effectively make the changes. Use a new migration



      php artisan make:migration add_foreign_to_profile


      Then the migration should be looking like this:



      public function up()

      Schema::table('profiles', function(Blueprint $table)

      $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
      );


      public function down()

      Schema::table('profiles', function(Blueprint $table)

      $table->dropForeign('user_id');
      );



      Then you can run php artisan migrate






      share|improve this answer

























        0












        0








        0







        Just as Vahe Shak already mentioned, your profile table needs a foreign key to show user_id is related to id in the users table. Your profile table migration needed to have:



        $table->integer('user_id');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');


        But editing the migration would not effectively make the changes. Use a new migration



        php artisan make:migration add_foreign_to_profile


        Then the migration should be looking like this:



        public function up()

        Schema::table('profiles', function(Blueprint $table)

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        );


        public function down()

        Schema::table('profiles', function(Blueprint $table)

        $table->dropForeign('user_id');
        );



        Then you can run php artisan migrate






        share|improve this answer













        Just as Vahe Shak already mentioned, your profile table needs a foreign key to show user_id is related to id in the users table. Your profile table migration needed to have:



        $table->integer('user_id');
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');


        But editing the migration would not effectively make the changes. Use a new migration



        php artisan make:migration add_foreign_to_profile


        Then the migration should be looking like this:



        public function up()

        Schema::table('profiles', function(Blueprint $table)

        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        );


        public function down()

        Schema::table('profiles', function(Blueprint $table)

        $table->dropForeign('user_id');
        );



        Then you can run php artisan migrate







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 19:04









        bryceandybryceandy

        615




        615





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305510%2flaravel-setting-up-an-profile-page-for-the-users-error-property-user-does-not%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3

            Museum of Modern and Contemporary Art of Trento and Rovereto