Including Sub-Views in Blade - laravel

I'm trying to include two sub-views ( 'login' and 'register' ) in the 'home' view which is like this:
#extends('master')
#section('content')
#include('auth.login')
<hr>
#include('auth.register')
#endsection
And the 'login' and 'register' views:
//register.blade.php
#extends('master')
#section('content')
{!! Form::open() !!}
<div class="form-group">
{!! Form::label('email', 'Email Address') !!}
{!! Form::email('email', null,
['class' => 'form-control',
'placeholder' => 'Email Address',
'required' => true]) !!}
</div>
<div class="form-group">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password',
['class' => 'form-control',
'placeholder' => 'Password',
'required' => true]) !!}
</div>
<div class="checkbox">
<label>
{!! Form::checkbox('remember', null, []) !!}
Remeber Me
</label>
</div>
{!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}
{!! Form::close() !!}
#endsection
//login.blade.php
#extends('master')
#section('content')
{!! Form::open() !!}
<div class="form-group">
{!! Form::label('email', 'Email Address') !!}
{!! Form::email('email', null,
['class' => 'form-control',
'placeholder' => 'Email Address',
'required' => true]) !!}
</div>
<div class="form-group">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password',
['class' => 'form-control',
'placeholder' => 'Password',
'required' => true]) !!}
</div>
<div class="checkbox">
<label>
{!! Form::checkbox('remember', null, []) !!}
Remeber Me
</label>
</div>
{!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}
{!! Form::close() !!}
#endsection
I've tried removing the master extension from the sub-views but it didn't work. Only one sub-view is being rendered. I can't figure out, why that's happening?

main.blade.php
#extends('master')
#section('content')
#include('auth.login')
<hr>
#include('auth.register')
#endsection
For the sub views you have to remove the #extends('master'). Then you have two options I can think of.
You can add the #parent directive.
register.blade.php
#section('content')
{!! Form::open() !!}
...
{!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}
{!! Form::close() !!}
#parent
#endsection
login.blade.php
#section('content')
{!! Form::open() !!}
...
{!! Form::close() !!}
#parent
#endsection
or remove the sections.
register.blade.php
{!! Form::open() !!}
...
{!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}
{!! Form::close() !!}
login.blade.php
{!! Form::open() !!}
...
{!! Form::close() !!}
See this similar question.
Updated per comments
If you need to access the pages separately you could extract the form code into partials and include them where needed.
main.blade.php
#extends('master')
#section('content')
#include('auth.partials.login')
<hr>
#include('auth.partials.register')
#endsection
Setup you stand alone pages.
auth/register.blade.php
#section('content')
#include('auth.partials.register')
#endsection
auth/login.blade.php
#section('content')
#include('auth.partials.login')
#endsection
Setup your partials
auth/partials/register.blade.php
{!! Form::open() !!}
...
{!! Form::submit('Login', ['class' => 'btn btn btn-primary']) !!}
{!! Form::close() !!}
auth/partials/login.blade.php
{!! Form::open() !!}
...
{!! Form::close() !!}

Related

Why i have POST method if everywhere is GET method?

I want make search by parameters. But it shows i have mixing GET and POST methods. (Error message: MethodNotAllowedHttpException
No message). Blade form by default have POST. i changed to GET. Route have GET method. Maybe you can see what i am doing wrong. This is my VIEW:
{!! Form::open([ 'action' => ['HomePageController#index', 'method' => 'get']]) !!}
<div class="container">
<div class="col-xs-2 form-inline">
{!! Form::label('city_id', trans('quickadmin.companies.fields.city').'', ['class' => 'control-label']) !!}
{!! Form::select('city_id', $cities, old('city_id'), ['class' => 'form-control select2') !!}
</div>
<div class="col-xs-3 form-inline">
{!! Form::label('categories', trans('quickadmin.companies.fields.categories').'', ['class' => 'control-label']) !!}
{!! Form::select('categories', $categories, old('categories'), ['class' => 'form-control select2']) !!}
</div>
<div class="col-xs-3 form-inline">
{!! Form::label('search', trans('quickadmin.companies.fields.name').'', ['class' => 'control-label']) !!}
{!! Form::text('search', old('search'), ['class' => 'form-control', 'placeholder' => 'Search']) !!}
</div>
<div class="form-inline">
<div class="col-xs-2">
<button type="submit"
class="btn btn-primary">
Search
</button>
</div>
</div>
</div>
{!! Form::close() !!}
My controller:
public function index( Request $request)
{
$cities = \App\City::get()->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
$categories = \App\Category::get()->pluck('name', 'id')->prepend(trans('quickadmin.qa_please_select'), '');
$name = $request->input('city_id');
$companies = \App\Company::All()->where('city_id', '=', $name);
return view('table', compact('companies', $companies, 'cities', $cities, 'categories', $categories));
My route:
Route::get('/', 'HomePageController#index');
Thank you for your help.
There is a problem in the form open, try it like this :
{!! Form::open([ 'action' => 'HomePageController#index', 'method' => 'get']) !!}

Form model binding laravel 5.1 for multiple models

I want Form model binding for multiple objects in laracollective's Form package?
Something as following?
Form::model([$user,$vendors], array('route' => array('user.update', $user->id)))
Where can I request this feature?
I assume you're using Laravel-Collective, Unfortunately you cant do something like that. instead you can try something like this :
UPDATE
you can query all your model in your controller and combine them like this :
$user = User::where('id',$user_id)->get();
$vendor = Vendor::where('user_id',$user_id)->get();
//merge two model
$user = $user->merge($vendor);
// return $user;
return view('admin.users.edit', compact('user'))
->withTitle('Edit user');
and in your form call them like this :
{!! Form::model($user[1], ['route' => ['admin.users.update', $user],'method'=>'PUT']) !!}
#include('admin.users._formEdit')
<div>
{!! Form::submit('Save user', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
_formEdit.blade.php
<div class="form-group">
{!! Form::label('first_name', 'First Name : ') !!}
{!! Form::text('user[first_name]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('last_name', 'Last Name : ') !!}
{!! Form::text('user[last_name]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('email', 'Email : ') !!}
{!! Form::email('user[email]', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('password', 'Password') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
<div class="form-group ">
{!! Form::label('vendor_name', 'vendor_name') !!}
{!! Form::text('vendor_name', null,['class' => 'form-control']) !!}
</div>
OR ANOTHER SOLUTION
create relation between model of your User and Vendor (one-to-one or one-to-many) example
User :
public function vendor(){
return $this->hasOne('App\Vendor','user_id');
}
Vendor:
public function user(){
return $this->belongsTo('App\User','user_id);
}
Build your response query like this :
$user = Vendor::with('user')->find($user_id);
and then in your view template :
{!! Form::model($user, ...) !!}
Vendor: {!! Form::text('vendor_name') !!}
User: {{ Form::text('user[username]') }}
{!! Form::close() !!}

Form model binding not working in laravel 5.2 using laravelcollective

Hello I'm new to laravel 5.2 and going through some lessons.
For some reason form model binding is not working for me.
{!! Form::model($post, ['method'=>'PATCH', 'action'=> ['PostController#update', $post->id]]) !!}
I received data in $post because I'm using a workaround like this:
{!! Form::text('title', "$post->title" ,['class'=> 'form-control']) !!}
And that is showing my data.
Controller:
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
use App\Http\Requests;
class PostController extends Controller{
public function update(Request $request, $id){
$post =Post::findOrfail($id);
$post->update($request->all());
return redirect('/posts');
}
}
create.blade.php view:
#section('content')
<h1>Create Post</h1>
{!! Form::open(['method'=>'POST', 'action'=>'PostController#store']) !!}
<!-- Title Form Input -->
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
</div>
<!-- Form Input -->
<div class="form-group">
{!! Form::submit('Create Post', ['class'=> 'btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
#endsection
You shouldn't be quoting the value for null in your input:
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
should be
{!! Form::text('title', null, ['class'=> 'form-control']) !!}
Ok, try adding the body of the form into a partial called posts/partials/form.blade.phpand include it between the form open / model and form close tags.
Example:
posts/partials/form.blade.php
<!-- Title Form Input -->
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', 'null', ['class'=> 'form-control']) !!}
</div>
<!-- Form Input -->
<div class="form-group">
{!! Form::submit($formButtonText, ['class'=> 'btn-primary form-control']) !!}
</div>
posts/create.blade.php
{!! Form::open(['method'=>'POST', 'action'=>'PostController#store']) !!}
#include('posts.partials.form', [
'formSubmitButtonText' => 'Create Post'
])
{!! Form::close() !!}
posts/edit.blade.php
{!! Form::model($post, ['method'=>'PATCH', 'action'=> ['PostController#update', $post->id]]) !!}
#include('posts.partials.form', [
'formSubmitButtonText' => 'Update Post'
])
{!! Form::close() !!}

Has Blade Templating Changed from Laravel 4 to Laravel 5?

Issue: Converting my Laravel 4 code to Laravel 5. Nothing appears on the screen in between the #section('container') & #stop.
Notes: I am using the Laravel Collective HTML Facades. I have severely shortened the master.blade.php file for brevity.
Request: Please assist in figuring out why nothing is showing up in the browser and let me know in detail what I am doing wrong with examples, if possible.
Error & Debug: There is none. The page is just blank.
Attempts: If I add anything outside the #section('container') & #stop then it shows up on the screen.
composer.json
"require": {
"php": ">=5.5.9",
"laravelcollective/html": "5.2.*"
},
app.php
Providers Array
Collective\Html\HtmlServiceProvider::class,
Alias Array
'Form' => Collective\Html\FormFacade::class,
'Html' => Collective\Html\HtmlFacade::class,
master.blade.php
<body>
<!--[if lt IE 7]>
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please upgrade your browser or activate Google Chrome Frame to improve your experience.</p>
<![endif]-->
<!-- Add your site or application content here -->
#yield('container')
</body>
index.blade.php
#section('container')
<!--[if gte IE 9]>
<style type="text/css">
.gradient {
filter: none;
}
</style>
<![endif]-->
<div style="margin: 10px 0 0 20px;border: 0px black dashed;width: 410px;float:left;">
{!! Form::open(array('action' => 'ContractController#store')) !!}
{!! Form::token() !!}
<dl style="list-style-type:none;">
<fieldset>
<legend>General Information</legend>
<dd>
{!! Form::label('contractterm','Contract Term', array('class' => 'label')) !!}
{!! Form::select('contractterm_id', $contracttermlist) !!}
</dd>
<dd>
{!! Form::label('businesstype','Business Type', array('class' => 'label')) !!}
{!! Form::select('businesstype_id', $businesstypelist) !!}
</dd>
<dd>
{!! Form::label('contract_date','Contract Starting Date', array('class' => 'label')) !!}
{!! Form::text('contract_date', date('m/d/Y'), array(
'size'=>'16',
'id' =>'datepicker'))
!!}
</dd>
<dd>
{!! Form::label('service_credit','Previous Service', array('class' => 'label')) !!}
<div style="float:right;width:140px;height:29px;"><span>Yes</span><span style="float:right;width:20px;padding-top:4px;padding-right:90px;">{!! Form::checkbox('prevcredit', 'credityes', false, array('class' => 'creditcheckbox','id' => 'prevcredit')); !!}</span></div>
</dd>
<dd>
{!! Form::label('credit_amount','Credit Amount', array('class' => 'label','id' => 'credit_amount')) !!}
{!! Form::text('credit_amount','0.00',array('id' => 'credit_amount2')) !!}
</dd>
<dd>
{!! Form::label('addtpcs','Additional PCs', array('class' => 'label')) !!}
{!! Form::select('addtpcs', $addtpcs) !!}
</dd>
<div id="inputs" style="margin:0;">
</div>
</fieldset>
<fieldset>
<legend>Personal Information</legend>
<dd>
{!! Form::label('firstname','First Name', array('class' => 'label')) !!}
{!! Form::text('firstname') !!}
</dd>
<dd>
{!! Form::label('lastname','Last Name', array('class' => 'label')) !!}
{!! Form::text('lastname') !!}
</dd>
<dd>
{!! Form::label('hstraddr','Home Address', array('class' => 'label')) !!}
{!! Form::text('hstraddr') !!}
</dd>
<dd>
{!! Form::label('hcity','Home City', array('class' => 'label')) !!}
{!! Form::text('hcity') !!}
</dd>
<dd>
{!! Form::label('hstate','Home State', array('class' => 'label')) !!}
{!! Form::text('hstate', 'Florida') !!}
</dd>
<dd>
{!! Form::label('hzip','Home Zip Code', array('class' => 'label')) !!}
{!! Form::text('hzip') !!}
</dd>
<dd>
{!! Form::label('hphone','Home Phone', array('class' => 'label')) !!}
{!! Form::text('hphone') !!}
</dd>
<dd>
{!! Form::label('mobile','Mobile Phone', array('class' => 'label')) !!}
{!! Form::text('mobile') !!}
</dd>
</fieldset>
<fieldset>
<legend>Business Information</legend>
<dd>
{!! Form::label('company','Company Name', array('class' => 'label')) !!}
{!! Form::text('company') !!}
</dd>
<dd>
{!! Form::label('bstraddr','Business Address', array('class' => 'label')) !!}
{!! Form::text('bstraddr') !!}
</dd>
<dd>
{!! Form::label('bcity','Business City', array('class' => 'label')) !!}
{!! Form::text('bcity') !!}
</dd>
<dd>
{!! Form::label('bstate','Business State', array('class' => 'label')) !!}
{!! Form::text('bstate', 'Florida') !!}
</dd>
<dd>
{!! Form::label('bzip','Business Zip', array('class' => 'label')) !!}
{!! Form::text('bzip') !!}
</dd>
<dd>
{!! Form::label('bphone','Business Phone', array('class' => 'label')) !!}
{!! Form::text('bphone') !!}
</dd>
<dd>
<div class="submitbutton">
{!! Form::submit('Submit') !!}
</div>
</dd>
</fieldset>
</dl>
{!! Form::close() !!}
</div>
<div style="clear:both;"></div>
#stop
IndexController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Library\additionalPCs;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use View;
use App\Models\businesstype;
use App\Models\contractterm;
class IndexController extends BaseController
{
Protected $layout = 'master';
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
/** Wayne - 03-02-2014 - Moved for loop to a method within its own class. */
$numberofpcs = new additionalPCs();
$addtpcs=$numberofpcs->display();
//$this->layout->content = View::make('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());
return view('index')->with('addtpcs', $addtpcs)->with('businesstypelist', businesstype::dropdown())->with('contracttermlist',ContractTerm::dropdown());
}
}
In your index.blade file you appear to be missing the extends tag that should appear at the very top, followed by your #section code
#extends('master')
#section('container')
your section code here
#stop
It's now #section('container') and then #endsection instead of #stop as of Laravel 5.1.
https://laravel.com/docs/5.1/blade#template-inheritance

Error when upload file - laravel 5

Controller:
public function store(StoreSongRequest $request)
{
dd($request->get('song'));die;
}
create.blade.php
{!! Form::open(array('route' => 'songs.store', 'class' => 'form', 'novalidate' => 'novalidate', 'files' => true)) !!}
<div class="form-group">
{!! Form::label('Name') !!}
{!! Form::text('name') !!}
</div>
<div class="form-group">
{!! Form::label('Upload') !!}
{!! Form::file('song') !!}
</div>
<div class="form-group">
{!! Form::submit('Upload', array('class' => 'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
When i submit i appear error:
TokenMismatchException in VerifyCsrfToken.php line 46:
I was comment out 'app\Http\Middleware\VerifyCsrfToken' in kernel.php file but still the same.
The issue is that post_max_size was low $_POST. Increasing post_max_size in php.ini solved the issue.

Resources