Laravel error page when creating CRUD application - php

Following code in create.blade.php in the view->tasks->create.blade.php
#extends('layouts.master')
#section('content')
<h1>Add a New Task</h1>
<p class="lead">Add to your task list below.</p>
<hr>
{!! Form::open(['route' => 'tasks.store']) !!}
<div class="form-group">
{!! Form::label('title', 'Title:', ['class' => 'control-label']) !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('description', 'Description:', ['class' => 'control-label']) !!}
{!! Form::textarea('description', null, ['class' => 'form-control']) !!}
</div>
{!! Form::submit('Create New Task', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
#stop
A task controller I created for the controller sections
Controller->TasksController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class TasksController extends Controller {
public function index() {
return view('tasks.index');
}
public function create() {
return view('tasks.create');
}
}
inside our config/app.php We add one service provider
'Illuminate\Html\HtmlServiceProvider',
aliases something like this
'Form' => 'Illuminate\Html\FormFacade',
'Html' => 'Illuminate\Html\HtmlFacade',
In a Vender->laravel->framework->src->Illuminate->Html->HtmlServiceProvider.php
Change the method name to bindingshare to the singleton.
IN The Route file Following code written.
Route::resource('tasks', 'TasksController');
Following Snapshot error display.

Model Task is in other namespace, so you should after:
use App\Http\Controllers\Controller;
add something like this:
use App\Models\Task;
or
use Task;
to use Task from valid namespace

Related

Laravel Contact form on Single Page website

I'm trying to get the correct routes for the Laravel Contact form on a single page website but I'm not sure how to apply the routes so far, since i've done it with sites that aren't single page (parallax - like website/ scrollable).
These are the routes I am creating, so everything stays on the homepage(no redirects at all because it's a one page scrollable website)
Route::get('/', 'ContactUsController#create')->name('contact.create');
Route::post('/', 'ContactUsController#store')->name('contact.store');
My Controller looks like this: Please note that the create controller returns the view to my index which of course is routed like so ('/'),
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Mail\ContactEmail;
class ContactUsController extends Controller
{
public function create()
{
return view('index');
}
public function store(Request $request)
{
$contact = [];
$contact['name'] = $request -> get('name');
$contact['phone'] = $request -> get('phone');
$contact['email'] = $request -> get('email');
$contact['subject'] = $request -> get('subject');
$contact['message'] = $request -> get('message');
//send mail logic here
Mail::to(config('mail.support.address'))->send(new ContactEmail($contact));
flash('Your Message has been sent!) -> success();
return redirect()-> route('/');
}
}
And below is my Contact Form:
{!! Form::open(['route' => 'contact.store', 'class' => 'text-light '])!!}
{!! Form::label('name', 'Your Name', ['class' => 'text-light'])!!}
{!! Form::text('name', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('phone', 'tel', ['class' => 'text-light'])!!}
{!! Form::text('phone', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('email', 'Email', ['class' => 'text-light'])!!}
{!! Form::text('email', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('subject', 'Subject', ['class' => 'text-light'])!!}
{!! Form::text('subject', null, ['class' => 'form-control text-light'])!!}
{!! Form::label('message', 'Your Message Here..', ['class' => 'text-light'] )!!}
{!! Form::textarea('message', null, ['class' => 'form-control text-light'])!!}
{!! Form::submit('Submit', ['class' => 'btn btn-info']) !!}
{!! Form::close() !!}
#if($errors -> any())
<div class="alert alert-danger">
<ul>
#foreach($errors -> all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#include('flash::message')
Solved problem with solution :
//routes for contact form
Route::get('/contacts', 'ContactUsController#create')->name('contact.create');
Route::post('/contacts', 'ContactUsController#store')->name('contact.store');
Everything else same.
Thanks guys for all support.
//Create the routes in web.php
Route::get('contact',['as' => 'contact, 'uses' => 'ContactController#create']);
Route::post('contact',['as' => 'contact', 'uses' => 'ContactController#store']);
// Create the ContactController using the command
php artisan make:controller ContactController
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ContactRequest;
use Mail;
class ContactController extends Controller
{
public function create()
{
return view('front.contact.index');
}
public function store(ContactRequest $request)
{
\Mail::send('emails.contact',
array(
'name' => $request->get('name'),
'email' => $request->get('email'),
'user_message' => $request->get('message')
), function($message)
{
$message->from('hello#outlined.co');
$message->to('poornima#outlined.co', 'outlined')->subject('New Contact
Request from outlined');
});
return back()->with('status', 'your message has been received');
}}

Laravel inserting "null" into database from form input.

This is a strange problem i've run into. I have a form that takes in First, Last, Title, Email, and Phone which takes that data and inputs into a ContactPerson table I have set up in a database.
When I fill out the form, and hit submit, only the Title, Email, and Phone number get inserted correctly into the database. The first and last names get inserted as NULL.
Here's some of my code that I have using PHPstorm and Laravel 2.5.3
My Create a New Contact view:
<h1 style="text-align:center;">Add A New Contact Person</h1>
<hr/>
{!! Form::open(['url' => '/create']) !!}
<span style="text-align:center;align-content:right;display:block; margin-right:auto;margin-left:auto;">
<div class="form">
{!! Form::label('first', 'First Name: ') !!}
{!! Form::text('First', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('last', 'Last Name: ') !!}
{!! Form::text('Last', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('title', 'Title: ') !!}
{!! Form::text('Title', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('email', 'Email: ') !!}
{!! Form::text('Email', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('phone', 'Phone: ') !!}
{!! Form::text('Phone', null, ['class' => 'form']) !!}
</div>
{!! Form::submit('Submit', ['class' => 'btn btn-primary form']) !!}
</span>
{!! Form::close() !!}
Here is my controller called ResourceController I have created:
class ResourceController extends Controller
{
public function resource()
{
$contacts = ContactPerson::all();
return view('pages.resource', compact('contacts'));
}
public function create()
{
return view('pages.create');
}
public function store(Requests\CreateNewContactRequest $request)
{
ContactPerson::create($request->all());
return redirect('resource');
}
}
Here's the validation I have set up in the CreateNewContactRequest class for the form:
public function rules()
{
return [
'First' => 'required|min:2',
'Last' => 'required|min:1',
'Title' => 'required|min:2',
'Email' => 'required|Email|unique:users',
'Phone' => 'required|numeric|min:9'
];
}
Here's what it looks like when I fill out/submit the form.
After hitting submit it redirects to the database dump:
the view of the database after insertion:
Looking at your dump details, your field names need to match your form names or visa versa, so your form names need to change to say:
<div class="form">
{!! Form::label('first', 'First Name: ') !!}
{!! Form::text('First_Name', null, ['class' => 'form']) !!}
</div>
<div class="form">
{!! Form::label('last', 'Last Name: ') !!}
{!! Form::text('Last_Name', null, ['class' => 'form']) !!}
</div>
EG for field name First_Name your input needs to be named the same name="First_Name"
Also making sure your fillable details are also correct
protected $fillable = ['First_Name','Last_Name','Title','Email','Phone'];

Lravel 5.1 How to find database records through a from?

In the admin area of my app, I want to have a form by which an admin can find a project by its id and show the project details there. What is the best approach to implement this?
This is what I have tried:
//route
Route::get('admin/projects/{project_id}', 'AdminController#showProject');
//form
{!! Form::open(['action' => 'AdminController#showProject', 'method' => 'get']) !!}
{!! Form::label('project_id', 'Project Id', ['class' => 'control-label']) !!}
{!! Form::text('project_id', null, ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'form-control']) !!}
{!! Form::close() !!}
//controller method
public function showProject(Request $request)
{
$project=Project::find($request->get('project_id'));
return view('admin.projects.showProject', compact('project'));
}
It almost worked but there is little problem. After retrieving the requested project, the ULR is like this:
admin/projects/%7Bproject_id%7D?project_id=5
I want it be like this one:
admin/projects/5
How can I solve this problem?
Create the following routes:
Route::get('admin/projects', 'AdminController#getProject');
Route::post('admin/projects', 'AdminController#postProject');
Route::get('admin/projects/{project_id}', 'AdminController#showProject');
In your getProject function you return a view that show the form where the user can enter an ID. (The one you already have):
{!! Form::open(['action' => 'AdminController#postProject', 'method' => 'post']) !!}
{!! Form::label('project_id', 'Project Id', ['class' => 'control-label']) !!}
{!! Form::text('project_id', null, ['class' => 'form-control']) !!}
{!! Form::submit('Submit', ['class' => 'form-control']) !!}
{!! Form::close() !!}
In your postProject function you just send a redirect to admin/project/{project_id} URL:
public function postProject(Request $request)
{
return redirect('admin/projects/' . $request->project_id);
}
In your showProject function you just retrieve the record and return a view with the information:
public function showProject($ProjectID)
{
$project=Project::find($ProjectID);
return view('admin.projects.showProject'),
->with('Project', compact('project'));
}
Try changing your controller method to
public function showProject($project_id)
{
$project=Project::find($project_id);
return view('admin.projects.showProject', compact('project'));
}
You don't need to use request on get route.

Laravel Passing Data From One View to Another View

I am thinking of making a landing page from the home page, which will direct the guest to the register page. I thought of making two forms for sending data and two submit buttons in them, let's say reader and writer and according to the button they use to go to the register form page, I want to pass the profession string from the button in the landing page and then, place it into the register form in /auth/register.
{!! Form::open(array('url' => '/auth/register', 'profession' => 'writer')) !!}
{!! Form::submit('Writer', array('class' => 'btn btn-warning')) !!}
{!! Form::close() !!}
{!! Form::open(array('url' => '/auth/register', 'profession' => 'reader')) !!}
{!! Form::submit('Reader', array('class' => 'btn btn-default')) !!}
{!! Form::close() !!}
It is not directing me to the page app.com/auth/register. But it works when I directly type the link.
What I thought was using $profession in /auth/register/ and access the value and use it as a hidden field in the registeration form.
(using laravel 5.1)
Edit:
In view source:
<form method="POST" action="http://app.com/auth/register" accept-charset="UTF-8" profession="writer"><input name="_token" type="hidden" value="dZXQsNI1BGQ39JjDLFUEkSQzL5bTNwe8o3rpiSQL">
<input class="btn btn-warning" type="submit" value="Writer">
</form>
<form method="POST" action="http://app.com/auth/register" accept-charset="UTF-8" profession="reader"><input name="_token" type="hidden" value="dZXQsNI1BGQ39JjDLFUEkSQzL5bTNwe8o3rpiSQL">
<input class="btn btn-default" type="submit" value="Reader">
</form>
Edit 2:
{!! Form::open(array('url' => '/auth/register', 'profession' => 'writer')) !!}
{!! link_to('/auth/register', 'Writer', array('class' => 'btn btn-default')) !!}
{!! Form::close() !!}
I tried this instead. At least, now it is directing the page but I still can't access the data value of profession
Edit 3:
Routes:
Route::get('auth/register', 'Auth\AuthController#getRegister');
Route::post('auth/register', 'Auth\AuthController#postRegister');
Route::get('/', function()
{
return view('pages.home');
});
and https://app.com/auth/register is working.
Here's a step by step walkthrough on how to implement it. I tested it. So it works. This is for 'writer', but you could replicate it as you had originally planned for other professions.
I assume you've registered the Laravel Collective package since you're using the curly braces and exclamation points.
Step 1:
In your landing page view, where you have the writer button, add a hidden field with the string 'writer'. Like this:
{!! Form::open(['route' => ['writer_path']]) !!}
{!! Form::hidden('profession', 'writer') !!}
{!! Form::submit('Writer', array('class' => 'btn btn-warning')) !!}
{!! Form::close() !!}
Not that in the open field we are using a named route ('writer_path').
Step 2:
Register a route and a controller on your routes.php file, like this:
Route::post('auth/register', [
'as' => 'writer_path',
'uses' => 'SampleController#displayForm'
]);
Step 3:
In your sample controller, you define the displayForm method.
Within that method you first obtain the value you passed from the landing page view.
If you don't know how to create a controler, you can do
php artisan make:controller SampleController
from the command line
Because the value arrives as an array, you have to obtain the string 'writer' from the array and then pass it to the new view (the view with the registration form for the writer).
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
class SampleController extends Controller
{
/**
* Display a listing of the resource.
*
* #return Response
*/
public function displayForm()
{
$input = Input::get();
$profession = $input['profession'];
return view('writerregistration', ['profession' => $profession]);
}
}
Last Step:
In the new view which you will create as writerregistration.blade.php, you will display the form with the field you just passed ('profession') which contains the string 'writer'. Like this:
{!! Form::open() !!}
{!! Form::label('username', 'Username:') !!}
{!! Form::text('username', null, ['class' => 'form-control']) !!}
{!! Form::label('profession', 'Profession:') !!}
{!! Form::text('profession', $profession, ['class' => 'form-control']) !!}
{!! Form::label('email', 'Email:') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
{!! Form::label('passowrd', 'Password:') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
{!! Form::label('password_confirmation', 'Password Confirmation:') !!}
{!! Form::password('password_confirmation', ['class' => 'form-control']) !!}
{!! Form::submit('Sign Up', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
Presto, you've populated the field in the registration form for the writer with the info on the hidden field that belonged to the writer button in the landing page.

Laravel 5.0 Model::create() method creates two rows in one form request

When I fire Product::create($request->all()) method it creates two rows in a table with the same values.
Here is my Form.
{!! Form::open(['route' => 'product.store', 'method' => 'post']) !!}
<!-- Title Form Input -->
<div class="form-group">
{!! Form::label('title', 'Title:', ['class' => 'control-label']) !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<!-- Description Form Input -->
<div class="form-group">
{!! Form::label('description', 'Description:', ['class' => 'control-label']) !!}
{!! Form::textarea('description', null, ['class' => 'form-control', 'id' => 'source']) !!}
</div>
<!-- Category Form Input -->
<div class="form-group">
{!! Form::label('category', 'Category:', ['class' => 'control-label']) !!}
{!! Form::text('category', 'Test Cat', ['class' => 'form-control']) !!}
</div>
<!-- create Form -->
<div class="form-group">
{!! Form::submit('create', ['class' => 'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
Here is my store method
public function store(ProductRequest $request)
{
Product::create($request->all());
// $productTran = $request->only(['title', 'description']);
// $productTran['product_id'] = $product->id;
}
Here is my Product.php (Model)
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model {
protected $table = 'products';
protected $fillable = ['category'];
}
And here is my ProductRequest class
class ProductRequest extends FormRequest {
/**
* Get the validation rules that apply to the request.
*
* #return array
*/
public function rules()
{
return [
'title' => 'required'
];
}
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
}
I'm using it exatly the way you do, and didn't happen to me.
I don't know if this have any relationship, but in your model, the fillable var you need to put ['title', 'description', 'category'] for working correctly.
Or, at less, ['title'] that is the only required value.
Why you don't put dd($request); and show us what is inside.

Categories