Im a beginner with laravel. Im following the laracasts tutorial and Im stuck at the part where you access another page with a form by:
{{ Form::open(['url' => 'created']) }}
for example.
Now that leads me to the right url but it gives me
Whoops, looks like something went wrong.
As soon as I type the link manually it works normally.
This is the code of the page where it directs to:
controller:
public function created()
{
return 'hello';
}
Routes:
Route::get('created', 'TestController#created');
View:
#extends('layout')
#section('content')
<h1> Test </h1>
#stop
This is the form of the 1st page:
#extends('layout')
#section('content')
<h1>Create New User</h1>
{{ Form::open(['url' => 'created']) }}
<div>
{{ Form::label('email', 'E-mail:')}}
{{ Form::text('email')}}
</div>
<div>
{{ Form::label('password', 'Password:')}}
{{ Form::password('password')}}
</div>
<div>
{{ Form::submit('Create')}}
</div>
{{ Form::close()}}
#stop
What is going wrong here?
Form open by default links to a post method so what you need is either a post route or a get method. Following should work:
{{ Form::open(['url' => 'created']) }}
// Insert your fields/codes here
{{ Form::close() }}
//Change route method to post
Route::post('created', 'TestController#created');
Please read the documentation here for more details.
Related
Sometimes blade echo doesn't print and I don't know why.
With this code visits are not printed
#extends('layouts/base', ['title' => "$url->new"])
#section('content')
<h1>Link Shortened !</h1>
{{ $url->new }}
<h3>visits {{ $url->visitsĀ }}</h3>
#endsection
With this code (removing the space after visits) the number of visits is printed
#extends('layouts/base', ['title' => "$url->new"])
#section('content')
<h1>Link Shortened !</h1>
{{ $url->new }}
<h3>visits {{ $url->visits}}</h3>
#endsection
It should work when you replace "$url->new" to $url->new in the #extends lines.
(answered in the comments by #Jinal)
I'll copy the relevant code and then i'll explain the problem.
routes.php
get('/movies/create', 'MovieController#create');
Moviecontroller.php
public function create()
{
return view('movies.create');
}
master.blade.php
<html>
<head>
<title>Laravel</title>
</head>
<body>
#yield('content')
</body>
</html>
edit.blade.php
#extends('master')
#section('content')
<h1>Edit {{ $movie->name }}</h1>
{!! Form::model($movie, ['url' => '/movies/' . $movie->id, 'method' => 'PATCH']) !!}
{!! Form::text('name') !!}
{!! Form::text('director') !!}
{!! Form::textarea('description') !!}
{!! Form::text('rating') !!}
{!! Form::submit('Update movie') !!}
{!! Form::close() !!}
#stop
show.blade.php
#extends('master')
#section('content')
<h1>{{ $movie->name }}</h1>
<h4>Director: {{ $movie->director }}</h4>
<h4>Rating: {{ $movie->rating }}</h4>
<p>Description: {{ $movie->description }}</p>
{!! link_to('/movies/' . $movie->id . '/edit', 'Edit') !!}
#stop
so i have this code, but when i go to /movies/create in the browser, it's trying to open show.blade.html, which, of course, will throw an exception ($movie does not exist). Why does that happen?
You probably have a conflicting route above the one you showed, something like this:
get('/movies/{movie}', 'MovieController#show');
get('/movies/create', 'MovieController#create');
So when you go to yoursite.com/movies/create in your browser, the first route is triggered, and the controller opens show.blade.php - but there is no movie for it to show yet.
If you move them the other way around, the create method will work as intended, and you'll still be able to show existing movies:
get('/movies/create', 'MovieController#create');
get('/movies/{movie}', 'MovieController#show');
best way:
php artisan make:controller MovieController
and define route
Route::resource('movies', 'MovieController');
//available routes
Verb Path
GET /movies
GET /movies/create
POST /movies
GET /movies/{id}
GET /movies/{id}/edit
PUT/PATCH /movies/{id}
DELETE /movies/{id}
I have created a Form in a blade template. This form sends a POST request to search.postQuery, so I can get the search query and then do something with it and return a View.
This is the route I have defined:
Route::post('/search/{query}', ['as' => 'search.postQuery', 'uses' => 'SearchController#postQuery'])->where('query', '[a-zA-Z0-9]+');
My form looks like this:
{{ Form::open(array('method' => 'POST', 'route' => array('search.postQuery')) }}
{{ Form::text('searchQuery') }}
{{ Form::submit('Zoeken!') }}
{{ Form::close() }}
This is the method the route calls on POST:
public function postQuery($query)
{
var_dump("Landed here");
}
And finally, the error Laravel poses me with is a NotFoundHttpException.
I also discovered that Laravel is constructing a rather strange URL when I press submit: http://homestead.app/search/%7Bquery%7D
What am I doing wrong? As to my knowledge, I'm not doing something very strange?
This is your error
{{ Form::open(array('route'=>'search.postQuery','method' => 'POST')) }}
{{ Form::text('searchQuery') }}
{{ Form::submit('Zoeken!') }}
{{ Form::close() }}
I am trying to update information in my database with Laravel. Not sure what I am doing wrong but I can't seem to find where the problem is. Here is the code for my edit page (This is the page where I would edit information taken from my DB).
{{ Form::open(['url'=>'portfolio/update']) }}
<div>
{{ Form::label('portfolio_title', 'Portfolio Title:') }}
{{ Form::text('portfolio_title',$work->portfolio_title) }}
{{ $errors->first('portfolio_title','<span class="error">:message</span>') }}
</div>
<div>
{{ Form::label('portfolio_description', 'Portfolio Description') }}<br>
{{ Form::textarea('portfolio_description', $work->portfolio_description, ['size' => '50x5']) }}
{{ $errors->first('portfolio_description','<span class="error">:message</span>') }}
</div>
<div>
{{ Form::label('portfolio_content', 'Portfolio Content') }}<br>
{{ Form::textarea('portfolio_content', $work->portfolio_content, ['size' => '50x5']) }}
{{ $errors->first('portfolio_content','<span class="error">:message</span>') }}
</div>
{{ Form::hidden('id',$work->id) }}
<div>
{{ Form::submit('Update Work') }}
</div>
{{ Form::close() }}
I have a controller called PortfolioController that will save info to database and what not.
public function edit($work_title){
$work = Portfolio::wherePortfolio_title($work_title)->first();
return View::make('portfolio/edit', ['work' => $work]);
}
public function update(){
$id = Input::get('id');
$input = Input::except('id');
if( !$this->portfolio->fill($input)->isValid()){
return Redirect::back()->withInput()->withErrors($this->portfolio->errors);
}
$work = Portfolio::find($id);
$work->portfolio_title = Input::get('id');
$work->save();
}
Here is my route that I am working with:
Route::resource('portfolio','PortfolioController');
Route::post('portfolio/update','PortfolioController#update');
I am able to get the form populated with the correct information but when i change something like the title and click update, the page reloads but does not save in the DB. Sometimes I will get an MethodNotAllowedHttpException error. This has been pretty frustrating for me so any help will be greatly appreciated.
Why don't you just actually use your resource route?
First, remove the portfolio/update route. You don't need it.
Then change your Form::open to this:
{{ Form::open(['route' => ['portfolio.update', $work->portfolio_title], 'method' => 'put']) }}
This way you target the update method in your RESTful controller.
Finally change that to use the portfolio_title as identifier and you should be able to remove the hidden id field from your form.
public function update($work_title){}
Please take a look at:
RESTful controllers
Opening a form
I have problem with action in {{ Form::open() }}
Route [adm/tagedit] not defined.
My code in view is
{{ Form::open(['method'=>'post','action'=>"adm/tagedit"])}}
{{ Form::submit('edit',['class'=>'btn btn-default']) }}
{{ Form::close() }}
and in routes
Route::get('adm/{action?}/{params?}',function($action,$params=null){
if(Auth::check()==false||Auth::user()->isAdmin()==false){
return \Illuminate\Support\Facades\Redirect::to('/')->withError('Youe need be logged in');
}
return (new AdmController())->{$action}($params);
});
//routing for bacend post method
Route::post('adm/{action?}/{params?}',['before'=>'csrf',function($action,$params=null){
if(Auth::check()==false||Auth::user()->isAdmin()==false){
return \Illuminate\Support\Facades\Redirect::to('/')->withError('You need be logged in');
}
return (new AdmController())->{$action.'Post'}($params);
}]);
Of course if I use get request action work. In controller I use action "tageditPost"
Probably my problem is similar to Rediredt:route() is not work too. But in redirect I use Reditect:to() and work fine. In form, I don't know what should I change.
Thanks in advance for answers.
Regards
Try the following:
{{ Form::open(['method'=>'post','url'=>"adm/tagedit"]) }}
{{ Form::submit('edit',['class'=>'btn btn-default']) }}
{{ Form::close() }}