Display DataTable with other data - datatable

I am going to display DataTable. My controller code for DataTable is like below,
$data = Student::latest()->get();
return Datatables::of($data)
->addIndexColumn()
->addColumn('action', function($row){
$btn = 'Edit Delete';
return $btn;
})
->rawColumns(['action'])
->make(true);
But I have to show results of below data also in View file.
$product = Product::where('product_id',$id)->with('product_log')->first();

First of all, don't use get() as it will return collection, this will slowdown the loading speed of the table. Better if you pass query instead.
You might want to make another method for datatableble of product, and in blade call that method inside of datatable initialization for product table if you want to display it in datatable. Else you can pass the data in the method from where you are returning view.
Hope it helped.

Related

Unserialize cart data not working Laravel

I'm save my cart products as unserialized data from my cart session.
$order = new Order();
$order->cart = serialize($cart);
$order->code = strtoupper(str_random(15));
$order->user_id = Auth::user()->id;
$order->save();
now i need to unserialize the data to use it in my blade file, this is the function i'm using
$orders = Order::with('user')->findOrFail($order->id);
$orders->transform(function($order, $key){
$order->cart = unserialize($order->cart);
return $order;
});
dd($orders);
I'm getting this error
BadMethodCallException Call to undefined method App\Order::transform()
what seems to be the problem? and how can i unserialize my data;
any ideas ???
With findOrFail you retrieve just one instance of model, not a collection.
$orders = Order::with('user')->findOrFail($order->id);
Also what is $order->id you have your order model?
So
$order->load('user');
$order->cart = unserialize($order->cart);
Or do you want a collection then your code will work like this
$orders = Order::with('user')->get();
$orders->transform(function($order, $key) {
$order->cart = unserialize($order->cart);
return $order;
});
The problem is not about unserialize, it doesn't even get there. The problem seems to be related to an undefined method you are trying to use ($orders->transform).
Can you please provide the code in your Order class? Did you define the transform method there?
EDIT:
by using serialize you are kinda' missing the point of a relational database and the datatypes inherent in your database engine. Doing this makes data in your database non-portable, difficult to read, and can complicate queries.
Also, many tests prove that json_encode is faster than serialize.

Only display selected fields to the view using Laravel Query Builder

I have quotes I am displaying to the screen one at a time upon each page refresh. The data is displaying fine from the DB; the issue is that I am getting the (id) display on the screen as well when I only need the quote and author name. How do I "exclude" the id field from displaying in my view?
Controller
public function index()
{
$quotes = DB::table('quotes')->inRandomOrder()->first();
return view('home', compact('quotes'));
}
Blade/View
#foreach($quotes as $quote)
<p>{{$quote}}</p>
#endforeach
Edit based on the comment of #devk:
Generally answers OPs question, but the code here (and in OPs post)
doesn't exactly make sense. The ->first() will return null or a single
quote, but the #foreach(..) is used like it's a collection of quotes
you can do the query like this:
$quote = DB::table('quotes')->select('quote', 'author')->inRandomOrder()->first();
replacing 'quote' and 'author' by the fields names in your table.
And return te quote:
return view('home', compact('quote'));
And in blade show the object:
<p>{{$quote}}</p>
or show the fields:
#isset($quote)
// $quote is defined and is not null...
<p>{{$quote->quote}}</p>
<small>{{$quote->author}}</small>
#endisset
If you want to show multiple quotes, do the query like this:
$quotes = DB::table('quotes')->select('quote', 'author')->inRandomOrder()->get();
or
$quotes = DB::table('quotes')->select('quote', 'author')->inRandomOrder()->take(5)->get();
And in blade you can loop through the collection:
#foreach($quotes as $quote)
<p>{{$quote}}</p>
#endforeach
or
#foreach($quotes as $quote)
<p>{{$quote->quote}}</p>
<small>{{$quote->author}}</small>
#endforeach

How to show specific items of an array in Laravel

I am using Laravel.i want to show only the Name in my View Part. But facing problem.
function index() {
$data=array(
['id'=>'1','Name'=>'Debasais','sirname'=>'Acharya'],
['id'=>'2','Name'=>'prashant','sirname'=>'Mohaptra'],
['id'=>'3','Name'=>'Silu','sirname'=>'Mohaptra']);
return view("welcome")->withdata($data);
}
To show just the name in your view, just loop through $data and select only name.
#foreach($data as $names)
{{$names['Name']}}
#endforeach
I am pretty sure you are already looping through $data just make sure to output the name only

Adding a grid as a below the edit form in laravel backpack

I have User that has many Posts.
Bellow the User edit form I want to show the related Posts grid.
The following function will bring the related posts.
How can I combine it above/inside/bellow the edit User form?
I know this function will work, just don't know how to combine it.
// user crud controller
public function getUserRlatedPosts($user_id)
{
$crud = new CrudPanel();
$crud->addClause('where', 'user_id', '=', $user_id);
$crud->setModel("post");
$crud->setEntityNameStrings("post","posts");
$crud->enableAjaxTable();
$this->data['crud'] = $crud;
$this->data['title'] = ucfirst($this->crud->entity_name_plural);
if (! $this->data['crud']->ajaxTable()) {
$this->data['entries'] = $this->data['crud']->getEntries();
}
return view('crud::list', $this->data);
}
In Backpack\CRUD 3.2.x you can use a custom view with the following methods:
$this->crud->setShowView('your-view');
$this->crud->setEditView('your-view');
$this->crud->setCreateView('your-view');
$this->crud->setListView('your-view');
$this->crud->setReorderView('your-view');
$this->crud->setRevisionsView('your-view');
$this->crud->setRevisionsTimelineView('your-view');
$this->crud->setDetailsRowView('your-view');
and specify the view in which you also include that form.

codeigniter pass variable from controller to model

simple issue I presume.
My controller is getting the if to display from the url using $this->uri->segment(3). This will always be a single value. I am putting this in an array to pass to the model with:
$customerid = array(
'id' => $this->uri->segment(3)
);
The controller syntax is below:
function confirm_delete_customer()
{
$data['title']="Confirm Customer Deletion";
$customerid=array(
'id'=>$this->uri->segment(3)
);
//query model to get data results for form
$data=array();
if($query=$this->model_master_data->get_customer_records_to_delete()){
$data['records']=$query;
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_confirm_customer_deletion",$data);
$this->load->view("master_data/view_master_data_footer");
}
I am then trying to access this array value and pass it to my model to process. If I hard code the array into the model it works as per below syntax:
Model - Manual Syntax is:
function get_customer_records_to_delete()
{
$query = $this->db->get_where('customers', array('id'=>43));
return $query->result();
}
if I try replace this with the array from my controller it fails with error:
Undefined variable: customerid
idea of model that I want to get working:
function get_customer_records_to_delete()
{
$query = $this->db->get_where('customers', $customerid);
return $query->result();
}
I have a feeling it is something small. however is this the best way to get a single record from the database in order to output to a view?
Thanks in advance for the assistance.
The best way to do that is:
function confirm_delete_customer()
{
$data=array();
$data['title']="Confirm Customer Deletion";
$customerId = $this->uri->segment(3);
//Prevent SQL injections
if(!is_numeric($customerId) || empty($customerId)) {
show_error("Bad Request");
}
$query = $this->model_master_data->get_customer_records_to_delete($customerId);
if ($query){
$data['records']=$query;
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_confirm_customer_deletion",$data);
$this->load->view("master_data/view_master_data_footer");
}
}
and then you can simply call:
function get_customer_records_to_delete($customerId)
{
$query = $this->db->get_where('customers', array('id'=>$customerId));
return $query->result();
}
at your model.
You need to pass the value as an argument to the function so it can access it.
Ex:
get_customer_records_to_delete($customerid)
{
// now $customerid is accessible
$query = ....;
return $……;
}
You should heavily rely on function parameters. Grab the customer id from the controller and send it to the model. Moreover, you can use row() to get a single result from the database.
Controller:
function confirm_delete_customer(){
$data['title']="Confirm Customer Deletion";
$customerid=$this->uri->segment(3);
//query model to get data results for form
$data=array();
if($query=$this->model_master_data->get_customer_records_to_delete( $customerid)) //you are sending customer id as a parameter here
$data['records']=$query;
$this->load->view("master_data/view_master_data_header",$data);
$this->load->view("master_data/view_master_data_nav");
$this->load->view("master_data/view_content_master_data_confirm_customer_deletion",$data);
$this->load->view("master_data/view_master_data_footer");
}}
Model
function get_customer_records_to_delete($customerid)
{
$query = $this->db->get_where('customers', array("id"=>$customerid)); //you are using the customer id sent from the controller here
return $query->row(); //this will return a single row
}
Old thread but the answer is to declare the variable as "public" in the controller (i.e. public $customerid;), in which case it'll be available to your model. In some cases it's probably safer to explicitly pass as an argument. However, when you have several variables, it's useful to have the option to declare them instead.

Resources