Laravel 5 error message if keywords not exist - php

I am receiving this message as it is.
I want an error message when I call something that is not databased.
I am new at laravel, i know i have to write elseif condition, but i don't know how.
FatalErrorException
Method Illuminate\View\View::__toString() must not throw an exception,
caught ErrorException: Undefined variable: yyy (View:
/Users/xxx/Work/yyy/resources/views/yyy/index.blade.php)
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use DB;
class SearchController extends Controller {
public function getSearch(Request $request) {
$search = $request->input('search');
if(empty($search)) {
return "please search something!";
}
$searchResult = DB::table('yyy')->where('dateOfReport', 'LIKE', "%$search%")->get()->toArray();
return view('search.searchResult', ['searchResult' => $searchResult, 'search' => $search]);
}
}
Also here is my view.blade file:
#extends('layout')
#section('content')
<h1>Detaily yyy Reports</h1>
<h2>{{$yyy->temperature }}°C</h2>
<p>{{$yyy->dateOfReport}}
<font color="red">Delete</font>
Report created at: {{$yyy->created_at}}
#if($yyy->created_at != $yyy->updated_at)
Report updated at: {{$yyy->updated_at}}
{{ csrf_field() }}
#endif
#endsection
thank you for your time!

this error seems to originate in your view code, not the controller.
you probably use a $yyy variable in your view, while you didn't assign it in the controller.
i.e.
return view('search.searchResult', ['searchResult' => $searchResult, 'search' => $search, 'yyy' => 'this is your assigned variable value']);

Related

Trying to get property 'POST /employee HTTP/1.1

I have created a laravel application to store employee data, but when I submit the form it gives me the following error, what should I do to avoid this problem. thanks
This is my EmployeeController store method
public function store(Request $request)
{
$this->validate($request,array(
'lastname'=>'required|max:60',
'firstname'=>'required|max:60',
'middlename'=>'required|max:60',
'address'=>'required|max:120',
'NIC'=>'required|max:10',
'city_id'=>'required|max:60',
'state_id'=>'required|max:60',
'mobile'=>'required|max:10',
'email'=>'required|max:60',
'postal_code'=>'required|max:10',
'birthdate'=>'required|date',
'date_hired'=>'required|date',
'department_id'=>'required|max:10',
));
$employee = new Employee();
$employee->lastname=$request->lastname;
$employee->firstname=$request->firstname;
$employee->middlename=$request->middlename;
$employee->address=$request->address;
$employee->NIC=$request->NIC;
$employee->city_id=$request->city_id;
$employee->state_id=$request->state_id;
$employee->mobile=$request->mobile;
$employee->email->$request->email;
$employee->postal_code=$request->postal_code;
$employee->birthdate=$request->birthdate;
$employee->date_hired=$request->date_hired;
$employee->department_id=$request->department_id;
$employee->save();
}
Form header
{!! Form::open(['route'=>'employee.store','class'=>'form-horizontal p-t-20']) !!}
Classes i used for the controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Department;
use Illuminate\Support\Facades\DB;
use App\Employee;
There is an error in your code.
$employee->email->$request->email;
This should be,
$employee->email = $request->email;
By the looks of it you are trying to validate the $request variable itself. Hence the Trying to get property POST
Should it not be...
$request->validate(array(
'lastname'=>'required|max:60',
'firstname'=>'required|max:60',
'middlename'=>'required|max:60',
'address'=>'required|max:120',
'NIC'=>'required|max:10',
'city_id'=>'required|max:60',
'state_id'=>'required|max:60',
'mobile'=>'required|max:10',
'email'=>'required|max:60',
'postal_code'=>'required|max:10',
'birthdate'=>'required|date',
'date_hired'=>'required|date',
'department_id'=>'required|max:10',
));
ErrorException: Trying to get property 'POST /Addpatients HTTP/1.1
This ERROR IS DUE to
when you difine your variable in any method Please check that you doest not define as below because it gives the error
$patients->address->$request->input('address');
The solution is of this is as below
$patients->address=$request->input('address');

Very Confusing MethodNotAllowedHttpException on a put request laravel

So far all attempts to modify the routing methods have failed.
Been following some documentation on laravel restful controllers and have one set up to do basic editing and adding of items to a database. It was going well till I hit the snag on... well I'm not sure what precisely is triggering the problem, but basically, everything works till I hit submit on the form and then it's Game Over.
Normally I'd be able to diagnose this by checking to see if I'm using the right call, or made a spelling mistake or something. But this is a new request for me, so I can't quite debug where the problem is coming from.
This is the error those who know what to look for. In full here.
MethodNotAllowedHttpException in RouteCollection.php line 218:
My routes are pasted here.
A printout of the routes is here:
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests\ContactFormRequest;
use App\UserEdit;
use DB;
use App\Http\Requests;
class EditUserController extends Controller
{
public function index()
{
$array = UserEdit::all()->toArray();
return view('UserEntry', compact('array'));
}
public function create()
{
$id = UserEdit::find(715)->toArray();
return view('NewUser', compact('id'));
}
public function store(UserFormRequest $request)
{
//$user = new UserEdit([
// 'name'=>$request->get('First_Name'),
// 'email'=>$request->get('email'),
// 'username'=>$request->get('name')
//]);
//
//$user->save();
//return \Redirect::route('users')->with('message', 'Nice Work.');
}
public function show($id)
{
try {
$array = UserEdit::findorFail($id)->toArray();
return view('UserEdit')->with('array', $array);
} catch(\Exception $e) {
return \Redirect::route('users.index')
->withMessage('This user does not exist');
}
}
public function edit($id)
{
$user = UserEdit::findorFail($id);
return view('EditUser')->with('user',$user);
}
public function update($id, UserFormRequest $request)
{
$user = UserEdit::findorFail($id);
$user->update([
'name' => $request->get('name'),
'email' => $request->get('email')
]);
return \Redirect::route('users.edit', [$user->id])->with('message', 'Details Updated!');
}
public function destroy($id)
{
//
}
}
The Blade is here.
if you have a hard time finding the solution the easiest solution is using
Route::any('users/{user}', 'UserEntryController#update');
this allow you to access this action with any method type
OR
Route::match(array('get', 'put'), 'users/{user}', 'UserEntryController#update');
so you need 2 method which are
get -> view
put -> submit update
you can just indicate which method type you want to be accessible with in this action
i think you are using model in form.try this
{{ Form::open(['method' => 'put', 'route'=>['users.update', $user->id], 'class'=>'form']) }}
As per your route list and route put doesnt taking id so you get method not found exception
PUT users/{user} App\Http\Controllers\EditUserController#update
instead of using resouce just type each route for each method
Route::put('users/{user}', 'EditUserController #update');
It seems like after sorting out the routes, the issue fell to a bad capitalisation. $user->id should have been $user->ID.

BadMethodCallException in Builder.php

I'm not having much luck tying to get past the following error
BadMethodCallException in Builder.php
Call to undefined method Illuminate\Database\Query\Builder::posts()
The project I'm working on is a simple timeline for a social network.
Although I'm stuck when calling the following route (creating a post)
Route::group(['middleware' => ['web', 'auth']], function () {
Route::get('home', 'TimelineController#index');
Route::post('/posts', 'PostController#create');
});
POST returns the 500 error I specified above. A quick search for the "unimplemented" method posts() returns:
Searching 5094 files for "posts()"
C:\Users\Administrator\Code\instyle\app\User.php:
16 ];
17
18: public function posts()
19 {
20 return $this->hasMany('Instyle\Post');
C:\Users\Administrator\Code\instyle\app\Http\Controllers \PostController.php:
17 ]);
18
19: $createdPost = $request->user()->posts()->create([
20 'body' => $request->body,
21 ]);
Since I'm having trouble pinpointing this error I'll happily elaborate on any file you require.
Thanks!
As stated by Khalid Dabjan I modified the function "create" to include the following line "dd($request->user());"
The result was a 200 response from the server containing a JSON with all the info relevant to the logged in user.
My Post Controller is as follows:
<?php
namespace Instyle\Http\Controllers;
use Illuminate\Http\Request;
use Instyle\Http\Requests;
use Instyle\Http\Controllers\Controller;
use Instyle\Post;
class PostController extends Controller
{
public function create(Request $request, Post $post)
{
$this->validate($request, [
'body' => 'required|max:600',
]);
$createdPost = $request->user()->posts()->create([
'body' => $request->body,
]);
return response()->json($post->with('user')->find($createdPost->id));
}
}
I fixed the error myself thanks to #Khalid Dabjan poking in my code.
I was using the wrong file for my Users model, for some reason I was using two and the one that didn't have the posts() method was being applied.
I have no idea why I was using two User.php files but that is best left aside for another question since the answer to this question is now clear:
I wasn't implementing the fuction posts() on the correct model, that's why I was getting a "function does not exist" error.

Laravel 5: not able to use pagination, I get error "Call to undefined method Illuminate\Database\Eloquent\Collection::render()"

I am quite new to Laravel and started to play around with L5 and tried to paginate my data (from my HomeController method):
someMethod(){
...
$messages = Message::paginate(50)->sortByDesc('timestamp');
return view('home', ['messages' => $messages]);
}
in my home.blade view:
...
<div class="list-group">
#foreach($messages as $message)
#if(!$message->processed)
...
#endif
#endforeach
</div>
{!! $messages->render() !!}
...
I get the error below:
FatalErrorException in b706d42af4b0adc08aee8abb2fdf4ba9 line 105:
Call to undefined method Illuminate\Database\Eloquent\Collection::render()
in b706d42af4b0adc08aee8abb2fdf4ba9 line 105
I followed the logic from the Pagination doc page : https://laravel.com/docs/5.1/pagination#basic-usage and https://laravel.com/docs/5.1/pagination#displaying-results-in-a-view
You have an error in your code. Try this:
$messages = Message::orderBy('timestamp', 'desc')->paginate(50);

Laravel 4: Unable to generate a URL, as such route does not exist

Here is my routes.php:
Route::group(['prefix' => 'mine'], function () {
Route::get('/first', ['as' => 'mine.first', 'uses' => 'MyApp\Controllers\MyController#first']);
});
Here is my HTML/Twig file:
{{ form_open({'action': 'mine.first'}) }}
{{ form_submit('Start') }}
{{ form_close }}
And here is my controller:
class MyController extends BaseController {
public function first()
{
\View::make('stuff.mine.first'); //in folder app/views/stuff/mine
}
}
The error is "An exception has been thrown during the rendering of a template ("Unable to generate a URL for the named route "MyController#first" as such route does not exist.") in "stuff.show" at line 130."
All the answers on this topic that I've seen are to name the route, but I've already done that.
Also, when I go to the URL manually (localhost/mine/first), the screen is blank even though there is HTML in that file.
Any idea what's going on? Thanks.
mine.first is a route name, not an action.
Use:
{{ form_open({'route': 'mine.first'}) }}
As for the view, controller action need to return a Response (the View generates one), so you just need add the proper keyword:
public function first()
{
return \View::make('stuff.mine.first');
}

Categories