Show pages name

Published 1 month ago by davy_yg

HomeController.php

public function getAdminPages()
    {

        $domain = $_SERVER['SERVER_NAME'];
        $user = User::where('domain', $domain)->first();  
    
        $pages_name = Pages::where('user_id', $user->id)->get('pages_name');
          

        return view('soulfy.apages', ['pages_name' => $pages_name]);
    }

I am trying to print out all the pages name for a certain user_id. How to write the view?

I am getting this error message:

FatalThrowableError in Grammar.php line 107: Type error: Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array, string given, called in C:\xampp\htdocs\soulfy_repo\framework\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 108

tisuchi
tisuchi
1 month ago (262,165 XP)

How do you print in view?

davy_yg

I wonder why I cannot write my complete view in this textbox? and there is this red text.

apages.blade.php

{{ pages_name }}

Edit Delete

tisuchi
tisuchi
1 month ago (262,165 XP)

You need to use foreach for that.

For example-

@foreach($pages_name as $page)
    {{ $page->pages_name }}
@endforeach

Its because you are getting collections from Pages table.

davy_yg

I still having this error message after writing the for each

FatalThrowableError in Grammar.php line 107: Type error: Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array, string given, called in C:\xampp\htdocs\soulfy_repo\framework\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 108

tisuchi
tisuchi
1 month ago (262,165 XP)

Change this line-

return view('soulfy.apages', ['pages_name' => $pages_name]);

to

return view('soulfy.apages')->with(
['pages_name' => $pages_name]);
davy_yg

It doesn't make any different.

36864
36864
1 month ago (26,430 XP)

I really wish that just once you'd actually read the exception that's thrown before posting.

FatalThrowableError in Grammar.php line 107: Type error: Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array, string given, called in C:\xampp\htdocs\soulfy_repo\framework\vendor\laravel\framework\src\Illuminate\Database\Query\Grammars\Grammar.php on line 108

Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array, string given

If you examine the stack trace, you'll see that this is being thrown by $pages_name = Pages::where('user_id', $user->id)->get('pages_name');

https://laravel.com/api/5.5/Illuminate/Database/Eloquent/Builder.html#method_get

The get method expects an array of columns. You're passing in a string. A string is not an array (at least no in php).

kenny11

I got a feeling that you might be the biggest troll on Laracasts. You don't even reply to others on the topics you created nor you don't mark a reply that could have solved your problem @davy_yg

davy_yg

@kenny11, Gee I did marked it solve when I am satisfy with the solution and I did reply as I like.

@all:

I wonder why this does not works:

  $pages_name = Pages::where('user_id', $user->id)->get('pages_name');

This does works:

  $pages = Pages::where('user_id', $user->id)->get();

I finally can see the page comes out.

topvillas

@36864 told you why it doesn't work.

kenny11

Are u serious? What's the point asking a question if you don't even read it?

davy_yg

@kenny11 - I read everything, I just don't like your normal reply about forcing me to search the google. Even after I search the google - I still have another question.

Besides that I can only select one best answer which I think fits my needs. It's my thread anyway.

kenny11

Don't you think it's disrespectful to not read an answer provided by people like @36864 ?

Can you just select his reply as the best answer and end this nonsense? What else do you need?

36864
36864
1 month ago (26,430 XP)

Can't teach a man to fish if he doesn't know what a fish is.

$pages_name = Pages::where('user_id', $user->id)->get('pages_name');

This doesn't work because ->get() requires you to pass in an array of columns. If you want a single column, you need to pass an array with a single element.

$pages_name = Pages::where('user_id', $user->id)->get(['pages_name']);

I am assuming that pages_name is the name of the column in your pages table that holds the name of your page and that Pages is actually the name of your model, instead of Page which it should be if you bothered to follow convention.

davy_yg

@kenny11 - you don't have to reply to my thread if it bothers you.

@36864 - thanks.

Please sign in or create an account to participate in this conversation.