I am getting this error (Route [companies.show] not defined.) and I don't know what to do.
Actually I am updating the data in CompaniesController and data is updating but the route is not working
Here is the code for that:
public function update(Request $request, Company $company){
$companyUpdate = Company::where('id', $company->id)->update(['name'=> $request->input('name'),'description'=> $request->input('description')]);
if($companyUpdate){
return redirect()->route('companies.show', ['company'=> $company->id])
->with('success' , 'Company updated successfully');
}
return back()->withInput();
And My web.php file is as follow `
Route::get('/', function () {
return view('welcome');});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('/company','CompaniesController');
Thanks in advance for helping me
change companies.show to
return redirect()->route('company.show', ['company'=> $company->id])
->with('success' , 'Company updated successfully');
}
companies.show is undefined because you didn't give your route a name.
Route::get('/companies/{id}', 'CompaniesController#showCompanyForID')->name('companies.show');
Create a function called showCompanyForID in your CompaniesController and return the company which has the id requested for in your Request.
use Illuminate\Http\Request;
public function showCompanyForID(Request $request)
{
$id = isset($request->id) ? $request->id : 0;
if ($id) {
// do work here
}
return view('companies.company')->with(compact('var1', 'var2'));
}
You can now redirect to that route:
return redirect()
->route('companies.show')
->with(['company'=> $company->id, 'success' => 'Company updated successfully']);
To see all routes, cd to your project in cmd / Terminal and type: php artisan route:list
Related
I'm new to Laravel and I'm trying to figure out how to send confirmation emails to customers after they place an order.
I've read a few articles about it but couldn't find the answer I needed.
The following are the things I have done with the order process:
I have and Order controller which collects the informations I need when a customer pay for his products and save them to the database:
public function index()
{
$order = Order::all();
return response()->json(
[
'results' => $order,
'success' => true
]
);}
public function create(Request $request)
{
$data = $request->all();
$order = new Order();
$order->fill($data);
$order->save();
return response()->json([
'order' => 'New order created'
]);}
This is the Model:
class Order extends Model
{protected $fillable = ['customer_name','customer_address', 'customer_telephone','total','customer_email','user_id','cart'];
public function dishes() {
return $this->belongsToMany('App\Dish');
}}
And this is my web.php file:
Auth::routes();
Route::middleware('auth')
->namespace('Admin')
->name('admin.')
->prefix('admin')
->group(function () {
Route::get('/', 'HomeController#index')->name('home');
Route::resource('orders', 'OrderController');
Route::resource('restaurants', 'RestaurantController');
});
Route::get('{any?}', function() {
return view('guests.home');})->where('any', '.*');
And my api.php:
Route::get('/restaurants', 'Api\RestaurantController#index');
Route::get('/restaurant/{slug}','Api\RestaurantController#show');
Route::get('/users', 'Api\UserController#index');
Route::get('/categories', 'Api\CategoryController#index');
Route::post('/orders', 'Api\OrderController#store');
Can someone guide me step-by-step on what to do to collect the current email address that a customer has used and then send them a confirmation email?
Any help (also links to tutorials dealing with this situation) will be really appreciated
I want to go from home.blade.php to all_article.blade.php, but they tell me that such a route was not found. What am I doing wrong. Thanks in advance for your help.
blog/home.blade
<i class="fa fa-plus-square-o"></i> Add
BlogController
public function articlesAll_blade(){
return view('blog.all_article',[
'articles' => Article::orderBy('created_at', 'desc')->paginate(10),
'footers' => System::all(),
]);
}
web.php
Route::get('/', 'BlogController#articlesAll', function () {
return view('blog.home');
});
Route::get('/all_article', 'BlogController#articlesAll_blade', function () {
return view('blog.all_article');
});
what you are missing is a route name, add a name to your route
Route::get('/all_article', 'BlogController#articlesAll_blade', function () {
return view('blog.all_article');
})->name('blog.all_article');// see the name part
doc link https://laravel.com/docs/routing#named-routes
Route::get('/all_article','BlogController#articlesAll_blade')->name('blog.all_article');
When update function get completed it should redirect to another page sample.blade. But it shows the error not defined Here is my code,
In PassengerController,
public function update(Request $request, $id)
{
$this->validate($request, [
'name' => 'required',
'email' => 'required',
]);
Move::find($id)->update($request->all());
return redirect()->route('Move.sample')
->with('success','updated successfully');
//return redirect('Move.sample');
}
My routes.php,
Route::resource('Move', 'PassengerController');
Should I defined this route in routes?
You don't have a route named Move.sample
return redirect()->route('move.index')
->with('success','updated successfully');
Route::resource generates the following named routes:
move.index
move.create
move.store
move.edit
move.update
move.destroy
If you have sample blade inside Move folder, Use return view('Move.sample')
return view('Move.sample')
->with('success','updated successfully');
You can only use route if you defined route for sample view
redirect()->route('/sample');
And set route for this like
Route::get('/sample', 'PassengerController#getSample');
And return view Move.sample in getSample function inside PassengerController
return view('Move.sample')->with('success','updated successfully');
Check official doc says about resources
I am calling getting_started route after successfully login :
protected $redirectTo = '/getting_started';
Here is my getting_started route code :
Route::get('/getting_started','UserController#getting_started');
And controller code :
public function getting_started()
{
$id= Auth::id();
$user = DB::table('user_profiles')->where('user_id', '=', $id)->first();
if($user->dashboard_access == 0)
{
DB::table('user_profiles')
->where('user_id', $id)
->update(['dashboard_access' => 1]);
return view('user.getting_started');
}
return view('user.dashboard');
}
It works perfectly and show in url :
http://localhost:8080/getting_started
Now I actually want that if user.dashboard view is call it show in url like :
http://localhost:8080/dashboard`
And on getting_started view show :
http://localhost:8080/getting_started
It is possible to call dashboard route instead of :
return view('user.dashboard');
My dashobard route is :
Route::get('/dashboard',['middleware' => 'auth', function () {
return view('user.dashboard');
}]);
What I understand it is that you are looking for is this function
return redirect()->route('dashboard');
It's my understanding of your question which can be wrong. Maybe you are asking something else.
That called Redirection and especially you want to Returning A Redirect To A Named Route, you route called user.dashboard so you could redirect to it using redirect()->route(route_name) :
return redirect()->route('user.dashboard');
Hope this helps.
I am using Hashid to hide the id of a resource in Laravel 5.
Here is the route bind in the routes file:
Route::bind('schedule', function($value, $route)
{
$hashids = new Hashids\Hashids(env('APP_KEY'),8);
if( isset($hashids->decode($value)[0]) )
{
$id = $hashids->decode($value)[0];
return App\Schedule::findOrFail($id);
}
App::abort(404);
});
And in the model:
public function getRouteKey()
{
$hashids = new \Hashids\Hashids(env('APP_KEY'),8);
return $hashids->encode($this->getKey());
}
Now this works fine the resource displays perfectly and the ID is hashed.
BUT when I go to my create route, it 404's - if I remove App::abort(404) the create route goes to the resource 'show' view without any data...
Here is the Create route:
Route::get('schedules/create', [
'uses' => 'SchedulesController#create',
'as' => 'schedules.create'
]);
The Show route:
Route::get('schedules/{schedule}', [
'uses' => 'Schedules Controller#show',
'as' => 'schedules.show'
]);
I am also binding the model to the route:
Route::model('schedule', 'App\Schedule');
Any ideas why my create view is not showing correctly? The index view displays fine.
Turns out to solve this, I had to rearrange my crud routes.
Create needed to come before the Show route...
There's a package that does exactly what you want to do: https://github.com/balping/laravel-hashslug
Also note, that it's not a good idea to use APP_KEY as salt because it can be exposed.
Using the above package all you need to do is add a trait and typehint in controller:
class Post extends Model {
use HasHashSlug;
}
// routes/web.php
Route::resource('/posts', 'PostController');
// app/Http/Controllers/PostController.php
public function show(Post $post){
return view('post.show', compact('post'));
}