How to show post using another column than ID

Published 8 months ago by pickab00

I have a table called Post and i want to show the different single posts using the show method. But i want to use my post_id instead of id.

The thing is i am using id for updating my posts. but i want to use post_id to show the posts. when i try:

      <a href={{route('post.show', $post->post_id)}}>{{$post->name}}</a>

it gives me 404. But when i use $post->id it works. Does it has to do with id being the primary key? how can i achieve this?i want to update the posts using id but show the posts using post_id column.

thanks

Best Answer (As Selected By pickab00)
calder12

In your update method check the logged in user vs the post owner. For example if your post has a user_id you could do something like this

if( Auth::user()->id === $post->user_id ) {
 //Do your update code here
} else {
  //Return some error here
}


topvillas
$post = Post::where('post_id', $id)->first();
pickab00

@topvillas Sorry i updated my question. Anchor was not showing before. I removed "" from href. Theres the original question now.

topvillas

It's probably something to do with model binding.

Show us your route and controller.

pickab00

Here is my controller:

public function displayAll(){

        $posts = Post::all()->where('user_id', '=', Auth::user()->id);

        return view('/user-dashboard/post/all-post', compact('posts'));


    }

Here is the route:

Route::get('/dashboard/posts/show/{post_id}', '[email protected]')->name('post.show');

and the above question is blade.

On show method i am simply returning a testing string for now.

topvillas

The signature of the show method is important, please show us it,

pickab00

This is the show method:

public function show($id)
    {
        return view('/user-dashboard/post/show');
    }
topvillas

Hmm!

You need to pass an array as the second parameter of the route helper.

route(route('post.show', ['id' => $post->post_id])
pickab00

Sorry but is it suppose to be like this?

route(route('post.show', ['id' => $post->post_id])

['id' -> $post->post_id] to ['id' => $post->post_id]

Because it is giving me error on that part and also closing bracket

topvillas

You can figure out the syntax. The important part is passing an array as the second parameter.

pickab00

Route [http://test.io/dashboard/posts/show] not defined

Why so?

Updated version:

<a href="{{route(route('post.show', ['id' => $post->post_id]))}}">{{$post->name}}</a>
pickab00

I just want to know why its displaying when i give the id on the url. but it does not work when i give post_id on url. why is that

calder12

Just out of curiosity why do you have a post_id and an id on post? What is the difference?

pickab00

They both are uuid

I want the id to be able to update post and post_id to show a post

calder12

I'm still confused, why do you need two ids? The method you're using shouldn't make any difference, both should identify the post by the same field. It sounds like you're over complicating things because there's something you're not quite understanding.

Anyway, maybe this will help.

https://medium.com/@steveazz/setting-up-uuids-in-laravel-5-552412db2088

pickab00

I want to show the post using post_id because if i show the post using uuid, a user might copy the uuid and go to inspect element and paste it on the form action and update some other post. If this is not the right way to achieve this, could you please suggest the right method.

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