Laravel parameters without explicitly specifying - laravel-5

I want to generate the page url without $name, something like this
mysite.app/statestring
BUT with the route get('/action/{name}', ...) i can get only this
mysite.app/statestring/somename
If i changing route to Route::get('/action', ...) it doesnt work, the error is "Missing argument 1"
My web.php
Route::get('/action/{name}', [
'uses' => 'DoActionController#getAction',
'as' => 'returnAction',
]);
My Controller action
public function ($name)
{
return view('returnaction', ['name'=>$name]);
}
My home page
<body>
#foreach ($yourActions as $yourAction)
<li>
{{ $yourAction->name }}
</li>
#endforeach

You'll need to make the $name argument optional.
First, declare it as optional in the controller:
public function ($name = null)
That will get rid of the error your getting about missing argument. Next, make the name parameter optional in your route with:
Route::get('/action/{name?}', ...);

Related

Laravel 5.6 Function () does not exist in route/web.php

This is my code using to send an email
Route::post('/mail/send', [
'EmailController#send',
]);
in EmailController this is the send action
public function send(Request $request)
{
$data = $request->all();
$data['email'] = Input::get('email');
$data['name'] = Input::get('name');
$obj = new \stdClass();
$obj->attr = 'Hello';
Mail::to("dev#mail.com")->send(new WelcomeEmail($obj));
}
getting a error as Function () does not exist
In your route/web.php file
Change it to
Route::post('/mail/send', 'EmailController#send');
Refer to the documentation to see the possible options to define routes:
https://laravel.com/docs/5.6/routing
Route's action method can be defined using a array, but not simply wrap controller#action in an array, you should assign it to array's key 'uses'.
In your example, it should be like this:
Route::post('/mail/send', [
'uses' => 'EmailController#send',
//'middleware' => .... assign a middleware to this route, if needed
]);
the array form usually is used when we want to specify more specification about the route like use a specific middleware and pass middleware parameters.
if you just want to define route's processing method you can simply use controller#action as Route::post's second parameter:
Route::post('/mail/send','EmailController#send');
In your route ...
Route::post('/mail/send','EmailController#send')->name('send_email');
Inside of your HTML form add below code...
<form action="{{route('send_email')}}" method="post">
...
{{csrf_field()}}

Passing an array with redirect. Problems with »reload«.

Framework is Laravel. I am passing an array with the redirect method from a controller like this:
$serializeThrowsArray = serialize($throwsArray);
return redirect()->route('pages.result')
->with( ['serializeThrowsArray' => $serializeThrowsArray] );
to a named route:
Route::get('/result', ['as' => 'pages.result', function() {
$serializeThrowsArray = session()->get('serializeThrowsArray');
$throwsArray = unserialize($serializeThrowsArray);
return view('pages.result', ['throwsArray' =>$throwsArray]);
}]);
which loads the next page:
#section('content')
#foreach ($throwsArray as $throw)
{{$throw}},
#endforeach
#endsection
Everything work as it should, except when i hit F5(reload) and get the next error msg: "Invalid argument supplied for foreach()" and the next code is higlighted:
<?php $__currentLoopData = $throwsArray; $__env->addLoop($__currentLoopData);
foreach($__currentLoopData as $throw): $__env->incrementLoopIndices(); $loop
= $__env->getLastLoop(); ?>
I know is a problem with session-flash that has been cleared. Is there a work around or another way to pass an array with redirect?
Try
return->view('pages.result')

Laravel - Passing Parameters/ undefined variable

I'm struggling with passing parameters through in Laravel, I can access it via URL but I don't want that. I'm getting Undefined variable: user in the master.blade error.
Any help is appreciated
AuthController.php
function viewUserDetails($userId)
{
$user = User::find($userId);
return view('user/userdetails',['user' => $user]);
}
Web.php
Route::get('userdetails/{userId}', 'AuthController#viewUserDetails');
Master.Blade
<li>Your Details</li>
Other guys are telling you about userdetails view, but your code is totally fine. What you need to do is pass $user variable to the master view too:
return view('master', ['user' => $user]);
Try to pass like this
return view('user/userdetails')->with('user' => $user);
Controller::
function viewUserDetails($userId)
{
$user = User::find($userId);
return view('user.userdetails')-with('user',$user);
}
Then Used In blade File..
#foreach ($user as $data){
<h1> {{ $data['_id'] }} </h1>
<h1> {{ $data['name'] }} </h1>
#endforeach
I assume that you have extended master.blade in user/userdetails.blade. You need use array as second parameter to url function.
<li>Your Details</li>
It is best practice to use named route in laravel.
Change your route like this :
Route::get('userdetails/{userId}', 'AuthController#viewUserDetails')->named('user_details');
In your blade you can use route function to get the path.
<li>Your Details</li>
You can update your code like:
AuthController.php
function viewUserDetails($userId)
{
$user = User::find($userId);
return view('user.userdetails',compact('user'));
}
Web.php
Route::get('userdetails/{userId}', 'AuthController#viewUserDetails');
Master.Blade
<li>Your Details</li>

Laravel pass exact parameter to route

i have translated url which i need to redirect to the specific controller function, but i need also to pass an exact parameter.
For example i want to show all football news, but in the url i do not have the ID of the sport football (id=1) so i need to pass the parameter id=1 to the index() function.
Route::get('/football-news/', ['as' => 'news.index', 'uses' => 'NewsController#index']);
it is not an option to pass 'football' as a parameter, because it is just an example. The real route is translated and the code looks like that:
Route::get(LaravelLocalization::transRoute('routes.football.news'), ['as' => 'news.index', 'uses' => 'NewsController#index']);
suppose you have a NewsController to fetch all news be like
class NewsController extends Controller
{
public function index()
{
$news = News::all(); //you have to create News model
return view('news.index', compact('news')); //use to pass data in view
}
public function show($id)
{
$news_detail=News::find($id); //to fetch detail of news from database
return view('news.show', compact('news_detail'));
}
}
create index.php and show.php in views/news folder. in index.php
#foreach($news as $news_item)
<div>
{{ $news_item->title }}
</div>
#endforeach
here using "/news/{{$news_item->id}}" you can pass id of specific news into route file.
in show.php
<h1>news</h1>
<h1>
{{ $news_detail->title }}
</h1>
<ul class="list-group">
#foreach($news_detail->detail as $details)
<li class="list-group-item">{{$details}}</li>
#endforeach
</ul>
in route file
Route::get('/news/{news}', 'NewsController#show');
now you have to create show($id) function in NewsController.php which parameter is id.
You can append the index URL with ?id=1 parameter (eg. domain.com?id=1) and get it in your index controller action by using Request::get('id');
For example:
Url in template file:
<a href="domain.com?id=1" />
In your NewsController:
public function index(Request $request){
$id = $request->get('id');
}
You should be able to have access to the parameter even though you didn't specify wildcards in the route file.
Edit:
you will have to call a different #action for a different route. You can pass in an id wildcard.
For example, in Route file:
Route::get('tennis-news/{id}', 'NewsController#tennisIndex');
Route::get('football-news/{id}', 'NewsController#footballIndex');
Then in the NewsControlleryou must have public methods tennisIndex($id) and footballIindex($id), these methods will have access to the wildcard you set in the route.
For example, in NewsController
public function tennisIndex($id){
$tennnis_news = News::where('sport'='tennis)->where('id', $id)->get();
return view('tennis_news', compact('tennnis_news'));
}

Laravel5.2 delete doesn't work

I developed website with CRUD on products table .this is the structure of the table.
Create and update works fine But delete not work.
This is the form in blade to delete product
{{ Form::open(array('url' => 'admin/products/' . $product->id, 'class' => 'pull-right')) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete ', array('class' => 'btn btn-warning')) }}
{{ Form::close() }}
And this the destroy function in controller
public function destroy($id)
{
$product = Product::find($id);
$product->delete();
// Product::destroy($id);
return redirect('admin/products')->with('message', 'Successfully deleted the product!');
}
And This is my routes
Route::group(['middleware' =>'App\Http\Middleware\AdminMiddleware'], function () {
//resource
Route::resource('admin/products','AdminFront');
});
When I click delete button it enter the destroy function and dd($id) correct
But when write
$product = Product::find($id);
$product->delete();
Or
Product::destroy($id);
I get this error
The localhost page isn’t working
localhost is currently unable to handle this request.
This error tired me . I developed delete fun with resource API in another table and work fine.I don't know are the problem in the db or where. please any one help me ,
What does your routes.php look like?
You may need to include the resource route in routes.php.
Route::resource('admin/products/', 'TheNameOfYourController');
But make sure the route is protected either in the controller or routes.php.
Here is somewhat the same setup you have:
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/routes.php LINE 119
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/Controllers/UsersManagementController.php LINES 369-376
https://github.com/jeremykenedy/laravel-material-design/blob/master/resources/views/admin/edit-user.blade.php LINES 243-246
Cheers!

Resources