How to update a record in lumen - php

I am simply trying to update a record but it says undefined variable in my view.
My html form view is like:
<form name="add_page" data-toggle="validator" role="form" id="add-pageform" action='{{url("update/page/$page->page_id")}}'>
My Controller:
public function updatePage(Request $request,$id)
{
$page = $request->all();
$plan = PageList::find($id);
$plan->update($page);
return view('edit-list');
}
and my route.php
$router->post('update/page/{id}','AjaxController#updatePage');
when i trying to access my view it says
Undefined variable: page (View: D:\xampp\htdocs\dev\app\resources\views\edit-list.blade.php)
its displaying error from in my form tag
any help would be appreciated:

You have not passed the $page variable to the view. Update your controller action like such:
public function updatePage(Request $request,$id)
{
$page = $request->all();
$plan = PageList::find($id);
$plan->update($page);
return view('edit-list')->with('page', $page);
}
For more information on passing data to views check this link

You should change your routes method to a patch if you want to update and then insert this into your form #method{'PATCH'}

Related

How to pass a collection or array from blade file to controller in laravel?

$cartItems contains all the rows of products from database and I am using this inside a blade file.
I want to pass this $cartItems back to a controller from this blade file
Note: $cartItems is from index() function in a controller like below.
$cartItems = DB::table('products')->whereIn('id', $cartItemsArray)->get();
return view('cart.index', compact('cartItems')
Below is my code.
index.blade.php
Proceed to checkout
web.php
Route::get('/cart/checkout/{cartItems}', 'CartController#checkout')->name('cart.checkout')->middleware('auth');
CartController.php
public function checkout($cartItems)
{
dd($cartItems);
return view('cart.checkout');
}
The error I am getting is,
Missing required parameters for [Route: cart.checkout] [URI: cart/checkout/{cartItems}]. (View: E:\github\LARAVEL\Deal-Ocean\resources\views\cart\index.blade.php)
You can use a form to send data back to server
Update your route from get to post
Route::post('/cart/checkout', 'CartController#checkout')->name('cart.checkout')->middleware('auth');
Use a form to post data to server. You can pass any additional data along with the request as well.
<form method="post" action="/cart/checkout">
#foreach($cartItems as $item)
<input name="cartItems[]" value="{{ $item->id }}"
#endforeach
<button class="site-btn">Proceed to checkout</button>
</form>
And in your controller use Request to access data
public function checkout(Request $request)
{
$cartItems = DB::table('products')->whereIn('id', $request->get($cartItems))->get();
dd($cartItems);
return view('cart.checkout');
}
If you want to proceed with the get request you should be able to do as follow
As $cartItems is a collection of products. So you can send the product ids and query the products using the ids from request.
<a href="{{ route('cart.checkout', ['cartItems' => $cartItems->pluck('id')->toArray()]) }}"
class="site-btn">Proceed to checkout</a>
Update controller
public function checkout(Request $request)
{
$cartItems = DB::table('products')->whereIn('id', $request->get($cartItems))->get();
dd($cartItems);
return view('cart.checkout');
}
Why use the same code logic of the index() method in the checkout method in the
CartController.
the checkout method will look like this:
$cartItems = DB::table('products')->whereIn('id', $cartItemsArray)->get();
return view('cart.checkout', compact('cartItems');

Getting Error Of Undefined Variable "data" php Codeigniter

I am updating table taking id in url which is auto increment.. i am also defiend a variable data in controller that is $data['data']=$this->articlesmodel->find_data($art_id); and this varible passing to view that is $this->load->view('admin/edit',$data);
and in view at form open i called the variable id**}",['class'=>'form-horizontal']);?>
but the error occure that the $data is undefiend.
this is view code
<?php echo form_open("admin/update_data/{$data->id}",['class'=>'form-horizontal']);?>
This is controller code
public function edit_data($art_id)
{
$this->load->helper('form');
$this->load->model('articlesmodel');
$data['data']=$this->articlesmodel->find_data($art_id);
$this->load->view('admin/edit',$data);
}
This is Model Code
public function update_data($art_id, Array $data )
{
return $this->db
->where('id',$art_id)
->update('data',$data);
}

How to use variables in routes in laravel?

I'm trying to build a application in laravel 5.3 in which I get the variable from request method and then trying to pass that variable in a redirect to the routes. I want to use this variable in my view so that I can be able to display the value of variable. I'm currently doing this:
In my controller I'm getting the request like this:
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member' => $member);
}
Now I've following in my routes:
Route::get('/member/memberinfo', 'MemberController#memberinfo')->with('member', $member);
Now in MemberController I want to use $member variable and display this into my view:
public function memberinfo()
{
return view('member.memberinfo', ['member' => $member]);
}
But I'm getting an error in the routes files
Call to undefined method Illuminate\Routing\Route::with()
Help me out, how can I achieve this.
When you're using redirect()->with(), you're saving data to the session. So to get data from the session in controller or even view you can use session() helper:
$member = session('member'); // In controller.
{{ session('member')['xyz'] }} // In view.
Alternatively, you could pass variables as string parameters.
Redirect:
return redirect('member/memberinfo/xyz/abc')
Route:
Route::get('/member/memberinfo/{xyz}/{abc}', 'MemberController#memberinfo');
Controller:
public function memberinfo($xyz, $abc)
{
return view('member.memberinfo', compact('xyz', 'abc'));
}
You can use like this:
route:
Route::get('/member/memberinfo', 'MemberController#memberinfo')
and the redirect:
return redirect('member/memberinfo')->with('member', $member);
You need to replace => with ,
public function register(Request $request)
{
$data = request->only('xyz','abc');
// Do some coding
.
.
$member['xyz'] = $data['xyz'];
$member['abc'] = $data['abc'];
return redirect('member/memberinfo')->with('member', $member); // => needs to be replaced with ,
}
Hope this works!
Replace line
return redirect('member/memberinfo')->with('member' => $member);
to
return redirect('member/memberinfo')->with('member', $member);
......

New route or another way to call a function from controller with ajax? Laravel 5.2 , Ajax

Is it possible to call a function in the controller without using a route or should I make a new route with two parameters as below that redirects to the specific page after the session has been added?
route::get('addsesion/{session-name}/{session slug};
If it's possible with ajax, can someone please point me in the right direction?
Basically what I would like to do is call the function addSession($session_name, $slug) from a controller with ajax on link <a href/> click , where it stores my specific session name and current page's slug.
It should call this addSession function on a click, store session data and then redirect to a different url. e.g. /seeparts, where it displays all saved session data.
Do I have to make a new route route::get('addsesion/{param1 - session-name}/{param2 - session slug}', currentController#addSession ); and then use that route as an ajax url? Or is there any other way how to use the controller's function?
My current Controller:
public function showAll() {
$parts = \DB::table() - > all();
$data = [
'parts' => $parts,
];
return view('partlist', $data);
}
public function showCpu($slug) {
// Specification query
$specs = \DB::table() - > select($select_columns) - > where('slug', $slug) - > first();
$data = [
'specs' => $specs,
'slug' => $slug
];
return view('part', $data);
}
//Add session - call this function
public function addSession($session_name, $slug) {\
Session::put($session_name, $slug);
}
}
part.blade.php:
<html>
#include('head.blade.php')
</body>
//on .add-to-partlist click adds session name that is specified in html and the current slug of the page
<a class="add-to-partlist" href="/seeparts" >Add to partlist</a>
</body>
</html>
I think you can use Service Injection binding controller function in your view.
Maybe you can reference it, https://laravel.com/docs/master/blade#service-injection.
For example:
<html>
#include('head.blade.php')
#inject('currentController', 'App\Http\Controllers\currentController')
</body>
//on .add-to-partlist click adds session name that is specified in html and the current slug of the page
<a class="add-to-partlist" href="/seeparts" onClick="{{ $currentController->addSession($session_name, $slug) }}">HERE</a>
</body>
</html>

Laravel 5 Passing data from Controller to View with AJAX

I'm having an issue passing data from controller to a view loaded via ajax. Below is my code:
JS:
function editass(id, name, title) {
var modal_title = document.getElementById("modal-title");
var modal_name = document.getElementById("modal-name");
var modal_body = document.getElementById("modal-body");
modal_title.innerHTML=title;
modal_name.innerHTML=name;
modal_body.innerHTML = '<div class = "text-info" align="center">Just a moment...</div>';
$("#modal-body").load('/SchoolSmart/public/assessments/edit/'+id);
//alert("modal");
}
Routes:
Route::get('/assessments/edit/{id}', 'AssessmentsController#edit');
Controller function
public function edit($id)
{
$editassessments = Assessment::findOrFail($id);
return view('assessments.edit')->with('$editassessments', $editassessments);
}
View
Assessment name: {!! $editassessments->name !!}
The error returned from the header is:
Undefined variable: editassessments
The view loads fine if I remove the call to variable, which is the whole essence of the page. Also, this function works fine if I the view was not loaded via AJAX.
Please, help on how I pass the variable from controller to view loaded via AJAX.
Thank you
Remove the '$':
return view('assessments.edit')->with('editassessments', $editassessments);

Categories