Version of Laravel 5.0 and following is my code:
My form:
<form method="get" action="{{ url('result')}}" class="search-wrap" >
<div class="form-group">
<input type="text" class="form-control search" placeholder="Search" name="key"/>
<button class="btn btn-primary submit-search text-center" type="submit">
<i class="icon-search"></i>
</button>
</div>
</form>
My controller:
use Illuminate\Http\Request;
use App\Http\Requests;
use App\products;
public function searchName(Request $request){
$key = $request->key;
$data = product::where('name','like','%'.$key.'%')->get();
return view('footwear/result',['search'=>$data]);
}
My route:
Route::get('result', 'myControler#searchName');
My view:
<?php
var_dump($search);
?>
I receive the following error: (Undefined variable: search)
Problem is in your routes, why don't you
Try:
Route::post('result', 'myControler#searchName');
or
Route::any('result', 'myControler#searchName');
change method to post in form too, and try.
NOTE: I should have added this as comment but my rep is less than 50.
Related
i have littel problem in my code, i dont know write mastake but i check my code is good
This ShopController
`
public function show($id)
{
$product = Product::findOrFail($id);
return view('shop.show');
}
`
this my route
`
Route::get('/shop/detail/{id}', 'ShopController#show');
`
this my view
`
<div class="container">
<h2 class="title">{{$product->name}}</h2>
<hr>
<div class="row">
<div class="wrapper">
<div class="col-lg-4" id="picture">
<img src="{{asset($product->image)}}" alt="" height="200" width="200">
</div>
</div>
<div class="col-lg-4 desc">
<h4 id="description">Description</h4>
<p>{{$product->desc}}</p>
</div>
<div class="col-lg-4">
<div class="kartu">
<p>Harga</p>
<h2>Rp {{number_format($product->price)}}</h2>
<form action="" method="POST">
#csrf
<input type="hidden" value="" name="item_id">
<input type="submit" class="btn btn-primary" value="Add to Cart">
</form>
</div>
</div>
</div>
</div>
`
I have checked my code and there are no errors
You don't pass the product to the view. You need to compact the variable like this in the controller:
public function show($id)
{
$product = Product::findOrFail($id);
return view('shop.show', compact('product'));
}
you are getting product from database in $product variable, but not passing that variable to your view. There are several ways to pass variables to view.
return view('shop.show', compact('product'));
OR
return view('shop.show', ['product' => $product]);
OR
return view('shop.show', get_defined_vars());
get_defined_vars() is built-in php function, by using this function any numbers of variables declared in method, all will be passed to view
Hello i am trying to change 2 values from a table in DB, using Laravel 8, but for some reason, when i press the button to update my data, i get a 404 not found. However i have the route defined, and it appears on the list when i use php artisan routes:list I also tried clearing all cache , and it still doesn't work
My controller
public function confirmar_pedido_mentoria(Request $request){
$user = User::findOrFail($request->mentorando_id);
$user -> area_interesse = $request->area_interesse;
$user -> pedido_mentoria = 1;
$user -> update();
return redirect('/admin/pedido_mentoria/{{$user->id}}')->with('msg', 'Pedido realizado com sucesso!');
}
My routes
Route::get('/admin/pedido_mentoria/{id}', [UsersController::class, 'pedido_mentoria'])->middleware('auth');
Route::put('/admin/confirmar_pedido/{id}', [UsersController::class, 'confirmar_pedido_mentoria'])->middleware('auth');
I put here the get route, because that is the route i intend to user when i redirect after updating the values
My blade view
<?php
$inter = $user->interesses->pluck('id')->toArray();
?>
<h1 style="font-family: Eczar">Efectuar Pedido de Nova Mentoria: {{$user->user}}</h1>
<div class="col-md-6 offset-md-3">
<form action="/admin/confirmar_pedido/{{$user->id}}" method="POST" enctype="multipart/form-data" name="Form" onsubmit="return validateForm()">
#csrf
#method('PUT')
<div class="form-group">
<p style="font-family: Eczar">Escolha a Área de Interesse</p>
<div class="form-group">
#foreach($interesses as $interesse)
<input type="radio" name="area_interesse[]" class="area_interesse" value="{{$interesse->id}}" #if (in_array($interesse->id, $inter)) checked="checked" #endif > {{$interesse->area_interesse}} <br/>
#endforeach
</div>
<input type="submit" class="btn btn-primary" value="Escolher">
</form>
</div>
You have to get your id parameter in controller function(confirmar_pedido_mentoria).
Edit your code :
public function confirmar_pedido_mentoria(Request $request,$id){
....
}
I am getting an error in laravel 5.2
when I access it using the storeMessage function on my Controller
This is my ERROR STATEMENT:
ErrorException in 6e12af76911d23528f6e0ea587ccab13391eacf9.php line 37:
Undefined variable: thisPage (View: E:\xampp\htdocs\beasiswa\resources\views\layouts\partials\sidebar.blade.php) (View: E:\xampp\htdocs\beasiswa\resources\views\layouts\partials\sidebar.blade.php) (View: E:\xampp\htdocs\beasiswa\resources\views\layouts\partials\sidebar.blade.php)
And this is my code:
View Code
<?php $thisPage = "jenisbeasiswa"; ?>
#extends('layouts.app')
#section('htmlheader_title')
Portal Beasiswa
#endsection
#section('contentheader_title')
Broadcast Beasiswa via Telegram Messenger
#endsection
#section('main-content')
<form action="{{ url('/send-message') }}" method="post">
{{ csrf_field() }}
<div class="form-group">
<label for="email">Email address</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea name="message" id="message" class="form-control" placeholder="Enter your query" rows="10"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
#endsection
and this is my Controller Code:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Telegram\Bot\FileUpload\InputFile;
use Telegram\Bot\Laravel\Facades\Telegram;
class TelegramBotController extends Controller
{
public function updatedActivity()
{
$activity = Telegram::getUpdates();
dd($activity);
}
public function sendMessage()
{
return view('beasiswa.telegram');
}
public function storeMessage(Request $request)
{
$request->validate([
'email' => 'required|email',
'message' => 'required'
]);
$text = "[HEADLINE] Informasi Portal Beasiswa\n"
. "<b>Email Address: </b>\n"
. "$request->email\n"
. "<b>Message: </b>\n"
. $request->message;
Telegram::sendMessage([
'chat_id' => env('TELEGRAM_CHANNEL_ID', ''),
'parse_mode' => 'HTML',
'text' => $text
]);
return redirect()->back();
}
}
Apparently sidebar.blade.php is referring to the $thisPage variable (which is declared at the top of your view code). My guess is, you are including the sidebar in the layouts.app template. But as you extend your layouts.app the $thisPage variable will get rendered in the sidebar first before the declaration comes in your view code. And thus, it will be undefined in the Sidebar.
So, you should try another approach. Why do you even want to set the $thisPage variable? Maybe you can pass it in from the controller?
Hey I am working on a code in which when user clicks update button it will go to another page that is doctorEdit. I defined the route in my web file but its again and again giving error route not defined. Can anyone please help me to reslove my problem. Below is my code.
Route code:
Route::resource('doctor/doctorEdit','DoctorController#edit');
Controller code
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
class DoctorController extends Controller
{
public function edit()
{
return view('doctor.doctorEdit');
}
}
And my view code is
<form class="row" method="POST" action="#" onsubmit = "return confirm('Are you sure?')">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<a href="{{ route('doctor/doctorEdit', ['id' => $doctor->id]) }}" class="btn btn-warning col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Update
</a>
<button type="submit" class="btn btn-danger col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Delete
</button>
</form>
Please let me know what I am doing wrong because I am new at laravel.
Try using GET method with a named route.
Your routefile
Route::get('doctor/doctorEdit','DoctorController#edit')->name('doctor.edit');
Your view code
<form class="row" method="POST" action="#" onsubmit = "return confirm('Are you sure?')">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<a href="{{ route('doctor.edit', ['id' => $doctor->id]) }}" class="btn btn-warning col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Update
</a>
<button type="submit" class="btn btn-danger col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Delete
</button>
</form>
You're doing it wrong, take a look at the documentation on Resource Controllers.
Basically if you define a resource with Route::resource() method, you must not specify the controller action, since the Resource Controller is expected to provide compatible REST methods.
So if you want to create a REST resource controller you must specify your route as:
Route::resource('doctor', 'DoctorController');
so you have to specify just your controller class name.
Then in your controller you have to specify required methods:
class DoctorController extends Controller
{
public function index()
{
// GET yourapp.com/doctor -> typically return all doctors
}
public function create()
{
// GET yourapp.com/doctor/create -> typically show doctor creation form
}
public function show()
{
// GET yourapp.com/doctor/{doctor_id} -> show a single doctor
}
public function store()
{
// POST yourapp.com/doctor -> create a new doctor
}
public function edit()
{
// GET yourapp.com/doctor/{doctor_id}/edit -> show edit form view
return view('doctor.doctorEdit');
}
public function update()
{
// PUT|PATCH yourapp.com/doctor/{doctor_id} -> update a doctor
}
public function destroy()
{
// DELETE yourapp.com/doctor/{doctor_id} -> delete a doctor
}
}
If you just want to expose the edit form without the REST logic use the Request::get() method:
Request::get('doctor/doctorEdit', 'DoctorController#edit')->name('doctor.edit');
If your are caching your routes remember to refresh them using the artisan command sequence
php artisan route:clear
php artisan route:cache
or more concisely just
php artisan route:cache
You're defining a resource. The correct way to do this is:
Route::resource('doctor','DoctorController');
The name for the DELETE method is doctor.destroy, and the controller method is destroy
Your view code should be
<form class="row" method="POST" action="{{ route('doctor.delete') }}" onsubmit = "return confirm('Are you sure?')">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<a href="{{ route('doctor.edit', ['id' => $doctor->id]) }}" class="btn btn-warning col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Update
</a>
<button type="submit" class="btn btn-danger col-sm-3 col-xs-5 btn-margin" style="width:100px; margin-left:20px;">
Delete
</button>
</form>
here is my route
Route::post('/posts', 'Cdesigination#index');
HTNL and input Field
<div class="form-group">
<label class="control-label">Name</label>
<input type="text" class="form-control" name="name" id="name" data-validate="required" placeholder="Enter Name" />
</div>
<div class="form-group">
<label class="control-label">Detail</label>
<textarea class="form-control" name="detail" id="detail" placeholder="Enter Detail"></textarea>
</div>
<input type="hidden" id="_token" name="_token" value="{{ csrf_token() }}">
<div class="form-group col-sm-offset-3">
<button type="submit" onclick="postdata();" id="post" class="btn btn-success">Submit Now</button>
</div>
my controller file
<?php namespace App\Http\Controllers;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests;
use App\models\Designation as desig;
class Cdesigination extends Controller
{
public function index()
{
return view('designation');
}
}
where is the error have trouble me get method working fine but post method show error as soon as change
this route show following error
Route::post('/posts', 'Cdesigination#index');
MethodNotAllowedHttpException in RouteCollection.php line 219:
as soon as change post to get like Route::get('/posts', 'Cdesigination#index');
error not show working fine tell me how solve it
Make sure your <form> has method="" that matches your route declaration
<form method="post">
for
Route::post('/posts', 'Cdesigination#index');
Or
<form method="get">
for
Route::get('/posts', 'Cdesigination#index');