I want to link to my route in blade. How can i do this?
<a href="roue" > go first</a>
if you define Route name you can use that in your blade :
define Route Name :
Route::get('/admin/transfer/forms-list', [
'as' => 'transfer.formsList',
'uses' => 'Website\TransferController#IndexTransferForms'
]);
now you can use that in your blade like this :
<a href="{{URL::route('transfer.formsList')}}" type="submit">
go first</a>
Of course if you use form collective you can use this :
{!! link_to_route('route.name', 'go first') !!}
There are few ways to use routes in a view.
The simplest one is using route() helper:
go first
If you're using Laravel Collective HTML & Forms, it will create full link, not only path to the route:
{!! link_to_route('route.name', 'go first') !!}
Or:
{!! HTML::linkRoute('mainpage', 'hey') !!}
Using URL facade. Wouldn't recommend, because you're getting redundant code:
go first
Related
Hi I'm using laravel forms.
{!! Form::text('product_name', null, array('class' => 'form-control date_pick')) !!}
Is there a way to set this default input from controller?. Think we can do it with Flash but I couldn't find an example. I want to take vlues from a model and prepopulate.
$products = Products::all();
It would be great if someone know how to do this. What is the easiest way to do it?
This is done with a basic edit route where you simply use:
In ModelController you would call the edit page:
public function edit(ModelName $model) {
return view('name.of.the.blade.view', compact('model')); // if using compact then without dollar symbol
}
In blade view simply make the form with all the input fields you have for that model:
{!! Form::model($model, ['method' => 'PATCH', 'route' => ['model.update', $model->id],]) !!}
Now all the form fields will have the values from that model.
{!! Form::close() !!}
And the edit and update route (inside routes/web.php) would be like this:
Route::get('/model/{model}/edit', 'ModelController#edit')->name('model.edit');
Route::patch('/model/{model}', 'ModelController#update')->name('model.update');
I am using same thing but result is different. In posts.blade.php i can call like this
<a href = "posts/{{post->id}}"edit>Edit</a>
But when i use same thing in rooms.blade.php indisde rooms folder i had to use like this.
<a href = "{{$room->id}}/edit" class = 'btn btn-primary'> Edit</a>
It is really confusing me. Any solution? i want to add link for editing post inside room. I have tried many things but i dont understand.
My Route
Route::get('/','PagesController#index');
Route::get('/about', 'PagesController#about');
Route::get('/services', 'PagesController#services');
Route::get('/register', 'PagesController#register');
Route::get('/logout', 'PagesController#logout');
Route::get('/posts', 'PostsController#posts');
Route::resource('posts','PostsController');
Route::resource('rooms','RoomsController');
Auth::routes();
Route::get('/dashboard', 'DashboardController#index');
You can give the name of the route
Foe example:
Route::get('/post/{id}', 'PostController#showPost')->name('post.show');
And then call the blade
Link to Resource {{ $id }}
In Django, I can do this:
Account Link
which would give me domain/account/login where I have that URL named in my urls.py
url(r'^account/login/$', views.Login.as_view(), name='account_login'),
I want to do something similar in Laravel 5.2
I currently have something like this:
Route::get('/survey/new', ['as' => 'new.survey', 'uses' => 'SurveyController#new_survey']);
How do I use in my template, plus passing in parameters?
I came across this: https://laravel.com/docs/5.1/helpers, but it was just a piece of a white page without relevant content of how to actually use it.
You can use the route() helper, documented here.
My Link
If you include laravelcollective/html you could use link_to_route(). This was originally part of the core but removed in Laravel 5. It's explained here
{!! link_to_route('new.survey', 'My Link') !!}
The Laravel Collective have documented the aforementioned helper here. The function prototype is as follows
link_to_route($routeName, $title = null, $parameters = [], $attributes = []);
If for example you wanted to use parameters, it accepts an array of key value pairs which correspond to the named segments in your route URI.
For example, if you had a route
Route::get('surveys/{id}', 'SurveyController#details')->name('detail.survey');
You can generate a link to this route using the following in the parameters.
['id' => $id]
A full example, echoing markup containing an anchor to the named route.
{!! link_to_route('new.survey', 'My Link', ['id' => $id]) !!}
I working on a simple laravel 5.2 application. I want to pass a value $id from the view when a button is clicked to the controller method destroy(). This is what i have tried:
This is my route
Route::get('delete/{id}', array('as' => 'delete', 'uses' => 'ContactsController#destroy'));
... and this is my button in a view:
Delete
But this code didn't work. Thanks for any help.
According to the documentation you should be able to do this:
{{ action('ContactsController#destroy', ['id' => $contacts->id]) }}
Your route is expecting an id so you don't need to pass an identifier. The below code should work for you.
Make sure you're using the correct brackets as by default in Laravel 5.2 you'll need to use {!! !!} instead of {{ }}.
Delete
Since it is a GET request, you can simply have you own href instead of using blade. I just find this more readable:
Delete
I am having trouble figuring out why my data is not being posted and stored in my database. I have used the resource routes for another form and it works fine, but here for some reason it won't work. Clicking submit just seems to refresh the page, no errors to work from!
So I have a form which gets the workout routines from a database, and on submission I want this to create a new Workout "session" in my database table (called "Workouts"). The form is this:
{{ Form::open(array('url' => '/')) }}
<div class="form-group">
{{ Form::text('workout_name', Input::old('workout_name'), array('class' => 'form-control', 'placeholder' => 'Session Name')) }}
</div>
<div class="form-group">
{{ Form::select('routines', $routine_names, null, array('class' => 'form-control')) }}
</div>
{{ Form::submit('Select Routine', array('class' => 'btn btn-success pull-right')) }}
{{ Form::close() }}
In my HomeController I have this:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Routine;
use App\Workout;
class HomeController extends Controller
{
public function index()
{
$routines = Routine::all();
$routine_names = Routine::lists('routine_name');
return view('workout')->with(array('routines'=>$routines, 'routine_names'=>$routine_names));
}
public function store()
{
$workout = new Workout;
$workout->workout_name = Input::get('workout_name');
$workout->save();
}
}
I have a model created for the Workout, and the route for this page is the following:
Route::resource('/', 'HomeController');
I can't figure out where I'm going wrong. The index method in my controller is working, as it is returning the correct view with the data I need. The form also looks OK I think, as I'm posting to the same page, but submitting doesn't seem to carry out the code I have in the store method of the HomeController.
Any help would be appreciated!
Thanks :)
Change your route declaration from:
Route::resource('/', 'HomeController');
To something like this:
Route::resource('/workout', 'WorkoutController');
If you are using the resources controller creator command of php artisan then all the specific routes are created for you. To see all listed routes you can type , php artisan routes. This will show you RESTFUL routes even for your POST method .
And also even you did not created the resources controller and did made the routes with manual way then you can create ,
Route::POST('/workout' , SomeController#post);
I am trying to say , you have to use the different POST method for the form submission .
Hope this will solve your problem . Thanks.