I am trying to attach multiple models with one controller using implicit model binding but I am getting the following error if I try to attach more than one model with methods.
index() must be an instance of App\\Http\\Models\\Modelname, string given
Here is my code:
public function index(Model1 $model1,Model2 $model2,Model3 $model3)
{
print_r($application_endpoint);
}
Route:
Route::resource("model1.model2.model3","MyController",["except"=>["create","edit"]]);
Your route should looks like that:
Route::resource("your_route/{model1}/{model2}/{model3}","MyController",[
"except"=>["create","edit"]
]);
Yess you can register routes like these
Route::resource("model1.model2.model3","MyController",["except"=>["create","edit"]]);
but in your controller, you have to
public function index($id,$id2,$id3)
{
print_r($application_endpoint);
}
OR
you can do like this
Route::model('key/key/key', 'MyController')
and in your controller
public function index(Model1 $model1,Model2 $model2,Model3 $model3)
{
print_r($application_endpoint);
}
Related
I have this code in the HomeController#index:
$towns = Town::all();
return Redirect::to('home')
->with('towns', $towns);
Is there any way I can tell Laravel to execute that lines of code before the end of methods and controllers I define without me copying and pasting those lines of code in every method?
You don't need to do that, you can just share this data with all views by using the view()->share() method in a service provider:
view()->share('towns', Town::all());
You can also use a view composer for that:
public function compose(View $view)
{
$view->with('towns', Town::all());
}
You can extend all controllers from your basic controller. Use Controller.php on app/Http/Controllers/controller.php or create new one.
Add myThreeLines to base controller.
controller.php:
function myThreeLines(){
$towns = Town::all();
return Redirect::to('home')
->with('towns', $towns);
}
class TestController extend Controller{
function index(){
return $this->myThreeLines();
}
}
I want to know if it is possible to create multiple roots for single methode controller in laravel? something like this :
//route
Route::get('foo/bar', 'FooController#bar');
Route::get('foo/bar/{id}', 'FooController#bar');
Route::get('foo/bar/{id}/{date}', 'FooController#bar');
//controller
class FooController extends Controller {
public function bar($id,$date)
{
//do something
}
}
Yes but you have to handle null parameters, like that:
public function bar($id=null, $date=null)
{
....
But at this point its better to declare e single route with optional parameters:
Route::get('foo/bar/{id?}/{date?}', 'FooController#bar');
I have a route with dynamic model recognition. In other words, I take the desired model as an argument and use it in the controller. I have complex authorization in my app and I need to pass the model class name as a variable to the $user->can() method for using policies, but for some reason it doesn't work. Here's my code:
Policy:
public function view($user, Model $model) {
return $user->model_id == $model_id;
}
public function create($user) {
return $user->isAdmin();
}
Controller:
public function createModel($model) {
$model_class = $model . '::class';
if (Auth::user()->can('create', $model_class)) {
return $model_class::create();
}
return 'invalid_permissions';
}
If I hardcode the model class name it works. For example, if my model is 'Car' and in the controller I put:
if (Auth::user()->can('create', Car::class)) {
Anybody got any ideas why this is so and how to fix it? I hope that it's possible because I would have to change my whole concept if it isn't.
*Note: this is example code, not my actuall classes
I want to add my own controller method to Auth Facade in Laravel
auth()->user()->ownControllerMethod()
How can we do that ?
In your example, you'd actually be adding a method to the User model. (User.php)
public function my_method() {
// code here
}
EDIT:
Per your comment, lets say your route is mapped to the index() method in your UsersController, you could do this:
public function index()
{
// some code
$this->doSomething();
}
protected function doSomething()
{
// some code
// return data
}
But it really depends what this method is meant to do. There's lots of different ways to break up your code. And depending on the size of your project, your User model maybe a fine place.
I'm new to laravel 4 and I'm currently having the following problem:
In my routes.php I have the line:
Route::get('maps/{map_type}', 'MapsController#loadMaps');
And in my MapsController I'd like to get the {map_type} to use it.
So my question: How can I retrieve map_type in my Controller?
Your first argument in your loadMaps method is your map type.
public function loadMaps($map_type) {
return $map_type;
}
Take a look at the first two code snippets on http://laravel.com/docs/controllers.
Controllers receive parameters as parameters in the called function:
class MapsController extends BaseController {
public function loadMaps($map_type) {}
}
Check http://laravel.com/docs/controllers#basic-controllers for more info