0

I need a little help. I can see the image on Page A but not on Page B. Both pages output the same image path.

This works:

Controller A:

function index()
    {
        $games = Game::get();
        return Inertia::render('Games', ['games' => $games]);
    }

Page A:

<Link :href="route('show-game', game.id)" v-for="game in games" :key="game.id"
            class="flex flex-row h-36 md:h-20 odd:bg-white even:bg-green-100 hover:bg-slate-200">
        <div href="" class="flex items-center justify-center w-20 border">
            <div class="p-1">
                <img :src="game.image" />
            </div>
        </div>
</Link>

This works not:

Controller B:

public function index($id)
    {
        $game = Game::where('id', $id)->get()->first();
        $gameComments = Game::find($id)->usergameComments;

        return Inertia::render('ShowGame', ['game' => $game, 'gameComments' => $gameComments]);
    }

Page B:

<div class="flex flex-row h-40">
    <div class="flex items-center justify-center w-20 border xl:w-40">
        <div class="p-1">
            <img :src="game.image" />
        </div>
        <div class="flex items-center justify-center w-16 border xl:w-40">
             <div class="text-xs xl:text-2xl">
                  {{ game.title }}
            </div>
       </div>
    </div>

</div>

What confuses me is that page B shows the game.title. I ask for help. Thanks

I tried outputting "game" on side B via a foreach loop. But that led to errors. And it really isn't necessary.

  • What is the value of `game.image`? I'd bet you have a relative pathing issue; make sure you're giving an absolute URL, or run it through the `asset()` helper. – ceejayoz Mar 29 '23 at 11:46
  • Both pages output the following string. "storage\/dev\/images\/app\/pic_6.jpg" – Nervendolch Mar 29 '23 at 12:28
  • That's your problem, then. If you are on `https://localhost/foo/something`, it will result in `https://localhost/**foo**/storage/dev/images/app/pic_6.jpg`, because it's a relative path. Use the `asset()` helper. – ceejayoz Mar 29 '23 at 12:34
  • I do not understand this. Vue displays the images as I have it on page A. Why does it have to be on Page B via asset()? – Nervendolch Mar 29 '23 at 12:43
  • Because the URL of your page matters if your `game.image` value is a relative URL. https://stackoverflow.com/questions/2005079/absolute-vs-relative-urls – ceejayoz Mar 29 '23 at 12:51
  • Ok. Why does it work on Page A? – Nervendolch Mar 29 '23 at 12:56
  • If Page A's URL is something like `http://localhost/` or `http://localhost/foo`, the relative URL and the absolute URL are the same; the browser doesn't consider the page to be "in a folder". – ceejayoz Mar 29 '23 at 12:58
  • Thank you. I will get back to you. – Nervendolch Mar 29 '23 at 13:00
  • Unfortunately I did not get the problem solved. Can you show me how to write the code exactly? I have switched from Blade to Vue. In Blade I have the images displayed like this – Nervendolch Mar 30 '23 at 04:52
  • You'd want to do the `asset()` stuff in your controller; modify `$game` before sending it to the frontend. – ceejayoz Mar 30 '23 at 11:35
  • Thank you. I have solved it this way for now. – Nervendolch Mar 31 '23 at 04:24

0 Answers0