how to pass variable in route name in laravel? - php

I have defined route
Route::get('/edit-industry/{id}', 'Industries#edit')->name('admin.editIndustry');
And passing variable by
{{ route('admin.editIndustry', ['id'=>1]) }}
OR
{{ route('admin.editIndustry', [1]) }}
This is not working. How to pass variable here?

wow, why are wrong answers (or answers for questions which were not asked in this case) upvoted?
EkinOf is correct, you can do
{{ route('admin.editIndustry', 1) }}
Btw your first one works too and is necessary, if you have more than 1 parameter
{{ route('admin.editIndustry', ['id'=>1]) }}
{{ route('admin.editIndustry', ['id'=>1, 'something'=>42]) }}

If you have only one parameter you can do that :
{{ route('admin.editIndustry', 1) }}

Passing single parameter:
##Defining Route:##
Route::get('edit-industry/{id}', ['as' => 'admin.editIndustry', 'uses' => 'Industries#edit']);
##Calling Route:##
{{ route('admin.editIndustry',[$id]) }}
Passing multiple parameter:
##Defining Route:##
Route::get('edit-industry/{id}/{step}', ['as' => 'admin.editIndustry', 'uses' => 'Industries#edit']);
##Calling Route:##
{{ route('admin.editIndustry',[$id, $step]) }}

Simply try like this
View
{{URL::to('/edit-industry/1')}}
Route
Route::get('/edit-industry/{id}', 'Industries#edit')
Controller
public function edit($id){
// use $id here
}
Hope you understand.

Using generating URLs from Named Routes route():
{{ route('admin.editIndustry', 1) }}
Using URLs url():
{{url('/edit-industry', [1])}}

You can Directly pass with route name
{{ URL::to('/edit-industry/1') }}

Related

How to build path to route in Laravel with parameter?

I have this route
Route::get('org/edit/{id}/', ['as' => 'org.edit', 'uses' => 'OrgController#edit']);
And then create a link to this route by using Laravel Blade template:
<span class="glyphicon glyphicon-edit"></span>
What I expect to see:
/org/edit/123/
What I get:
/org/edit?123
What am I doing wrong?
Try passing it as a key value pair.
{{ route('org.edit', ['id' => $org->id]) }}
Dmitriy,
in this case you can try two ways.
{{ route('org.edit', ['id' => $org->id,]) }}
{{ route('org.edit', $org->id) }}
Check this out. :)

How do I use the POST of Laravel's Route::resource?

Below is my code for a Laravel 4 project.
Going to the authors/create URL and submitting the form gives me a 405 error.
However, if I prepend the routes.php file with Route::post('authors/store', 'AuthorsController#store');, basically doubling what it already should do, everything works like a charm!
Why do I need do prepend said line in my code to work? I can only assume I'm doing something wrong here.
routes.php:
Route::resource('authors', 'AuthorsController');
AuthorsController.php:
public function create() {
$view = View::make('authors.create');
return $view;
}
public function store() {
//
}
authors/create.twig:
{{ form_open({'url':'authors/store'},{"method" : "post"}) }}
<p>
{{ form_label("Name", "name") }}
{{ form_text("name") }}
</p>
<p>
{{ form_submit("Add Author") }}
</p>
{{ form_close() }}
The store action get's trigger when you POST to the resource. So just authors and not authors/store:
{{ form_open({'url':'authors'},{"method" : "post"}) }}
See this table on more information what URL corresponds to what controller action.
Also I think it should be like this:
{{ form_open({'url':'authors', 'method' : 'post'}) }}
And you can pass the route name Laravel automatically generates to make your life a bit easier:
{{ form_open({'route':'authors.store', 'method' : 'post'}) }}
Oh and one more, post is the default method so this should do as well:
{{ form_open({'route':'authors.store'}) }}

Changing a href to laravel anchor tag

Here is my regular code in the blade
{{ $Blog->BlogTitle }}
I want to do something like
{{ HTML::link('http://test.com', null, array('id' => 'linkid'))}}
I mean i want to do it in the laravel way..
How can i do this ?
Try the following code
{{ HTML::link(url().'/blog'.$Blog->id, $Blog->BlogTitle)}}

Laravel4: delete a comment

I have a simple blog with Post resource and Comment nested resource.
Until now I can see all the comments belonging to a post and create a new comment for a post.
I want to give the possibility to delete a specific comment, but somehow I am making some mistakes.
This is the view comments.index with all the comments:
#extends('master')
#section('blog')
#foreach($comments as $comment)
<div class="span11 well">
<ul>
<li><strong>Body: </strong> {{ $comment->body }} </li>
<li><strong>Author: </strong> {{ $comment->author }}</li>
</ul>
{{ Form::open(array('method' => 'DELETE', 'route' => array('posts.comments.destroy', $post_id), $comment->id)) }}
{{ Form::submit('Delete', array('class' => 'btn btn-danger')) }}
{{ Form::close() }}
</div>
#endforeach
{{ link_to_route('posts.index', 'Back to Post index') }}
This is the error i get running the index: Parameter "comments" for route "posts.comments.destroy" must match "[^/]++" ("" given) to generate a corresponding URL.
This is the Index method inside CommentsController:
public function index($post_id)
{
$comments = Post::find($post_id)->comments;
return View::make('comments.index', compact('comments'))->with('post_id', $post_id);
}
And this is the Destroy method inside CommentsController:
public function destroy($post_id, $comment_id)
{
$comment = $this->comment->find($comment_id)->delete();
return Redirect::route('posts.comments.index', $post_id);
}
Someone can tell me please where I am making the mistake?
This the routes:
Route::resource('posts', 'PostsController');
Route::resource('posts.comments', 'CommentsController');
You have put a regexp tester on your route, to check your comments parameter.
This error message says that parameter that you give to Laravel isn't good.
If your parameter is only a decimal id, use \d+ regexp instead.
Without your routes.php file - I cant be sure, but I think this might be the problem.
Change
{{ Form::open(array('method' => 'DELETE', 'route' => array('post.comments.destroy', $post_id), $comment->id)) }
to
{{ Form::open(array('method' => 'DELETE', 'route' => array('post.comments.destroy', array ($post_id, $comment->id))) }
If this does not work - please post your routes.php file.
Edit: You have defined your route as a "resource". This means your destroy route is defined with only one variable. You dont actually need the $post included, so just define this:
{{ Form::open(array('method' => 'DELETE', 'route' => array('posts.comments.destroy', $comment->id))) }}
and change your destroy method to this - there is no need for the $post to delete a $comment:
public function destroy($comment_id)
{
$comment = $this->comment->find($comment_id)->delete();
return Redirect::back();
}

Laravel: how do I pass a value from a form to a controller?

I have a form:
{ Form::open(array('action' => 'RatesController#postUserRate', $client->id)) }}
{{ Form::text('rate', '', array('placeholder' => 'Enter new custom client rate...')) }}
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
How do I pass my $client->id value through the form to my controller method?
I currently have a controller method that looks like this:
public function postUserRate($id)
{
$currentUser = User::find(Sentry::getUser()->id);
$userRate = DB::table('users_rates')->where('user_id', $currentUser->id)->where('client_id', $id)->pluck('rate');
if(is_null($userRate))
{
...
}else{
....
}
}
And the error log says "Missing argument 1 for RatesController::postUserRate()"
Any ideas on how to pass this $client->id into my controller so I can use it as I want to above?
Add {{ Form::hidden('id', $client->id) }}to the form. Then, when it's posted, you can fetch its value per usual with Input::get('id').
Also, remove the postUserRate method's argument.
You simply use :
Form::open(array('action' => array('Controller#method', $user->id)))
the variable $user->id is passed as argument to the method method, also this last one should recieve an argument as well, like so : method($userId)
Source : Laravel documentation

Categories