Laravel 5 blade template Undefined variable - php

I did a search and I want to display the result, but I cannot convey the variable to the view,
although I specify it in the controller.
My piece of view code:
<div class="search col-md-6">
<p>
Найти сотрудника по id
</p>
<form action="{{route('searchID')}}" class="search-id" method="GET">
<div class="row">
<div class="col-xs-10">
<div class="form-group">
<input class="form-control" name="id" required="" type="text" value="{{ old('id') }}">
</input>
</div>
</div>
<div class="col-xs-2">
<div class="form-group">
<input class="btn btn-info" type="submit" value="Искать">
</input>
</div>
</div>
</div>
{{$result}}
</form>
<div>
</div>
</div>
my route:
Route::match(['get', 'post'], 'searchID', 'SearchController#indexID')->name('searchID');
my method in the controller:
public function indexID(Request $request, View $view)
{
//$message= "Сотрудник не найден";
$id = $request->input('id');
dump($id);
$result = Staff::where('public_id', $id)->get();
if ($result == null) {
//dump($message);
return redirect()->back()->withInput($id);
} else {
dump($result);
return view('addworker')->with('result', $result);
}
}
But I constantly get an error: Undefined variable: result
I tried:
return view('addworker')->with($result);
and
return view('addworker',$result);
and
return view('addworker', ['result', $result]);
None of this helped me, I don't know what to do anymore
How to make the template access this variable only after the controller has been processed?

You can use compact for the same,
return view('addworker', compact('result'));
compact — Create array containing variables and their values

I hope this will help you
return view('addworker', ['result' => $result]);

You used the wrong syntax to send your variable to your view, there is a lot os ways to do that:
You could use the compact function:
return view('addworker', compact('result'));
You could use the with() method:
return view('addworker')->with('result', $result);
Or:
return view('addworker', ['result' => $result]);
You could also check the official documentation: click here

Related

laravel error "ArgumentCountError Too few arguments to function App\Http\Controllers\UserController::messagesend(), 1 passed 2 expected"

I'm trying to send a message to users through their user_id
this is my controller
public function messagesend(Request $request, $user_id){
$data = array('details'=>$request->details,
'email' =>$user_id->email);
Mail::send('transactionmessage', $data, function($message) use ($data){
$message->from ('test#test.com');
$message->to($request->email);
$message->subject($data['details']);
});
// echo $$request;
return redirect('ongoingstatus')->with('success','Message Sent!');
}
and my route is
Route::post('/messagesend', [UserController::class, 'messagesend'])->middleware(['auth'])->name('messagesend');
don't know why i'm getting this error
Too few arguments to function App\Http\Controllers\UserController::messagesend(), 1 passed in /home/swit/public_html/manager/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54 and exactly 2 expected
this is my blade.php
<form method="POST" action = "{{route('messagesend')}}">
#csrf
<input type = "hidden" value="{{$email}}" name ="email" />
<div class="row">
<div class="col-md-8">
<div class="tab-content profile-tab" id="myTabContent">
<div class="tab-pane fade show active" id="home" role="tabpanel" aria-labelledby="home-tab">
<textarea placeholder="Enter Message" name="details" rows="5" cols="60" id = "details"></textarea><br>
<button type = "submit" class = 'btn btn-outline-primary mr-2'>Send Message</button>
</div>
</div>
</div>
</div>
</form>
please help me out if you know where i'm wrong
thanks
Your route doesn't take any parameter:
Route::post('/messagesend', [UserController::class, 'messagesend'])->middleware(['auth'])->name('messagesend');
However, the controller method needs two:
public function messagesend(Request $request, $user_id){
$data = array('details'=>$request->details,
'email' =>$user_id->email);
Mail::send('transactionmessage', $data, function($message) use ($data){
$message->from ('test#test.com');
$message->to($request->email);
$message->subject($data['details']);
});
// echo $$request;
return redirect('ongoingstatus')->with('success','Message Sent!');
}
The first argument, Request $request, is injected by Laravel using dependency injection.
The second needs to be retrieved from the route, but... their is no parameters.
You have two options here, depending of what you are trying to achieve:
Adding a parameter to your route:
Route::post('/messagesend/{user_id}', [UserController::class, 'messagesend'])->middleware(['auth'])->name('messagesend');
Remove the argument $user_id from your controller method.
All the form does is POST a Request $request to your Route. You aren't passing the messagesend() function a $user_id.

how can insert data post with get route in Laravel

how do i send a form POST method with GET route in laravel?
Route
Route::get('domain_detail/{domain_name}','domain_detailController#index');
View domain_detail folder
<form method="post" action="{{url('domain_detail')}}/{{strtolower($domain_detail->domain_name)}}">
<div class="form-group">
<label for="namefamily">namefamily</label>
<input type="text" class="form-control round shadow-sm bg-white text-dark" name="namefamily">
</div>
<div class="form-group">
<label for="mobile">mobile</label>
<input type="text" class="form-control round shadow-sm bg-white text-dark" name="mobile">
</div>
<div class="form-group">
<label for="myprice">myprice</label>
<input type="number" class="form-control round shadow-sm bg-white text-dark" name="myprice">
</div>
<div class="form-group">
<input type="submit" name="send_price" class="btn btn-success" value="submit">
</div>
</form>
Controller
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
class domain_detailController extends Controller
{
public function index($domain_name)
{
$domain_detail_exist = DB::table("domains")->where('domain_name', $domain_name)->exists();
if ($domain_detail_exist) {
$domain_detail = DB::table("domains")->where('domain_name', $domain_name)->first();
return view('domain_detail/index', ['domain_detail' => $domain_detail]);
} else {
return view('404');
}
}
public function create()
{
return view('domain_detail.index');
}
}
At the controller i didn't put any codes in the create function, but when i click on submit button in form i get this error
Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException
The POST method is not supported for this route. Supported methods:
GET, HEAD.
Use the index function in your domain_detailController just so return the view.
like this:
public function index($domain_name)
{
return view('domain_detail.index');
}
create a route to return the view:
Route::get('domain_detail/','domain_detailController#index');
Then use the create function to store the domain detail like this:
public function create($domain_name)
{
$domain_detail_exist = DB::table("domains")->where('domain_name', $domain_name)->exists();
if ($domain_detail_exist) {
$domain_detail = DB::table("domains")->where('domain_name', $domain_name)->first();
return view('domain_detail/index', ['domain_detail' => $domain_detail]);
} else {
return view('404');
}
}
make a POST route like this:
Route::post('domain_detail/','domain_detailController#create');
Also take a look at the laravel best practices when it comes to naming conventions:
https://www.laravelbestpractices.com/

Laravel Error: The POST method is not supported for this route. Supported methods: GET, HEAD

Good evening , for school i am trying to create a simple CRUD app, using laravel 6 and mongoDB.
I can get read, update and delete working but creat fails with The POST method is not supported for this route. Supported methods: GET, HEAD.. I have searched the answers here and other sites but im stuck for 2 days now (could be something very silly but im not seeing it)
my routes are:
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/post/{_id?}', 'PostController#form')->name('post.form');
Route::post('/post/save/', 'PostController#save')->name('post.save');
Route::put('/post/update/{_id}', 'PostController#update')->name('post.update');
Route::get('/post/delete/{_id}', 'PostController#delete')->name('post.delete');
form.blade is:
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Post Form</div>
<div class="card-body">
#if($data)
<form action = "{{Route ('post.update', $data->_id)}}" method="post">
#csrf
#method('PUT')
<div class="form-group">
<label for="usr">Title:</label>
<input type="text" class="form-control" name="title" value = "{{$data->title}}" >
</div>
<div class="form-group">
<label for="comment">Content:</label>
<textarea class="form-control" rows="5" name="content">{{$data->content}}</textarea>
</div>
<p align="center"> <button class="btn btn-primary">save</button></p>
</form>
#else
<form action = "{{Route ('post.form')}}" method="post">
#csrf
<div class="form-group">
<label for="usr">Title:</label>
<input type="text" class="form-control" name="title">
</div>
<div class="form-group">
<label for="comment">Content:</label>
<textarea class="form-control" rows="5" name="content"></textarea>
</div>
<p align="center"> <button class="btn btn-primary">save</button></p>
</form>
#endif
</div>
</div>
</div>
</div>
#endsection
and my PostController is:
<?php
namespace App\Http\Controllers;
use App\Post;
use Illuminate\Http\Request;
class PostController extends Controller
{
//
public function form($_id = false){
if($_id){
$data = Post::findOrFail($_id);
}
$data = false;
return view ('post.form', compact('data'));
}
public function save (Request $request){
$data = new Post($request->all());
$data->save();
if($data){
return redirect()->route('home');
}else{
return back();
}
}
public function update (Request $request, $_id){
$data = post::findOrFail($_id);
$data->title = $request->title;
$data->content = $request->content;
$data->save();
/* return response()->json([
'name' => 'Abigail',
'state' => 'CA'
]); */
if($data){
return redirect()->route('home');
}else{
return back();
}
}
public function delete($_id){
$data = post::destroy($_id);
if($data) {
return redirect()->route('home');
}
else {
dd('error cannot delete this post');
}
}
}
Anybody any idea what i am missing?
Thanks in advance
You have to replace this line <form action = "{{Route ('post.form')}}" method="post"> with <form action = "{{Route ('post.save')}}" method="post">
You are using wrong route. Please change to Route ('post.save')
EDIT: I found that one myself, the PostControler didnt return a view if there was an $_id
Thanks for the help everyone!
Thanks for pointing that out, it did bring my from back to life :) However it breaks the update function :S.
When i now click on the edit button, the form does no longer get filled with the data for the post, and "save" creates a new post in stead of updating it.

Laravel Search function

i have a question about the Laravel search function, i had follow the guildeline online and i still fail to search the category, can someone guide me and tell me where i did wrongly ? Much appreciated
My category Controller php code:
public function search(Request $request)
{
$search = $request->get('search');
$posts = DB::table('bit_app_policy_category')->where('id','like','%' .$search. '%')->paginate(5);
return view('category.index',['posts' => $posts]);
}
My index.blade code
<div align="left">
<div class="col-md-4">
<h1>Policy</h1>
</div>
<div class="col-md-4">
<form action="/search" method="get" role="search">
{{ csrf_field() }}
<div class="input-group">
<input type="text" class="form-control" name="_method" placeholder="Search ID / Code"> <span class="input-group-btn">
<button type="submit" class="btn btn-primary">Search</button></span>
</div>
</form>
</div>
</div>
web.php
Route::get('/search','categoryController#search');
What error i get is here
Error image
interface
Database
You are sending $posts variable to your view. But the error says you are referencing a $category variable.
return view('category.index',['posts' => $posts]);
Maybe you might want to update view to use $posts. If you could post your full code (category/index.blade.php) we might be able to help you better.
__
Here is how I would do:
$categories= DB::table('bit_app_policy_category')->where('id','like','%' .$search. '%')->paginate(5);
return view('category.index',['categories' => $categories]); //you can also use compact return view('category.index', compact('categories') );
And to display:
#foreach( $categories as $category )
<div>{{ $category->id }}</div>
#endforeach
Another tip: you can name your routes like so
Route::get('search','categoryController#search')->name('search');
Then you can reference this route (in form or anywhere else you want) like so:
<form action="{{ route('search') }}" ..>

How to show message after filter in laravel?

After selecting Filter,I want to show summary tab/message what we selected.
I googled it and found session method, is it suitable in my case?
Here is my blade
{!! Form::open(['url'=>'/jobseekers','method'=>'GET', 'class'=>'form', 'id'=>'search_data']) !!}
<div class="form-group col-md-4">
<input type="text" name="fullname" placeholder="Name" value="{{ request()->input('fullname')}}" class="form-control"/>
</div>
<div class="form-group col-md-4">
<input type="text" name="fb_name" placeholder="Fb Name" value="{{ request()->input('fb_name')}}" class="form-control"/>
</div>
<button class="btn btn-flat btn-primary">Search</button>
</div>
{!! Form::close() !!}
and in my controller
public function index(Request $request)
{
$result = null;
if(count($request->all())!=0){
if ($request->has('sub_search')) {
$jobseekers = Jobseeker::Subsearch($request)->paginate(10);
dd($applicant_information);
}else{
$result=Jobseeker::Search($request)->paginate(10);
// dd($orders);
}
}
else{
$jobseekers = Jobseeker::with('calllogs')->orderBy('created_at', 'desc')->paginate(16);
}
return view('backend.jobseekers.index',compact('jobseekers','result'));
}
I am using get method to filter,and i want to show like
The Search results for fullname and fb_name are:
Is there any way to do like that in my case? Please guide me, thanks.
Are you trying to display filtered result in view? If so, change your code to:-
public function index(Request $request)
{
$fullname = $request->fullname;
$fb_name= $request->fb_name;
$result = null;
if(count($request->all())!=0){
if ($request->has('sub_search')) {
$jobseekers = Jobseeker::Subsearch($request)->paginate(10);
dd($applicant_information);
}else{
$result=Jobseeker::Search($request)->paginate(10);
// dd($orders);
}
}
else{
$jobseekers = Jobseeker::with('calllogs')->orderBy('created_at', 'desc')->paginate(16);
}
return view('backend.jobseekers.index',compact('jobseekers','result'))->with('fullname',$fullname)->with('fb_name',$fb_name);
}
All you need to is to access the passed variable from this controller is like
The Search results for {{$fullname}} and {{$fb_name}} are:
and loop your result here...

Categories