Laravel collectivehtml secure route or url - php

in my view page I have this route:
{!! Form::open(['url' => 'forumcomment/' . $forum->slug, 'files'=>false, 'id' => 'qw-commentform' ,'class' => 'qt-clearfix']) !!}
<hr class="qt-spacer-s"><div class="input-field">
{!! Form::textarea('comment', null, ['class'=>'materialize-textarea', 'id'=>'my-editor', 'required'=>'required','aria-required'=>true]) !!}
<label for="comment" class="">Comment*</label></div>
<hr class="qt-spacer-s">
{!! Form::submit('Post Comment', array( 'class'=>'qt-btn qt-btn-primary qt-btn-xl' )) !!}
{!! Form::close() !!}
getting mixed content error How can I get a secure route?

add this to the boot method of your AppServiceProvider.
This loads all content over http on local development and https on production
$this->app['request']->server->set('HTTPS', $this->app->environment() != 'local');
and always change your environment to production on production.

Related

Laravel forms with models

I have this code for my form and it works well:
<div class="form-group">
<select class="form-control" id="exampleSelect1">
<option>Assign</option>
#foreach ($projects as $project)
<option>{{ $project->name }} </option>
#endforeach
</select>
</div>
but what I'm trying to do is this with a contact form:
<div class="form-group">
{!! Form::label('name', 'Project') !!}
{!! Form::select (#theforeach with the values :c ) !!}}
</div>
I'm using the use App\Http\Requests\ContactFormRequest; and I have been searching the way of doing it but there is to few examples on google.
Form is part of the Laravel Collective HTML library, you can find the documentation here, specifically you're looking for the Select documentation:
echo Form::select('size', ['L' => 'Large', 'S' => 'Small']);
You have a Collection of Project models, each with a name and (presumably) an id which you need to turn into a key -> value array for the select method which you can do using pluck:
{!! Form::select('project', $projects->pluck('name', 'id')) !!}
Then within your controller you'll be able to find the project that has been selected using find, e.g:
Project::find($request->project);
If you want your select options to works, you need to call it properly,
From controller,
// Example : This is for single view page
$list_of_options = Products::pluck('name','id');
return view('your_view_name',compact('list_of_options'));
// Example : If you want select dropdown in all page ( within the controller views) then,
use Illuminate\Support\Facades\View;
public function __construct(){
View::share('list_of_options',Products::pluck('name','id'));
}
Now in blade,
{{ dd($list_of_options); }} // Check if the values are comming in proper formate
{!! Form::select('name_of_the_select', $list_of_options, null ,[
'class' => 'form-control',
'id' => 'name_of_the_select'
]);
!!}
Here is the button with <i> or <span> inside of it :-
{{ Form::button(
'<span class="fa fa-play fa-1x"></span>',
[
'class'=>'btn btn-info',
'type'=>'button'
])
}}
From your question ( UPDATED )
<div class="form-group">
{!! Form::label('exampleSelect1', 'Project') !!}
{!! Form::select('projects', $projects, null ,[
'class' => 'form-control',
'id' => 'exampleSelect1',
'placeholder' => 'Please select project'
]);
!!}
</div>
I hope this helps. :)
Try this
<div class="form-group">
{!! Form::label('project', 'Project') !!}
{!! Form::select ('project', $projects->pluck('name')) !!}}
</div>
See docs https://laravel.com/docs/4.2/html#drop-down-lists
<div class="form-group">
{!! Form::label('project', 'Project') !!}
{!! Form::select ('project', $projects->pluck('name', 'id')) !!}}
</div>

Is it possible to achieve update or delete functionality with POST and without using hidden() in the view

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

Route not defined error in laravel

i'm using form collective in laravel for 2 difference form and got error. Here is my view
<div class="table-list-donor">
{!! Form::model($transactions, ['route' => ['admin.update',$transactions->id ],'file'=>true, 'id'=>'project-form','method'=>'POST']) !!}
#include('admin.patials.form',['title' => 'edit form','submit' => 'edit'])
{!! Form::close() !!}
</div>
and here is my route
<td class="action">
edit
</td>
error is Route [] not defined in route . please help!

Laravel View Composer

I'm following a tutorial for laravel and there is something that I don't understand. It's referred to using views composer.
-I have a template called from app/http/routes.php given an Url:
Route::get('cats/create', function(){return view('cats.create');});
this is cats/create.blade.php:
#extends('master')
#section('header')
<h2> Add a new cat </h2>
#stop
#section('content')
{!! Form::open(['url'=>'/cats']) !!}
#include('partials.forms.cat')
{!! Form::close() !!}
#stop
-This template includes another one (partial) which covers the HTML of a Form
<div class="form-group">
{!! Form::label('name','Name') !!}
<div class="form-controls">
{!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('date_of_birth', 'Date of Birth') !!}
<div class="form-controls">
{!! Form::date('date_of_birth', null, ['class' => 'form-control']) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('breed_id','Breed') !!}
<div class="form-controls">
{!! Form::select('breed_id', $breeds, null, ['class' => 'form-control']) !!}
</div>
</div>
{!! Form::submit('Save Cat', ['class' => 'btn-primary']) !!}
-Now uses a view composer in order to automatically fullfill the select element when this partial view is called. In AppServiceProvider:
public function boot(ViewFactory $view)
{
$view->composer('partials.forms.cat','Furbook\Http\Views\Composers\CatFormComposer');
}
4.- And this is that CatFormComposer
class CatFormComposer
{
protected $breeds;
public function __construct(Breed $breeds)
{
$this->breeds = $breeds;
}
public function compose(View $view) {
$breeds = $this->breeds;
$view->with('breeds', $breeds->lists('name', 'id'));
}
}
It works. What I don't get is, How is that $breeds parameter being passed to the constructor of the class?
All View Composers are resolved by laravel service container. So when you type-hint Breed $breeds laravel will use php reflection api to find the expected class and inject the instance of that class (in that case: Container will find Breed class , create new Breed and pass the instance on constructor).
Also for deeper understanding you can take look about service locator pattern

Post Method Not Working in Laravel

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')) !!}

Categories