I am new to laravel.I am trying to create a basic form in my form.blade.php page.But when i try to navigate to my form.blade.php page i am getting the following error:
FatalErrorException in 5fca28a34b9eeee32d99bfd5ad77d6a463cb98c9.php
line 23: syntax error, unexpected 'put' (T_STRING), expecting ')'
in my route.php i am calling the View::make() method to get to the form.blade.php page
Route::get('/',function(){
return View::make('form');
});
form.blade.php page::
<!DOCTYPE html>
<html>
<body>
{{Form::open(array('url'=>'thanks'))}}
{{Form::label('email','Email Address')}}
{{Form::text('email')}}
{{Form::label('os','operating system')}}
{{Form::select('os',array(
'linux'=>'Linux',
'mac'=>'Mac',
'windows'=>'windows'
))}}
{{Form::label('comment','Comment')}}
{{From::textarea('comment','',array('placeholder=>'put your comment here'))}}
{{Form::checkbox('agree','yes',false)}}
{{Form::label('agree','i agree with your terms and condition')}}
{{Form::submit('submit')}}
{{Form::close()}}
</body>
</html>
{!! Form::open(array('url'=>'thanks')) !!}
{!! Form::label('email','Email Address') !!}
{!! Form::text('email') !!}
{!! Form::label('os','operating system') !!}
{!! Form::select('os',array(
'linux'=>'Linux',
'mac'=>'Mac',
'windows'=>'windows'
)) !!}
{!! Form::label('comment','Comment') !!}
{!! Form::textarea('comment','',array('placeholder'=>'put your comment here')) !!}
{!! Form::checkbox('agree','yes',false) !!}
{!! Form::label('agree','i agree with your terms and condition') !!}
{!! Form::submit('submit') !!}
{!! Form::close() !!}
Above is the correct way to use form/html, here is the documentation laravelcollective/html
{{From::textarea('comment','',array('placeholder=>'put your comment here'))}}
should be
{{From::textarea('comment','',array('placeholder'=>'put your comment here'))}}
Seems like you are missing a single quote.
Related
Good day to all, I am new to Laravel framework and as I read some articles about HTTP verbs which say that POST is universal to both PUT and DELETE (reference:
https://softwareengineering.stackexchange.com/questions/114156/why-are-there-are-no-put-and-delete-methods-on-html-forms
) and thus I wonder if it is possible to achieve Update and Delete functionality with POST and without using hidden() in the view itself. For example, if you look at the code below hidden method is used to clarify which HTTP verb should be used in this case PUT and what if I remove it and try to update the data will it be possible.
The code:
#extends('layouts.app')
#section('content')
<h1>Income</h1>
<br>
{!! Form::open(['action'=>['IncomeController#update',$income->id], 'method' =>'POST']) !!}
<div class="form-group">
{{Form::label('Title', 'Title')}}
{{Form::text('Title', $income->Title, ['class' =>'form-control', 'placeholder' =>'Enter title'])}}
</div>
**{{Form::hidden('_method', 'PUT')}}**
{{Form::submit('Create', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
#stop
suppose if you have Route like this
Route::put('income/update/{id}', 'IncomeController#update');
Route::delete('income/delete/{id}', 'IncomeController#delete');
Then you can replace your route with this
Route::post('income/update/{id}', 'IncomeController#update');
Route::post('income/delete/{id}', 'IncomeController#delete');
And then change your form like this (Just remove the hidden field)
#extends('layouts.app')
#section('content')
<h1>Income</h1>
<br>
{!! Form::open(['action'=>['IncomeController#update',$income->id], 'method' =>'POST']) !!}
<div class="form-group">
{{Form::label('Title', 'Title')}}
{{Form::text('Title', $income->Title, ['class' =>'form-control', 'placeholder' =>'Enter title'])}}
</div>
{{Form::submit('Create', ['class'=>'btn btn-primary'])}}
{!! Form::close() !!}
#stop
I'm trying to create a form that list the cars in a cars table and if clicked, sends into another form which is based on a DB query that returns the data of the chosen car (identified by $modelesc). This form sends the data to a "orders" table.
I'm now getting the following error in orders.blade.php:
Parse error: syntax error, unexpected '<', expecting ']'
I don't understand why I'm getting '<'; I don't see this in the code!
This is my code so far:
CarController
function catalog() {
$cars = DB::table('cars')->get();
return view('catalog', compact('cars'));
}
function orders($modelesc=null) {
$cars = DB::table('cars')->where('Model', '=', '$modelesc');
return view('orders', compact('cars'));
}
Catalog.blade.php
#foreach($cars as $car)
{!! Form::open(array('action' => 'CarController#orders', 'method' => 'GET')) !!}
{!! Form::hidden('$modelesc', $car->Model) !!}
{!! Form::submit($car->Model) !!}
{!! Form::close() !!}
#endforeach
Orders.blade.php
{!! Form::open(array('action' => 'index', 'method' => 'POST')) !!}
{!! Form::text('Model', $car->Model) !!}
{!! Form::hidden(users_id, Auth::user()->id) !!}
{!! Form::hidden(Fabrication_date, date(Y-m-d)) !!}
{!! Form::select('Colour', [
#foreach($colours as $colour)
'$colour->Colour' => '$colour->Colour'
#endforeach
]) !!}
{!! Form::hidden(Order_status_id, '1' !!}
{!! Form::close() !!}
This is the structure of the orders table. The *_id fields come from other tables, and I want to fill some values of the forms with the relevant entry (id, users_id, Model, Fabrication_date, Colour_id, Order_status_id).
Firstly you need to wrap the Form names within quotes, the Order Status ID was also missing a closing bracket:
{!! Form::hidden('users_id', Auth::user()->id) !!}
{!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}
{!! Form::hidden('Order_status_id', '1') !!}
Next, if $colours is a collection you can do the following in Laravel 5.4 (I'm unsure which version you're using)
{!! Form::select('Colour', $colours->pluck('Colour')) !!}
If you're on Laravel 5.1 or prior, you'll do the following:
{!! Form::select('Colour', $colours->lists('Colour')) !!}
This is because the lists method was removed in 5.2.
#pseudoanime is also correct with his answer, the database call needs the get method adding
try changing $cars = DB::table('cars')->where('Model', '=', '$modelesc');
$cars = DB::table('cars')->where('Model', '=', $modelesc)->get();
Your orders method in your CarController doesn't set $cars to what you'd think; without calling get(), it'll be an instance of Builder instead of an array! Furthermore, you're only fetching cars where the model equals the string "$modelesc" instead of the value of $modelesc.
Furthermore, in your orders.blade.php file, you seem to reference a $colours variable, but that doesn't get passed down in your CarController.
Change your orders method to the one below:
public function orders($modelesc = null)
{
$cars = DB::table('cars')
->where('Model', $modelesc)
->get();
$colours = DB::table('colours')->get()->pluck('Colour');
return view('orders', compact('cars', 'colours'));
}
There is also an issue with your Form::select call in your orders.blade.php file; the Blade template directives (#foreach, #endforeach) are only for the "HTML" portion of the Blade file. Since we called pluck() on the Collection of colours in the CarController, we can simply pass in $colours for that method.
{{-- Incorrect --}}
{!! Form::select('Colour', [
#foreach($colours as $colour)
'$colour->Colour' => '$colour->Colour'
#endforeach
]) !!}
{{-- Correct --}}
{!! Form::select('Colour', $colours) !!}
You also appear to be using constants in your Form builders (instead of strings with quotes); these should be strings only.
{!! Form::open(['action' => 'index', 'method' => 'POST']) !!}
{!! Form::text('Model', $car->Model) !!}
{!! Form::hidden('users_id', Auth::user()->id) !!}
{!! Form::hidden('Fabrication_date', date('Y-m-d')) !!}
{!! Form::select('Colour', $colours) !!}
{!! Form::hidden('Order_status_id', '1') !!}
{!! Form::close() !!}
I have only two forms for the moment in my view, they are both post method I tried to solve it like this
route
Route::post('view', function(){
if(Input::has('form1')){
'NameController#method1';
} elseif (Input::has('form2')){
'NameController#method2';
}
});
view
{!! Form::open(array('url' => '/view')) !!}
{!! Form::text('text', $trans->text) !!}
{!! Form::submit('Submit', array('name' => 'form1' )) !!}
{!! Form::close() !!}
{!! Form::open(array('url' => '/view')) !!}
{!! Form::text('text', 'text') !!}
{!! Form::submit('Submit', array('name' => 'form2')) !!}
{!! Form::close() !!}
And it throw's out this error
syntax error, unexpected ''ConfigurationController#title' (T_CONSTANT_ENCAPSED_STRING)
it was error in coding I corrected it but it won't do what I wish it simply returns blank screen it doesn't cycle trough controllers
I modified the code(removed return and closed the route)
What you want to do is create a method someMethodName in the NameController and in there
public function someMethodName()
{
if(Input::has('form1')){
$this->method1();
} elseif (Input::has('form2')){
$this->method2();
}
}
then replace all the route stuff with
Route::post('view', 'NameController#someMethodName')
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}
Form:
#section('form')
<div class="col-sm-4">
{!! Form::open() !!}
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', 'Enter your name', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('contact', 'Contact Number:') !!}
{!! Form::text('contact', 'Enter your contact number', ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('location', 'Location:') !!}
{!! Form::select('location', $locations, null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('service', 'Service Required:') !!}
{!! Form::select('service', $services, null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Just go Befikr >>', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
</div>
#stop
routes.php:
<?php
Route::get('/', 'PagesController#index');
Route::post('/', 'PagesController#store');
PagesController.php
<?php namespace App\Http\Controllers;
use App\service_location;
use App\service_type;
use App\service_request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Request;
class PagesController extends Controller {
public function index()
{
$locations = service_location::lists('location_area', 'location_id');
$services = service_type::lists('service_desc', 'service_id');
return view('home.index', compact('locations','services'));
}
public function store()
{
$input = Request::all();
return $input;
}
}
Unfortunately, the code neither seems to return $input on the screen (implying that its not executing the store() function at all), nor does it throw any error/exception.
Can anyone please let me know why the post method is not resulting in appropriate results here?
Edit 1
I attempted to directly return via the route's inline function, but even that didn't work. Hence, now I'm pretty confident that the post method is not getting triggered at all. This is what I did to verify it:
Route::post('/', function()
{
return 'Hello';
});
Well, I figured the answer to my own question here.
Turns out, that in order to be able to execute a post request, you need to explicitly deploy a PHP server as:
php -s localhost:<AnyFreePort> -t <YourRootDirectory>
Whereas when I attempted to run it the first time through XAMPP, I was attempting to run the application out of sheer laziness, directly as:
http://localhost/myproject/public
which worked fine for a get request.
Even I had same issue, but your solution wasn't helpful for me since I was using Apache server instead of built in PHP server
then I found this solution which says just enter space to form action, weird issue and solution but it worked for me...
Eg.
{!! Form::open(array('url' => ' ', 'method' => 'post')) !!}