Passing data from blade to controller Laravel - php

I want to pass a object from the blade file to the controller file. The purpose is when the user click an edit button the user will get a form which is filled with the previous input data. I am using this code in the blade file:
Edit
But When I want to get the passed object from the controller's edit method I get a null. My Controller code is like this now:
public function edit(FeesType $feesType)
{
//
dump($feesType->name);
return view('feestype.edit',['feesType'=>$feesType]);
}
Here I have dump the $feesType object but I get a null. Please help me how can I solve this problem.
Thanks in advance

Route model binding works a bit different here is the documentation
What you need to do is have your route like this:
Route::get('feestype/{feesType}/edit', 'YourController#edit')->name('feestype.edit');
then in your view
Edit
-- EDIT
using a resource file:
Route::resource('feestype', 'YourController')
the link will be built the same as above:
{{ route('feestype.edit', $feesType) }}

you should change your Route to :
Route::put('feestype/{id}/edit', 'YourController#edit');
For update and edit you should use put not get.
Note that for this code:
Edit
first you should compact $feestype in YourController then use your code in blade.

Now the code in the blade file is
Edit
The controller file contains this code:
public function edit(FeesType $feesType)
{
//
$feesType = FeesType::find($feesType->id);
dump($feesType->name);
return view('feestype.edit',['feesType'=>$feesType]);
}
And here is my Route definition:
Route::resource('feestype','FeesTypesController');
And the browser shows this message:

Related

Problem showing the view when I add a parameter in my Route - Laravel

I want to pass a parameter to my controller from an URL, my code is:
In web.php:
Route::get('infoPatient/{id}','patientController#infoPatient');
In my controller patienController.php:
public function infoPatient($id){
$d=patientModel::find($id);
return view('/patient')->with('d',$d);
}
As a result, the view show data without any style.
the view: https://ibb.co/GCr66zk
Try This.
public function infoPatient($id){
$d=patientModel::find($id);
return view('patient',compact('d'));
}
and in your patient.blade file use variable $d to get the data
the probleme was solved, the issue was that I forget "/" in all link of bootstrap files.
Thank's.

Laravel. conflict with routes

I have a problemwith my routes. When I call 'editPolicy' I dont know what execute but is not method editPolicy. I think I have got problem beteweeb this two routes:
My web.php ##
Route::get('admin/edit/{user_id}', 'PolicyController#listPolicy')->name('listPolicy');
Route::put('/admin/edit/{policy_id}','PolicyController#editPolicy')->name('editPolicy');
I call listPolicy route in all.blade.php view like this:
{{ $user->name }}
And call editPolicy route in edit.blade.php view like this:
Remove</td>
My PolicyController.php is:
public function listPolicy($user_id)
{
$policies = Policy::where('user_id', $user_id)->get();
return view('admin/edit',compact('policies'));
}
public function editPolicy($policy_id)
{
dd($policy_id);
}
But I dont know what happend when I call editPolicy route but editPolicy method not executing.
Any help please?
Best regards
Clicking an anchor will always trigger a GET request.
route('listPolicy', $user->id) and route('editPolicy', $policy->id) will both return admin/edit/{an_id} so when you click your anchor, listPolicy will be executed. If you want to call editPolicy, you have to send a PUT request via a form, as defined when you declared your route with Route::put.
Quick note, your two routes have the same URL but seem to do very different things, you should differentiate them to avoid disarray. It's ok to have multiple routes with the same url if they have an impact on the same resource and different methods. For example for showing, deleting or updating the same resource.
Have a look at the documentation.

Codeigniter: call a controller from another codeigniter file

I have this two file path:
1: /api/math/application/controllers/test.php
2: /learn/mathtek/application/controllers/Auth.php
I need to call the controller from path2(Auth) inside the controller of path1(test). Is this possible? if yes how?
Ive tried to used the redirect() function but didnt work.
Also i tried this:
require_once('/learn/mathtek/application/controllers/Auth.php');
$aObj = new a(); //create object
$aObj->custom_a(); //call function
but it still didnt work...help please ... newbie in codeigniter here
Thanks guys for the help. I ended up doing a direct post of the path using redirect function to make it work.
1: /api/math/application/controllers/test.php - codeigniter1
2: /learn/mathtek/application/controllers/Auth.php - codeigniter2
Examples:
If i want to call path(2) from path(1).
redirect('http://localhost/learn/mathtek/auth/signin'); --'signin' is a function inside 'auth' controller
thats the code inside the 'test' controller. This works in codeigniter even if its from different path of controller.
However this doesnt work for UNITY(WEBGL). Thats why i ended doing an echo and returning it back.
You can't call any other controller action/method for have output or return value in your controller action/method because it is out of rules of MVC.
But you can redirect from one controller action to another controller action and pass argument in another controller action like below:
redirect("CONTROLLER/ACTION/ARGUMENT1/ARGUMENT2");
Edit:
suppose you are in Test controller and in test_method() action of Test controller then you can put your business logic code in the method and got some output and now you want to call any other controller function(eg: Auth) for perform any other operation with that output then you can pass that output in a redirect function as below:
redirect("Auth/auth_method/ARGUMENT1/ARGUMENT2");

"Some mandatory parameters are missing" Laravel 4

I've having trouble passing off an object to my 'edit' view in Laravel 4. The URL is generated correctly "localhost/edit/1" however this is the returned error:
Some manadatory parameters are missing ("offer") to generate a URL for route "get edit/{offer}
My related routes.php snippet:
Route::get('edit/{offer}','OfferController#edit');
The OfferController#edit action:
public function edit(Offer $offer)
{
return View::make('edit',compact('offer'));
}
Just some extra detail, here's the snippet from the 'index' view that initiates the action:
Edit
I should also mention when I remove the Blade form in '/views/edit.blade.php' the view is created, including the header which specifies the $offer->id:
<h1>Edit Offer {{ $offer->id }}</h1>
What am I missing here?
You need to pass an array to action():
Edit
Your Edit function needs to be changed. You are passing id in link, but expects Instance of Offer in edit function. Assuming Offer is an Eloquent model,
public function edit($id)
{
$offer = Offer::find($id);
return View::make('edit',compact('offer'));
}
Hope this helps.

Laravel : Using controller in included view

I have a view that is rendered with its controller. The function that calls the view is linked in my routes. It works fine when directly accessing the route, but obviously my controller is not included when I include it in my template.
How do I use my controller when I include my view?
I'm on Laravel 3.
Right now I have my controller :
public function get_current()
{
// $sales = ...
return View::make('sale.current')->with('sales',$sales);
}
My route (which obv only work on GET /current) :
Route::get('current', 'sale#current');
My master view
#include('sale.current')
Then my sale.current view calls $sales
#foreach($sales as $sale)
Thanks!
So this is the case when you want to call some laravel controller action from view to render another partial view. Although you can find one or another hack around it. However, please note that laravel controllers are not meant for that.
When you encounter this scenario when you want to reuse the same view again but don't want to supply all necessary data again & again in multiple controller actions, it's the time you should explore the Laravel View Composers.
Here is the official documentation link : https://laravel.com/docs/master/views#view-composers
Here is the more detailed version of it :
https://scotch.io/tutorials/sharing-data-between-views-using-laravel-view-composers
This is the standard way of achieving it without any patch work.
Your question is still unclear but I can try to help you. I did a small example with the requirements you gave. I create a route to an action controller as follows:
Route::get('test', 'TestController#test');
In TestController I define the action test as follows:
public function test()
{
return View::make('test.home')->with('data', array('hello', 'world', '!'));
}
According to your asking, you defined a view who includes content from another view (layout) and in that layout you use the data passed for the action controller. I create the views as follows:
// home.blade.php
<h1>Message</h1>
#include('test.test')
and
// test.blade.php
<?php print_r($data); ?>
When I access to "test" I can see print_r output. I don't know if that is what you are doing, but in my case works fine.
I hope that can help you.

Categories