Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a variable through url in laravel

I'm fairly new to laravel and I'm struggling to get the format of my url correct.

It formats as http://mysite/blog?category1 instead of http://mysite/blog/category1

These are the files I am using. Is there a way to put the route into the BlogController?

Route.php

Route::get('blog/{category}', function($category = null)
{
    // get all the blog stuff from database
    // if a category was passed, use that
    // if no category, get all posts
    if ($category)
        $posts = Post::where('category', '=', $category)->get();
    else
        $posts = Post::all();

    // show the view with blog posts (app/views/blog.blade.php)
    return View::make('blog.index')
        ->with('posts', $posts);
});

Blogcontroller

class BlogController extends BaseController {


    public function index()
    {
        // get the posts from the database by asking the Active Record for "all"
        $posts = Post::all();

        // and create a view which we return - note dot syntax to go into folder
        return View::make('blog.index', array('posts' => $posts));
    }
}

blog.index blade

@foreach ($posts as $post)

    <h2>{{ $post->id }}</h2>
    <p>{{ $post->name }}</p>
    <p>{{ $post->category }}</p>
     <h2>{{ HTML::link(
    action('BlogController@index',array($post->category)),
    $post->category)}}


@endforeach
like image 830
tom harrison Avatar asked Mar 18 '23 04:03

tom harrison


1 Answers

routes.php

Route::get('category', 'CategoryController@indexExternal');

*.blade.php print the completed url

<a href="{{url('category/'.$category->id.'/subcategory')}}" class="btn btn-primary" >Ver más</a>
like image 197
erajuan Avatar answered Mar 21 '23 04:03

erajuan