I am making a laravel app and I want to pass data to the controller from a view. I want to pass the id of a vehicle in order to get certain information for that car from another table.
desc.blade.php
{{link_to_action('Insur_DocController#index', "Insurances&Docs", $v->id, array('id'=>$v->id))}}
Insur_DocController#index
public function index()
{
$values = DB::select('select * from insur_docs where car_id=?', $id);
return View::make('pages.insur_docs', array(
'values' => $values,
));
}
Displays this error: Undefined variable: id
Assuming that you have a route:
Route::get('car/{id}', 'Insur_DocController#index');
Your controller method must expect the $id:
public function index($id)
{
...
}
Your select will not work this way, you could change it to:
$values = DB::table('insur_docs')->where('car_id', $id)->get();
And check if you got something:
dd($values);
This line of code will dump and die your script, so you can see the result of your query.
There is also an error in the link, which should be:
{{link_to_action('Insur_DocController#index', "Insurances&Docs", array('id'=>$v->id))}}
Related
I am approaching Laravel 7, and I am lost in a banality I guess. I create a function in the model that picks up the count of how many posts a category has, then passing the ID parameter.
I call this function in the controller, which then picks up the variable in the views.
It gives me an error:
Object of class Illuminate\Database\Eloquent\Builder could not be
converted to string
How can I solve it?
MODEL
public function scopeCountActivePostCategory($id)
{
$query = DB::table('post')->where([
['status', 1],
['category', $id],
]);
return $query->count();
}
CONTROLLER
// get details category by slug
// preleva dettagli categoria by slug
$detailCat = DB::table('categories')->where('slug', $slug)->first();
// get stories
$post = DB::table('post')
->orderByDesc('id')
->where('category', $detailCat->id)
->where('status', 1)
->paginate($set->app_result_x_page);
return view('category')->with([
'posts' => $posts,
'set' => $set,
'totalActivePost' => Post::CountActivePostCategory($detailCat->id),
'detailCat' => $detailCat
]);
You're missing a get() in your query. The way you have it, you're trying to count query builder (SQL query string) instead of the collection you would get by putting get() on the end of your builder. Change your code to this:
public function scopeCountActivePostCategory($id)
{
$query = DB::table('post')->where([
['status', 1],
['category', $id],
])->get(); //Get here
return $query->count();
}
EDIT
The calling of CountActivePostCategory function might be a problem as well. You are calling static function CountActivePostCategory but you didn't define it as static in your Post model. Change your function to this:
public static function countActivePostCategory($id){
//body
}
And then call it in your controller like this:
'totalActivePost' => Post::countActivePostCategory($detailCat->id)
My controller method like below
public function index($book_id) {
print_r($book_id);
}
I will get the id from view. View like below
I need to get specific row according id on model how can I do that if I use query in controller its not working
If I use like below in controller it gives something else as result
public function index($book_id) {
print_r($book_id);
$this->db->select('*');
$this->db->from('books')->where('book_id', $book_id);
$query = $this->db->get();
print_r($query);
}
but if I use same query in model with hard coded id for testing it gives the expected out put
Please help me with this
You don't need to include "/index" in anchor tag href as controller default method will be index if found. And "print_r($book_id)" should be "echo $book_id" as $book_id is variable not array.
Please try this code (either in model/controller):
$query = $this->db->get_where('books', array('book_id' => $book_id));
$result = $query->row_array();
print_r($result);
i want to display data from my database
controller
public function generatePDF(Request $request)
{
$id = Auth::user()->id;
$name = User::select("NAME")->where("id", $id)->get();
$pdf = PDF::loadView('generatePDF', ['name'=>$name]);
return $pdf->stream('generatePDF.pdf');
}
blade
<h2>{{name}}</h2>
result
[{"NAME":"name_from_database"}]
can i display without [{"name":}], so just the value of data (name_from_database) ?
Simple use find like this
$name = User::find($id)->name;
Or direct from auth
$name = \Auth::user()->name;
To get logged in user id you can use \Auth::id()
you can use:
$user_id = User::findOrFail($id)->first();
$name=$user_id->name;
test the laravel log file:
\Log::info($name);
Use first() instead of get(), because get() will return an array and first() will return the object.
DRY Code line no.1 and no.2. simple use:
$name = auth()->user()->name;
to get the name of the currently authenticated user.
Send $name to you view is fine
$pdf = PDF::loadView('generatePDF', ['name' => $name]);
Correct your code in blade file
{{ name }} -> {{ $name }}
I hope this might help you. :)
Hello Aldi Rostiawan,
$nameGet = User::select("NAME")->where("id", $id)->get();
$nameFirst = User::select("NAME")->where("id", $id)->first();
Here in both line of code the difference is only Get and First.
Get method returns an Array of objects. So for example if the query apply on many records then the code will give you many objects in an array. Or if query will be true for only one record then also it will return an array with one object.
If you know that id is primary key of he table then only one record will be fetched then you can use First function instead if Get method.
First function always return an Object. In case if query apply on many records then also first method will return you only first record as an Object.
And it seems that you have required an Object only.
You should try this:
Controller
public function generatePDF(Request $request)
{
$id = Auth::user()->id;
$rsltUser = User::where("id", $id)->first();
$pdf = PDF::loadView('generatePDF', ['rsltUser'=>$rsltUser]);
return $pdf->stream('generatePDF.pdf');
}
blade
<h2>{{$rsltUser->name}}</h2>
use VALUE() to display name only.
in your case:
<h2>{{$rsltUser->value('name')}}</h2>
your name variable is an array containing a single object with a NAME attribute .. so to diplay that value, you should change your script to
<h2>{{name[0]['NAME']}}</h2>
I am new to Laravel and have been banging my head on this issue for a while. I am merely trying to get a single row from a table and hand it off to a view.
The table has two fields that should match up with this query, the first of course is the id and the second is user_id which matches the logged in user. When its done I plan on responding with a failure message (no record found that matches id: X).
public function show($id)
{
$data = Partners::where('id', $id);->where('user_id', Auth::user()->id)->get();
return View::make('partners.showone')
->with('data', $data)
->with('title', 'View Record')
->with('breadcrumb', 'View Partner');
}
In my view:
{{ $data->firstName }}
This configuration gives me an error of:
Undefined property: Illuminate\Database\Eloquent\Builder::$firstName
Typo...you have a ; in the middle there....it should be....
$data = Partners::where('id', $id)->where('user_id', Auth::user()->id)->get();
but your query really should be this..
$data = Partners::find($id)->where('user_id', Auth::user()->id)->first();
if you just want one row
EDIT (based on your comments below)
For your phones in your Partner model (Im working with Laravel 3 btw)
function phone(){
return $this->hasMany('Phone');
}
Then with the above $data query, you can get the phone by...
$data->phone();
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.