No query results for model [App\WhatTodoModel] - php

I am newbie in laravel and I try to insert a data form a form having foreign key by using hide such as the code is mention below:-
<form class="form-horizontal" role="form" action="/WhatTodo/store" method="POST">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="hidden" name="task_id" value=" {{$what->task_id}}">
<input type="hidden" name="work_id" value="{{$what->work_id}}">
<div class="form-group">
<label class="control-label col-sm-2" for="name"> Name</label>
<div class="col-sm-5">
{!!Form::select('name',$name)!!}
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="work">work:</label>
<div class="col-sm-5">
<input type="text" class="form-control" name="work" value="">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-default" value="Submit">
</div>
</div>
</form>
I have the controller with function:-
public function create($id)
{
$what=WhatTodoModel::findorFail($id);
$name=WOrk::lists('name','name');
return view('what/create',compact('what','name'));
}

You haven't really told us what your issue is or what error you're getting, but my guess given the current question is:
Assuming you're trying to implement a resource route and resourceful controller, the create method is used to show a form to create a new object, not edit an existing one. The create method does not take any parameters, therefore $id will be blank and WhatTodoModel::findorFail($id); will throw an exception.
If you want to edit an existing record, you do that using the edit action.
For creating a new record, create shows the form, store saves the record.
For editing an existing record, edit shows the form, update saves the record.

Related

Laravel form submission doesn't goes to the right route?

I have installed the latest laravel. I Have made this simple form. I want to create post but when I submit it goes to localhost/post which is the wrong URL . The actual URL is http://localhost/laravel_practice/'
Form
<form method="post" action="/post">
<div class="form-group">
<label>Title</label>
<input type="text" name="title" class="form-control" placeholder="Enter Title Here">
</div>
<div class="form-group">
<label>Body</label>
<textarea name="body" class="form-control" placeholder="Enter the body"></textarea>
</div>
<div class="form-group">
<input type="submit" name="sumit" class="btn btn-primary" value="Publish">
</div>
My Routes
Route::get('/' ,'PostController#index');
Route::get('/posts/create', 'PostController#create');
Route::post('/post','PostController#store');
Your short fix is to use action="/laravel_practice/post" or action="/laravel_practice/public/post" depending on what url you want to go.
However, it is a bad practice. You should use route name. To do that give any name to the route like below,
Route::post('/post','PostController#store')->name('post.store');
Then in view you should use,
<form method="post" action="{{ route('post.store') }}">
To read more about named route you can go through this document.

Laravel create custom method to get form data

How do I make custom method to get form data? I want this method same with Laravel update method with parameters request and id. I try this but get error.
In controller
public function updatePassword(Request $request, int $id) {
dd($request->all());
}
In route
Route::post('staffs/{id}/upassword', 'Admin\StaffController#updatePassword')->name('admin.staffs.upassword');
In blade file
<form method="post" accept-charset="utf-8" action="{{ action('Admin\StaffController#updatePassword', ['id' => $staff_id]) }}">
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="password">New Password</label>
<input class="form-control" name="password" type="password">
</div>
</div>
</div>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<label class="control-label" for="password_confirmation">Confirm New Password</label>
<input class="form-control" name="password_confirmation" type="password">
</div>
</div>
</div>
<input class="btn btn-primary" type="submit">
</form>
I am using Laravel 5.4.
here are some stuff to fix:
First in the tag you can set the action to :
action="route('admin.staffs.upassword', $staff_id)" since it's
easier to write and since you already gave the route a name, so why
not using it ;)
Second add {{csrf_field() }} right before your form closing tag
</form>
what error are you getting? the error is probably because you are not using {{csrf_field()}} after the form declaration, it is needed so that laravel can validate the request. if you want to get the data from the form you can use:
$request->get('inputname');

Symfony crawler selectButton method can't get form

here is my html
<form name="station" method="post" action="/stations/new" role="form">
<div class="form-group">
<label class="control-label required" for="station_name">Name</label>
<input type="text" id="station_name" name="station[name]" required="required" maxlength="255" class="form-control" >
</div>
<div class="form-group">
<div class="checkbox">
<label for="station_active">
<input type="checkbox" id="station_active" name="station[active]" value="1" />Active</label>
</div>
</div>
<div class="form-group">
<button type="submit" id="station_submit" name="station[submit]" class="btn btn-primary">Ajouter</button>
</div>
<input type="hidden" id="station__token" name="station[_token]" class="form-control" value="aze123aze" >
</form>
i want to get my form using the crawler. I tried the selectButton method like this
$form = $crawler->selectButton('station[submit]')->form(array());
but i get the error : InvalidArgumentException: The current node list is empty.
what is the problem ?
Unfortunately I have no enough rating to just write a comment instead of put it in the answer.
So, could you please show how are you getting $crawler? Problem might be:
$crawler not point to DOM which contains this form
this form appears on page after some java script actions(Ajax for example), but not sure that this is your case.
The selectButton method accept the value The button text. So Try with:
$form = $crawler->selectButton('Ajouter')->form(array());
Hope this help

Laravel testing phpunit press NotFoundHttpException

I'm trying to setup phpunit tests for a project with Laravel 5.1.40 (LTS), php 5.6.28, and phpunit 4.8.27. I'm sorry if this issue has been solved before, but I couldn't find anything.
public function testAdminLogin()
{
$this->visit('/auth/login')
->type('email#address.com', 'email')
->type('1234567890', 'password')
->press('Login');
}
There seem to be an issue with press('STRING') with both <button> and <input> as submit buttons. Below is the error message I receive.
1) ExampleTest::testAdminLogin
A request to [http://localhost/auth/login] failed. Received status code [500].
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:165
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:63
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:85
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:684
C:\xampp\htdocs\project\vendor\laravel\framework\src\Illuminate\Foundation\Testing\InteractsWithPages.php:671
C:\xampp\htdocs\project\tests\ExampleTest.php:52
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:176
C:\xampp\php\pear\PHPUnit\TextUI\Command.php:129
However, when I change the <button> tag to an <a> tag, add an id to it, and replace the press(STRING) function with the click(ID) function, the test passes. I could change the <button> to an <a>, but that would only a temporary fix, and future cases might not allow the tag change.
Below is the HTML form with the <button> tag.
<form action="/auth/login" method="POST" class="form-horizontal">
<div class="form-group">
<label for="email" class="col-sm-4 control-label">E-Mail</label>
<div class="col-sm-6">
<input type="email" name="email" class="form-control" value="{{ old('email') }}" autocomplete="off">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-4 control-label">Password</label>
<div class="col-sm-6">
<input type="password" name="password" class="form-control" autocomplete="off">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-6">
<button type="submit" class="btn btn-default btn-login">Login</button>
</div>
</div>
</form>
You said you define auth routes manually. In this case you should have POST route for sending login form:
Route::post('auth/login', ....
It works in a href because it sends GET request for which you have route. Form sends POST request by default.

Using "action" in Twitter Boostrap classes

I'm a newbie programmer trying to utilize Twitter Bootstrap to build out a concept. I'm using PHP and assigning actions within HTML tags to POST data and essentially take a user through the navigation. This has been pretty straightforward when using forms: i.e. here is a snippet that does work. For example, for this snippet of HTML:
<form action="?viewnotes" method="post">
<input type="hidden" name="id" value="<?php htmlout($note['id']); ?>">
</form>
It successfully triggers the following if statement in my index.php:
if (isset($_GET['viewnotes']))
However, I'm trying to do the same thing in my registration page, using a Twitter Bootstrap class for buttons. Here is the HTML:
<a class="btn btn-primary btn-large" action="?register">Sign Up Free!ยป</a></p>
The PHP code is:
if (isset($_GET['register']))
{
include 'register.html.php';
}
Clicking on the Sign Up button is not invoking the PHP code. If I hard code the URL it works, but then I have a similar issue on the register.html.php page. HTML on the register page has:
<form class="form-horizontal" action="?storeuser" method="post">
<fieldset>
<legend>It's quick and easy...</legend>
<div class="control-group">
<label class="control-label">First Name</label>
<div class="controls">
<input type="text" name="fname" class="input-xlarge" id="fname">
</div>
</div>
<div class="control-group">
<label class="control-label" name="lname">Last Name</label>
<div class="controls">
<input type="text" name="lname" class="input-xlarge" id="lname">
</div>
</div>
<div class="control-group">
<label class="control-label" name="email">Email Address</label>
<div class="controls">
<input type="text" name="email" class="input-xlarge" id="email">
</div>
</div>
<div class="control-group">
<label class="control-label" name="password">Password</label>
<div class="controls">
<input type="password" name="password" class="input-xlarge" id="password">
</div>
</div>
</fieldset>
<button type="submit" name="action" class="btn btn-primary btn-large">Complete Registration</button>
</form>
However, when clicking on the button, the index file does not trigger the following, which would store the fields into the DB.
if (isset($_GET['storeuser']))
If I hardcode the URL to register.html.php, then the resulting URL looks like localhost/register.html.php?storeuser instead of localhost/?storeuser. I'm not sure if that's affecting the behavior here.
Thank you for the help!
I think you're approaching this the wrong way, and it's not Twitter Bootstrap's fault.
Usually, you'd use POST, not GET, to handle user registrations. Your form would look like this:
<form action="register.php" method="post">
<!-- your form -->
<fieldset>
<input type="submit" name="register" value="Register" class="btn btn-primary" />
</fieldset>
</form>
You can then build register.php as follows:
<?php
if (isset($_POST['register'])) {
// handle user registration
}
// display form
?>
This will display the form when the user visits register.php, but will try and process the user registration first if the form's been POSTed.
try passing a value with your GET variable
<form action="?viewnotes=1" method="post">

Categories