I just starting learning laravel and was wondering how to pass data unrelated to the route to a controller. What I'm trying to accomplish is create a todo item that is able to have nested items.
View
<a class="btn btn-success" href="{{route('lists.items.create',4)}}">Create New Item</a>
The 4 is just a hard-coded example to see if it was working.
Controller
public function create(TodoList $list, $item_id = null)
{
dd($item_id);
return view('items.create', compact('list'));
}
So if your creating an item and don't pass in a parameter for id, it will default to null otherwise set it to whatever was passed in. However I'm getting a NotFoundHttpException. How would I be able to accomplish this.
Any help Welcome :)
You need to define the route, for example:
Route::get('create-item/{id}', [
'as' => 'lists.items.create',
'uses' => 'MyController#create'
])
Now, call the route like:
<a class="btn btn-success" href="{{route('lists.items.create', ['id' => 4])}}">Create New Item</a>
Related
I have been fixing this trivial problem a few days, but it didn't solve either.
When I click the detail button on the table, it will display a 404 or not found message.
Controller method
public function detail($id)
{
$data = DB::table('lirik_lagu')->where('id', $id)->first();
return view ('admin.detail-lirik');
}
Route
Route::get('lirik-lagu/detail/{$id}', [LirikLaguController::class, 'detail']);
Blade
<a class="btn btn-success btn-sm" href="{{ url('admin/lirik-lagu/detail', $data->id) }}">Detail</a>
I will try to fix what i think it's wrong in your code.
First you should give the $data to your blade file
Controller method
public function detail($id)
{
$data = DB::table('lirik_lagu')->where('id', $id)->first();
return view ('admin.detail-lirik',['data'=>$data]);
}
Second, your should omit the $ in your Route id parameters
Route
Route::get('lirik-lagu/detail/{id}', [LirikLaguController::class, 'detail']);
Third, when you generate the url url(...) it seems to have an admin prefix but not your route declaration.
Blade
<a class="btn btn-success btn-sm" href="{{ url('lirik-lagu/detail', $data->id) }}">Detail</a>
You are not passing the variable to the view. This is how you do it:
return view('admin.detail-lirik', $data);
If you need to pass multiple variables you can pass an array in the second paramter like this:
return view('admin.detail-lirik, ["varname" => $var]);
I see several problems here, so I'm going to point them out. First one, you never returned the data to your view:
return view ('admin.detail-lirik', $data);
The other is the URL in your blade file. You called admin/lirik-lagu/detail but you defined route without admin in your web.php file. You can remove the admin from your url, or create a name for your route and call it that way:
Route::get('lirik-lagu/detail/{$id}', [LirikLaguController::class, 'detail'])->name('lirik-langu');
And then use it like this
<a class="btn btn-success btn-sm" href="{{ route('lirik-langu', $data->id) }}">Detail</a>
Can anyone please help me to pass multiple parameters in GET method, I have following codes -
in blade -
#if(!isset($model->id_car_type))
<a class="btn btn-search-red" href="{{ route('frontend.model', [Request::segment(2),$model->year,$model->niceName]) }}">Select</a>
#else
<a class="btn btn-search-red" href="{{ route('frontend.model', array(Request::segment(2),$model->year,$model->niceName, $model->id_car_type)) }}">Select</a>
#endif
In route -
Route::get('/model/{make}/{year}/{niceName}/{type?}', 'GeneralController#trimShowByNiceName')->name('frontend.model');
But it is throwing error -
Missing required parameters for [Route: frontend.model] [URI:
model/make/{make}/year/{year}/niceName/{niceName}/{type?}]
To pass parameters in a route use an array with the paramter names as keys:
{{ route('frontend.model', ['make' => 'Ford', 'year' => 1988, 'niceName' => 'Ford Escort']) }}
https://laravel.com/docs/5.7/routing#named-routes
In Laravel 5.2 use the following example:
In the view.blade.php
Page
In the route.php
`Route::get('users/{id}{name}', 'UsersController#searchUsers'); // this gets the id, name` from the view url.
In the controller method, pass the parameters as function parameters as follows:
public function searchUsers($id, $name)
{
// your code here that use the parameters
}
route
Route::get('/dashboard/view-sub-project/{pid}/{sid}', 'SubProjectController#view')->name('sub-project.view')->middleware('auth');
View
View
Values of var
request()->route()->parameters['id'] is 2
$update->id is 1
I have defined router correctly on web.php and view but still, it throws an error
Missing required parameters for [Route: sub-project.view] [URI:
dashboard/view-sub-project/{pid}/{sid}]. (View:
/var/www/html/groot-server/resources/views/project/view.blade.php)
I have tried to change my router like this also
Route::get('/dashboard/view-sub-project/{pid}{sid}', 'SubProjectController#view')->name('sub-project.view')->middleware('auth');
Still got the same error.
Try adding parameters in array.
Route::get('/dashboard/view-sub-project/{pid}/{sid}','SubProjectController#view')
->name('sub-project.view')
->middleware('auth');
<a href="{{ route('sub-project.view',
[
'pid' => request()->route()->parameters['id'],
'sid' => $update->id
]
) }}" class="btn btn-primary project-view">
View
</a>
Hope this helps.
On your view, since you're using the route function to build the url you can do the following.
<a href="{{ route('sub-project.view', [
'pid' => request()->route()->parameters['id'],
'sid' => '$update->id'
]) }}" class="btn btn-primary project-view">View</a>
You can also view it in the Laravel Helper Function.
If you only have one parameter in the route you can just pass the value. Let's say you had a route that only took a post ID, Route::get('/posts/{post}/edit')->name(edit). On your view you can then do {{ route('edit', $post->id) }}.
When you have multiple values being passed to the route url as you have in your case you pass an array of item with the key being the same as the route parameter.
Let's say you have another route Route::get('/posts/{post}/comments/{comment}')->name(post.comment). On your view you can do {{ route('post.comment', ['post' => $post->id, 'commment' => $comment->id]) }}.
Hello I have routing method:
// Show Sport
Route::get('/{id}/{name_to_url}', [
'as' => 'front.sport',
'uses' => 'FrontController#sport'
]);
and method in Controller to this routing method:
public function sport($id, $name_to_url){
// Team
if (is($id.'/'.$name_to_url)) {
$teamSport = Team::select()->where('sport_id', '=', $id)->orderBy('team_name', 'asc')->get();
}
return view('master', compact('teamSport'));
}
and query in view file:
<?php
$getLeague = League::select()->orderBy('name', 'asc')->get();
?>
#foreach($getLeague as $league)
<p class="league-star"><span class="glyphicon glyphicon-star-empty" aria-hidden="true"></span> Liga {{ $league->name }} <img src="{{ asset('flags/'.$league->flags) }}" width="auto" height="18" class="league-icon"></p>
#endforeach
I don't really know how I can write a method which will check what exists in the GET. More precisely I have front page (home) url to this home page is "www.mypage.com/", now I want create new url (www.mypage.com/football, www.mypage.com/boks ...). And when user klick on button with link for example www.mypage.com/boks on front page they will be returned ONLY records with boks_id, not football ONLY WITH BOKS.
But when the user is on home page (www.mypage.com/) on front page is returned all records from database
You don't really need to check $_GET. You just need to do this:
In your route, you will need to put a ? behind each parameters to indicate that these parameters are optional. If you don't do so, you will need to explicitly provide a route to / for the home page. In this case, we will just use optional parameters:
Route::get('/{id?}/{name_to_url?}', [
'as' => 'front.sport',
'uses' => 'FrontController#sport'
]);
Then in your controller, simply make the parameters optional, and check if they are set or not:
public function sport($id = null, $name_to_url = null){
//We first creating the builder, default to sort by team_name
$teamSportBuilder = Team::orderBy('team_name', 'asc');
//If team id is set, we add the extra condition in
if (isset($id)) {
$teamSport = $teamSportBuilder->where('sport_id', '=', $id);
}
//Query it and return data
$teamSport = $teamSportBuilder->get();
return view('master', compact('teamSport'));
}
Not an answer because #Lionel Chan were good but you should never execute SQL requests in your view files.
You shouldn't use the PHP open tag in your view files.
This is not the purpose of a Framework like Symfony/Laravel
I'm having a hard time setting up simple links/actions.
In my index view, I have this little form that I want to launch the getTest action in the ProjectsController when I click on the button:
{{ Form::open(array('action' => array('ProjectsController#getTest', $project->id))) }}
<button type="submit"><i class="icon-arrow-up"></i></button>
{{ Form::close() }}
This is the getTest function :
public function getTest(){
echo "test";
return 'test';
}
But this keeps getting me a "Array_combine(): Both parameters should have an equal number of elements" error.
I tried making this work with a route. with this form open instead :
{{ Form::open(['method' => 'GET', 'route' => ['test_route', $project->id]]) }}
And this route :
Route::get('projects/test', array('as' => 'test_route', 'uses' =>'ProjectsController#getTest'));
But I still have the same error.
I can't find any good doc on routing/sending to actions that don't give me this problem. I don't see what
Your route doesn't need parameter, so I think this code is sufficient:
{{ Form::open(['method' => 'GET', 'route' => 'test_route']) }}
I believe the problem is you are adding parameters to the action, but you are not managing those parameters in your routes, nor is your getTest() function accepting any parameters. Another problem is you are setting your route as a GET route, but your form is going to be using POST.
It would be much easier instead on your form to use Form::hidden('id', $project->id); And then in your getTest() function, you could get the variable using $id = Input::get('id');. You'd also be able to use your route name in your form as well. Form::open(array('route'=> 'test_route', method=> 'get'));