I get the counts of 3 different tables with 3 different SQL queries.
Here :
$Count['FirstRecords']= DB::table('table1')->where('Id',$slug)->count('Id');
$Count['SecondRecords']= DB::table('table2')->where('Id',$slug)->count('Id');
There aren't any problems when I check the content on controller with print_r function. I get an output like the following:
Array
(
[FirstRecords] => 178
[SecondRecords] => 302
)
Then I sent this array to the blade template via view function.
return view('View/CountView',['Records' => $Count]);
I'm trying to print on the blade like this:
{{$Records->FirstRecords}}
I get the following error every time when I trying to print this array on the blade template.
Undefined variable: Records (View: {Path}
Related
I have a table named settings, in it, there are several columns, id,company_id, key, value.
In my Laravel model for Setting, I have the following cast:
protected $casts = [
'value' => 'array',
];
But when I go to retrieve data that has been stored, I can't.
For example, I have a record with the following value: "{\"default_remit_address\":\"2395\"}"
And when I go to retrieve the record in a Blade, it does pull it up correctly, but I'm not sure how to grab a specific value from the value field (like default_remit_address).
If I print the return "{{$settings->value}}" directly in the Blade, this is what I get:
{"default_remit_address":"2395"}
So how can I go one level deeper?
As this json object is being cast to an Array, you can just use regular array syntax to access it's contents.
$default_remit_address = Settings::find(1)->value['default_remit_address'];
or in your blade template
{{ $settings->value['default_remit_address'] }}
I have passed $id from the controller as:
$data['getId'] = $id; // say value = 12
return view('Administrator.notification.index',$data);
However, in the view file when I used {{getId}} , it show me the error:
Use of undefined constant getId - assumed 'getId'
The $data variable is acessible from the view, not getId. $data is an array, getId is a key of the $data array and 12 your value according to the key value.
Try printing out something like: {{$data['getId']}}
If you want to see the output of the variable, for debugging purposes, use {{dd($data)}}
https://laravel.com/docs/5.6/views
update this line from
return view('Administrator.notification.index',$data);
To
return view('Administrator.notification.index',compact('data'));
and then in view u can access it like this,
{{ $data['getId'] }}
The error is you are missing the $ sign. It's {{$getId}} not {{getId}}.
I am using rtconner/laravel-tagging package to get tags functionality to my app.
I can count attached tags by $o->tags->count()
I can loop the tags by a foreach: #foreach($o->tags as $t).
print attached tags by
the problem
Now I want to get a collection of random 10 Quotation with no tags attached.
While I can print a random 10 pieces with a given attribute:
$object = Quotation::where('deepness', null)->get()->random(10);
(Note: I have a random scope defined in the model, irrelevant for my issue)
... but this code, cloned from another model doesn't work:
$object = Quotation::whereHas('tags','>',0)->get()->random(10);
It produces this error message:
FatalThrowableError in Builder.php line 880:
Type error: Argument 2 passed to Illuminate\Database\Eloquent\Builder::whereHas() must be an instance of Closure, string given
I have also tried to execute this query
$object = Quotation::has('tags')->get()->random(10);
but I got this:
```
BadMethodCallException in Builder.php line 2431:
Call to undefined method Illuminate\Database\Query\Builder::tags()
```
Note 2: In the source model (the one I cloned from) the relation was counting a hasMany relation.
to do
Please help me to create the collection of Quotations with no tags assigned
Had the same problem and solved it this way:
$objects = Quotation::all();
$objects = $objects->filter(
function ($object, $key) {
return $object->tags->count() > 0;
}
)->random(10);
Hope issues still relevant :)
This is a Noob question , but i have searched a lot with no luck.
I am returning array of objects from model to controller and i have to pass it to another model. I will have to convert it but how ?
This is my controller :
$data['customer_phoneno'] = $this->session->userdata('customer_phoneno');
$data['cid']=$this->Stylish_wizard->getCid($this->session->userdata('customer_phoneno'));
$data['bookings']=$this->Myaccount_customer->getBookings($cid);
Model :
public function getBookings($cid)
{
$this->db->where('cid', $cid);
$this->db->from('bookings');
$query = $this->db->get();
return $query->result();
}
I want to get the data from one model and use it to get some other data from another model.
I had tried passing $data['cid'] but got this error
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: database/DB_query_builder.php
Line Number: 662
Backtrace:
File: C:\xampp\htdocs\style\application\models\Myaccount_customer.php
Line: 24
Function: where
File: C:\xampp\htdocs\style\application\controllers\Myaccount.php
Line: 95
Function: getBookings
File: C:\xampp\htdocs\style\index.php
Line: 292
Function: require_once
A Database Error Occurred
Error Number: 1054
Unknown column 'Array' in 'where clause'
SELECT * FROM `bookings` WHERE `cid` = `Array`
Filename: C:/xampp/htdocs/style/application/models/Myaccount_customer.php
Line Number: 26
I tried passing
$data['cid']->cid
But its not working
When i printed $data['cid'] i got
Array ( [0] => stdClass Object ( [cid] => 8 ) )
So how do i convert it in string ?
Then you can achieve it using as
Controller
public function your_function(){
$this->load->model('folder_name/my_calling_model');//initialize that model
//your rest code
$data['my_data'] = $this->my_calling_model->my_calling_function();//your function to be called
}
You can call another model within your code as like above code,but it seems you might be having a typo within your code while passing $cid it seems to be $data['cid'] instead. So your code looks like
$data['bookings']=$this->Myaccount_customer->getBookings($data['cid']);//<----- changed from $cid to $data['cid']
Correct me if I am wrong, but I think you want to achieve this:
$data['customer_phoneno'] = $this->session->userdata('customer_phoneno');
$data['cid']=$this->Stylish_wizard->getCid($this->session->userdata('customer_phoneno'));
$data['bookings']=$this->Myaccount_customer->getBookings($data['cid']);
Make sure to pass the right array / object element.
edit:
Like I said: Make sure to pass the right array or object element. $data['cid'] contains an array. But you have to pass a String(the id). So you have to look at this array and pass the right element with your id.
I am trying to output a variable like this: <h1>{{ $unitcode }}</h1>
I have this code making that variable:
// Validation was successful.
$inputcode = Input::get('unitcode');
// First validation is successful. Next to check that it is a correct unit code.
$unitcode = DB::table('unitcodes')->where('unitcode', $inputcode)->first();
if(!$unitcode) {
// Input code is wrong. The unit does not exist.
return Redirect::route('get-buy')
->with('global', 'That unit code does not exist. Try again.');
} else {
// Success! Unit code exists!
return View::make('showbooks')
->with('unitcode', $unitcode);
}
When I run everything it gives me this:
ErrorException (E_UNKNOWN)
Array to string conversion (View: ..BLAH goes on to display path to view.
How can I get it to display the variable I want that was pulled from the DB?
Thanks!
In Laravel's DB abastraction layer a row from a database table (result of a SELECT ... LIMIT 1 - DB::...->first()) is an array.
You can send any kind of variables using View::...->with but you need to use them properly in the template itself ('showbooks').
You most probably are doing a {{{ unitcode }}} in the template which is actually similar to executing an echo $unitcode. Now if $unitcode is an array and echo requires a string then PHP automatically tries to convert it and fails (ErrorException (E_UNKNOWN)
Array to string conversion...).
What you need to do is use it correctly:
{{{ unitcode['unitcode'] }}}
And this will work because $unitcode is a key-value dictionary where each key is a column from the DB table with its associated value.