Laravel form unexpected 'namespace' - php

I try to do a basic form and I did that 1 week ago but this time I have a new bug surely a stupid mistake but...
So here is my UsersController
<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
use App\Http\Requests;
use App\Http\Controllers\Controller;
  
class UsersController extends Controller
      
      
{
    public function getInfos()
          
          
    {
        return view('infos');
    }
      
      
    public function postInfos(Request $request)
    {
        return 'His name ' . $request->input('firstname');
 
    }
}
My infos.blade
#extends('template')
#section('contenu')
{!! Form::open(['url' => 'users']) !!}
{!! Form::label('frstname', 'Your name : ') !!}
{!! Form::text('firstanme') !!}
{!! Form::submit('Send !') !!}
{!! Form::close() !!}
#endsection
And the web.php
Route::get('users', 'UsersController#getInfos');
Route::post('users', 'UsersController#postInfos');
I use a template but I'm almost sure that's not the problem , I think I do bad routing , I have this error message
syntax error, unexpected 'namespace' (T_NAMESPACE)
Thx to help me :)
ps : the error message

Since ypur namespace suggests the said controller resides in the same directory as the extended controller you dont need to specify this.
Also depending on the laravel version you may require running
php artisan dump:autoload

Related

Laravel Route [blood-camp] not defined exception

I'm getting this error even though the route is defined
Route [blood-camp] not defined. (View: C:\wamp64\www\blood-donation\resources\views\layouts\app.blade.php)
web.php file
`Route::get('/', [App\Http\Controllers\HomeController::class, 'index'])->name('home.root');
Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::get('/user', [App\Http\Controllers\UserController::class, 'index'])->name('user');
Route::resource('blood-camp', BloodCampController::class);
Route::resource('donor', DonorController::class);
Route::resource('camp-schedule', CampScheduleController::class);`
app.blade.php
<li class="nav-item dropdown">
<a class="nav-link" href="{{ route('blood-camp') }}" role="button" >
{{ __('Camps') }}
</a>
</li>
controller class
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class BloodCampController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
return view('blood_camp.index', ['camps' => DB::table('blood_camps')->orderBy('name', 'ASC')->paginate(10)]);
}
//all the other resources are there...
}
route:list
There is no duplicate route
I have tried all the suggested ways including changing path names, cache clearing, private window, restarting server, but I'm still getting this error and no way to go further developments.
It first occurred when I adding new route called camp, I changed the name to blood-camp but no luck, now it throw exceptions for the other routes as well. Can someone please explain me what I'm doing wrong?
As the result of your route:list shows, you dont have a route with the alias blood-camp.
What you have as alias is blood-camp.index, blood-camp.store....
To generate the link of the ressources listing use:
By path/url
{{ url('blood-camp') }}
Or by alias
{{ route('blood-camp.index') }}

Laravel mail adds <br /> to the first line of mail and then proper end of lines

I am building a mail for my application but i have been trying to use nl2br in it because people can write in it. Now i have got this piece of code:
{!! nl2br($text['text']) !!}
I have put it in an array because someone said that works, well it does not really, my result is really strange:
So it adds add the end of the first line and then adds proper line endings. I have never seen this behaviour before.
This is the complete code in the blade markdown file:
#component('mail::layout')
{{-- Header --}}
#slot('header')
#component('mail::header', ['url' => config('app.url')])
<img src="https://www.cpned.nl/wp-content/uploads/2014/12/cpned-logo.png" height="75px">
#endcomponent
#endslot
<h1>Goedendag!</h1>
{!! nl2br($text['text']) !!}
Met vriendelijke groeten, {{PHP_EOL}}
{{PHP_EOL}}
Het Circulation Practitioners Intranet.
{{-- Footer --}}
#slot('footer')
#component('mail::footer')
© {{ date('Y') }} Circulation Practitioners. Alle rechten voorbehouden <br/>
Deze mail is gegenereert door het intranet, u kunt hierop niet reageren!
#endcomponent
#endslot
#endcomponent
The following is in my controller:
Mail::to($users)->send(new Reminder($request->subject, array('text' => $request->message)));
And then the last file i have got:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class Reminder extends Mailable
{
use Queueable, SerializesModels;
public $subject;
public $text;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($subject, $text)
{
$this->subject = $subject;
$this->text = $text;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this
->subject($this->subject)
->markdown('emails.reminder');
}
}
I have been struggling with this for hours and I am not abled to spot the problem what is causing this strange behaviour.
Thanks in advance!
Have you tried out to replace
{!! nl2br($text['text']) !!}
by {!! nl2br(e($text['text'])) !!}
?
https://laravel.com/docs/5.1/helpers#method-e

Laravel 5 error message if keywords not exist

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']);

How to update exsiting row in database in laravel5

I am new in laravel and I want to update an existing row in database,but when I click on send button in view (for example 127.0.0.1/laravel/public/Article/update/3 ) I encounter the following error:
MethodNotAllowedHttpException in RouteCollection.php line 201:
Here is my code
Route
Route::get('Article/edit/{id}','ArticleController#edit');
Route::get('Article/update/{id}','ArticleController#update');
ArticleController
public function edit($id)
{
$change = Article::find($id);
return view('edit',compact('change'));
}
public function Update($id, Request $request)
{
Article::update($request->all());
return redirect('Article');
}
Model
public $table = 'Article';
protected $fillable = ['title' , 'body'];
edit.blade.php
<h1>ویرایش بست {{$change->title}}</h1>
{!! Form::model($change ,['method'=>'patch' , 'url'=>['Article/update' , $change->id ]]) !!}
{!! Form::label('title','عنوان') !!}
{!! Form::text('title') !!}
<br>
{!! Form::label('body','متن') !!}
{!! Form::textarea('body') !!}
<br>
{!! Form::submit('send') !!}
{!! Form::close() !!}
#if($errors->any())
<ul class ='alert alert-danger'>
#foreach($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
#endif
The easiest way to resolve routing issues with Laravel is to use 'artisan'.
http://laravel.com/docs/5.1/artisan
If you use this command:
php artisan route:list
You'll see every possible route and HTTP verb available for use. Your error is in the RouteCollection so you can always fix these issues by looking at your app/http/routes.php file.
You defined a route as follows:
Route::get('Article/update/{id}','ArticleController#update');
Then you call that route via your form as follows:
{!! Form::model($change ,['method'=>'patch' , 'url'=>['Article/update' , $change->id ]]) !!}
Your routes.php GET definition does not match your form's PATCH method, so you're getting a method not allowed exception because the PATCH route is not defined.
You need this line of code in your routes.php file:
Route::patch('article/update/{id}','ArticleController#update');
I would highly recommend using this instead of defining each method individually:
Route::resource('article', 'ArticleController');
Then run the following command again with artisan to see all routes created:
php artisan route:list

Laravel 5 - FatalErrorException: Class 'User' not found

I am very new to Laravel and just started setting up things with Laravel 5. I am attempting to create a simple user authentication app with Laravel.
I created register.blade.php which includes form to register user.
Here is my routes.php
Route::post('/register', function()
{
$user = new User;
$user->email = Input::get('email');
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'));
$user->save();
$theEmail = Input::get('email');
return View::make('thanks')->with('theEmail', $theEmail);
});
Here is the section of register.blade.php which creates form for the user registration.
{!! Form::open(array('url' => 'register')) !!}
{!! Form::label('email', 'Email Address') !!}
{!! Form::text('email') !!}
{!! Form::label('username', 'Username') !!}
{!! Form::text('username') !!}
{!! Form::label('password', 'Password') !!}
{!! Form::password('password') !!}
{!! Form::submit('Sign Up') !!}
{!! Form::close() !!}
I am getting this error when I click on Sign Up button on register page.
in routes.php line 30
at HandleExceptions->fatalExceptionFromError(array('type' => '1', 'message' => 'Class 'User' not found', 'file' => 'C:\xampp\htdocs\urlshort\app\Http\routes.php', 'line' => '30')) in HandleExceptions.php line 116
at HandleExceptions->handleShutdown()
After going through few queries in Google, I realized I forgot to load the User class. So, I included link to file with User class in composer.json file
"autoload": {
"classmap": [
"database",
"app/User.php"
],
"psr-4": {
"App\\": "app/"
}
},
I ran the composer dump-autoload command. But I am still getting the same error. I am unable to figure out where my code went work.
try to use
$user = new \App\User;
instead
$user = new User;
I had the same issue, the answer above didn't help, but I used
use App\Models\Access\User\User; instead of use User; in my controller and it worked.
Type App\Models\User it worked for me

Categories