My question is, why does the following not work?
$questions = Question::where($queryatypes);
$questions->get();
I'm getting the following Error:
Object of class Illuminate\Database\Eloquent\Builder could not be
converted to string
Please check the answer
$questions = Question::where($queryatypes);
$questions = $questions->get();
I think you are trying this on your controller and return this Objects directly
use Illuminate\Http\Response;
public function controllerFunction(){
$queryatypes = .....
$questions = Question::where($queryatypes);
$questions->get();
return response($questions);
}
Related
this is my laravel custom accessor which I am appending using
protected $appends = [leave_balances];
public function getLeaveBalancesAttribute() {
// some code
}
I want to pass a parameter when I am calling this accessor like this
public function getLeaveBalancesAttribute($parameter) {
// use $parameter here
}
$payslip = Payslip::find(1);
\Log::debug($payslip->leave_balances("PARAMETER"));
I have searched and found that it is not possible. please can some one provide any solution to this I need to pass this parameter.
you dont append attribute unless you want it to act as an attribute,
you can just create a method since you are calling it like a method
in you Payslip model
public function leaveBalances( $params ) {
return $params
}
then you can use it like
$payslip = Payslip::find(1);
$payslip->leaveBalances("PARAMETER") // which output PARAMETER
If you declare an Attribute, you can only use it like this (following your example:
protected $appends = ['leave_balances'];
public function getLeaveBalancesAttribute()
{
return 'Hi!';
}
$payslip = Payslip::find(1);
$value = $payslip->leave_balances;
dd($value); // This will output string(Hi!)
What you (I think) want is setLeaveBalancesAttribute, so you can pass a value and do whatever you want with it:
public function setLeaveBalancesAttribute($parameter)
{
return $parameter.' Yes!';
}
$payslip = Payslip::find(1);
$payslip->leave_balances = 'It works!';
dd($payslip->leave_balances); // This will output string(It works! Yes!)
But, if you are using Laravel 9+, please do use the new way of defining attributes, it is better.
You can set the attribute $appends in the model where you have the accessor. Something like this:
protected $appends = ['the name of accessor'];
However, it will be in the most, I think in all, the responses or query you do with the model you declare it.
Another options is creating a single instance of the model using the ::find method. For example:
$model_instance = Model::find($id);
$attribute = $model_instance->attribute;
Here is the documentation reference: https://laravel.com/docs/9.x/eloquent-mutators#defining-an-accessor
Hello I try to get all Methods on a Class Controller on Laravel but I was failed.
I use get_class_methods function to get class methods.
Here is my ConfigController codes:
use Modules\Order\Http\Controllers\V1\Web\OrderController;
class ConfigController extends Controller
{
public function index(){
$methods = get_class_methods(new OrderController::class);
dd($methods);
}
}
After executing the code the result is:
Class "Modules\Order\Http\Controllers\V1\Web\OrderController" not found
And I'm sure that that class exists on that namespace because I have already called it on route.
How to do that on the right way?
Thank you.
Have you tried reflection yet? My example below:
$reflection = new ReflectionMethod($className, $methodName);
$parameter = $reflection->getParameters();
foreach ($parameter as $param) {
echo $parameter->getName();
echo $parameter->isOptional();
}
This is the answer, I just miss s on the method name:
use ReflectionClass;
...
$reflection = new ReflectionClass(OrdersController);
dd($reflection);
...
I am making this query in codeigniter4 and I am stuck
public function consultasHoy(){
$data = $this->consultas->select('pacientes.*,consultas_hc.*')
->from('consultas_hc')
->join('pacientes','consultas_hc.identificacion = pacientes.identificacion')
->where(date_format('consultas_rc.reConsulta','%Y-%m-%d'),date_format(now(),'%Y-%m-%d'))
->orderBy('consultas_hc.reConsulta', 'ASC')
->get();
return json_encode($data);
}
and it returns this error
date_format() expects parameter 1 to be DateTimeInterface, string
given
If you are getting date straightforwardly from the database to your controller then you can print date format in view like below:
$YourDate = $row->start_date;
$newDate = date("d-m-Y", strtotime($YourDate));
Try replacing your where clause with this line of code:
$where="DATE_FORMAT('consultas_rc.reConsulta','%Y-%m-01')= DATE_FORMAT(CURDATE() ,'%Y-%m-01')";
->where($where);
this was the solution used, thanks for the help
public function consultasHoy(){
helper('date');
$hoy=(new \DateTime("now"))->format("Y-m-d");
$builder = $this->consultas->select('*');
$builder->join('pacientes', 'pacientes.identificacion=consultas_hc.identificacion');
$builder->where("DATE(reConsulta)", $hoy);
$query = $builder->get();
return json_encode($query->getResult());
}
How can i achieve something like this?
public function getInformation($model) {
$result = $model::with(['province', 'city']);
if($model == 'App\Models\Business') {
$result->with(['businessProvince', 'businessCity']);
}
$result->get();
}
// call the function
$information->getInformation(\App\Models\Business::class);
i'm getting error
Object of class Illuminate\Database\Eloquent\Builder could not be
converted to string
on the sample code above. Any suggestion is really appreciated.
After taking a fourth look $model should be a string, and $result is an Eloquent Builder instance and never an instance of the model class (since a query was started when with was called).
So the $model == 'App\Models\Business' I would change to $model === \App\Models\Business::class but that should not change the outcome.
Are you sure this error comes from this part of the application? Which line specifically?
Original wrong answer.
You are trying to compare the model instance with a string (since $model::with() created a instance of the model class you passed in the $model argument).
You can use the instanceof keyword for comparing an instance with a class name (http://php.net/manual/en/language.operators.type.php).
if($model instanceof \App\Models\Business) {
$result->with(['businessProvince', 'businessCity']);
}
This solved my problem, thank you guys.
public function getInformation($model) {
$result = $model::with(['province', 'city']);
if($model == 'App\Models\Business') {
// my mistake
//$result->with(['businessProvince', 'businessCity']);
$result = $result->with(['businessProvince', 'businessCity']);
}
$result->get();
}
Right now I'm working on my first update function using Eloquent ORM. Trying to follow the docs, I have this in my model:
public function updateAvailability()
{
$this->active = Input::get('available');
$this->activeDetails = Input::get('availableStatus');
$this->save();
}
which returns:
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
and all of this is being called to in my controller as:
public function updateProfile($id)
{
if(Input::get('type')=='availability'){
$availability = User::find($id)->updateAvailability;
}
$name = str_replace(' ', '', Input::get('name'));
return Redirect::to('people/'.$name);
}
Are there some gaps in my understanding of updating in Eloquent? (I'm sure there are). I would love to use ajax to handle it, but I can't seem to find the right resources to get that working.
SOLVED: $availability = User::find($id)->updateAvailability; needed to be changed to $availability = User::find($id)->updateAvailability();. That's all.