need to insert data to database i am geting this called to undeifne method and i am totally new dont now how to define method
this is route line
Route::post('/colFunction','Controller#colFunction');
this is my controller function
function colFunction(Request $req)
{
$column =$req->input('column');
$data =array('column'=>$column);
\DB::table('column')->colFunction($data);
}
Try this code
use insert() instead of colFunction() in your code
function colFunction(Request $req)
{
$column =$req->input('column');
$data =array('column'=>$column);
\DB::table('column')->insert($data);
}
Related
I have a testController in which I have two function importCustomer() and addUpdateCustomer(Request $request).
I want to pass on the importCustomer values to the addUpdateCustomer function, but the problem is I can't pass the array, because of the request object.
function importCustomer(Request $request){
//Will read the csv file and for each record call the
//addUpdateCustomer function
$dataPayload = [];
$this->addUpdateCustomer($dataPayload);
}
function addUpdateCustomer(Request $request){
//Insert if user is new else update the details if already exists.
}
Is the addUpdateCustomer function publically accessible, meaning is there a route defined for it? If not just remove the Request object as a parameter and replace it with an array.
public function addUpdateCustomer(array $payload = [])
{
// do stuff
}
Alternatively you merge some content into the $request:
$this->addUpdateCustomer($request->merge($dataPayload));
Sorry this is very basic, but i dont know what is the problem.
I always get this error when trying to visit edit_supervisor.
Route:
Route::get('/edit_supervisor', 'SupervisorController#edit')->name('edit_supervisor');
SupervisorController:
public function edit($id)
{
return view('DataSupervisor.edit');
}
you can add $id as a parameter like this in Route
Route::get('/edit_supervisor/{id}', 'SupervisorController#edit')->name('edit_supervisor');
or you can change the function parameter $id to Request $request
public function edit(Request $request)
{
return view('DataSupervisor.edit');
}
in blade
edit
for showing previous data you need to get data using $id in controller
$supervisor = Model::findOrFail($id);
return view('DataSupervisor.edit', compact('supervisor'));
You need to pass the id :
Route::get('/edit_supervisor/{id}', 'SupervisorController#edit')->name('edit_supervisor');
On blade :
<a href="{{ route('edit_supervisor', $id) }}...
Assalamualaikum guys, i have some problems with my code. i tried to make search by using request parameters on laravel. and here is my code views.
$maintenance->with([
'user_post' => function($query, Request $request){
if($request->has('searchBy') && $request->has('searchQuery'))
{
$parse_query = explode('.',$request->query('searchBy'));
$is_user = $parse_query[0] == 'user_post'? true : false;
if($is_user)
{
$query->where($parse_query[1],'LIKE','%'.$request->query('searchQuery').'%');
}
}
},
'dt_project'
]);
when i tried to execute on browser, it return
1/1) ErrorException
Undefined variable: request
what should i do? thanks to the moon and back. hehehe
try this, change your line 2 to:
'user_post' => function($query) use($request){
explanation:
Request $request can only be used in controller functions like:
public function store(Request $request)
{
//code here
}
in order to pass the $request parameter inside another function, use use (this is also known as variable inheriting):
public function store(Request $request)
{
$user= User::with(['roles' => function($query) use($request){
^^^
$query->where('roles.id', $request['role_id'])->first();
}])->get();
}
in Controller
public function index(Request $request)
{
$search_cons = $request->all();
$search_con = $search_cons->name; //error place
return $search_cons.$search_con;
}
->name this place has the error
Trying to get property of non-object
Or in blade.view
<p>{{$search_cons->name}}</p> has the error
Trying to get property of non-object
But if I use
$search_cons=$request->input('name');
on controller
the blade view
<p>{{$search_cons}}</p> will work ok!
I want the $search=request->all() so I can freely use $search->name on my blade view
How can I fix the question?
PS: I tried $resquest('name') still not to work
Request::all() ->tell me the
When you do $request->all() it returns all the inputs submitted in array format. So in your case, you can do
$search_cons = $request->all(); // dd($search_cons) so you can see its structure
$search_con = $search_cons['name']; // instead of ->name since it's not an object anymore
And anyway, you can skip the $request->all() thing - you can actually just do this directly:
$request->name.
EDIT:
You can cast the array as an object using (object)
$search_cons = (object) $request->all();
this will still let you use $search_cons->name
$search_cons is an array, not an object:
$search_con = $search_cons['name']
public function index(Request $request)
{
$search_cons = $request->all(); //returns array
$search_con = $search_cons['name']; //error place
return $search_cons.$search_con;
}
Or you can do like this
request()->name //request() isa global helper function
Try
In Controller
public function index(Request $request)
{
$search = $request->all();
return ['search' => $search];
}
In Blade
<p>Name : {{$search['name'] ? $search['name'] : ''}} </p>
I am new to laravel and I'm trying to pass a value from one controller to another controller.. In this case.. I want to pass the order_id generated in Order Controller to /pizza/create in Pizza Controller..
this is my code in OrderController
public function store()
{
$user = User::find(Auth::user()->id);
$order= new Order;
$order->user()->associate($user);
$order->status = 'unconfirmed';
$order->save();
return Redirect::to('/pizza/create')
->with('order', $order->order_id);
}
this is my code in /pizza/create in PizzaController
public function create()
{
$id = Session::get('order');
$order = Order::find($id);
return View::make('pizza.create')
->with('order', $order);
}
this somehow works.. but the value (order_id) disappears when i change views/routes..
if you want to pass a value from controller to controller use session, when you are using redirect and with method it is creating session, when you are using view and with method it is creating variable which you can use in view to display that value.
this create a session
return Redirect::to('/pizza/create')
->with('order', $order->order_id);
this statement create a variable which you can use in view
return View::make('pizza.create')
->with('order', $order);
above variable you can use in blade or view file
{{ $order }}
so in create method you need to create a session keep variable alive and available in other controllers
public function create()
{
$id = Session::get('order');
$order = Order::find($id);
Session::put('order',$id);
return View::make('pizza.create')
->with('order', $order);
}