It seems like Laravel 4 cannot get an relation when I use the ::where() syntax. So this is what I have:
<?php
// works fine
$questions = Auth::user()->questions;
// error
$questions = User::where('profilename', '=', $username)->questions;
// error
$questions = User::where('profilename', '=', $username)->get()->questions;
?>
The first method just works fine, but the second an third not.
These give the following error:
"Undefined property: Illuminate\Database\Eloquent\Builder::$questions "
Any idea on how I can make this work? Thanks.
Try:
$questions = User::where('profilename', '=', $username)->first()->questions;
Relationships only works from a model and not an array of models which you have when you use get()
Related
When I'm trying to eager load my Model with relation like that:
$plants = Plant::with('history')
->get()
->where('user_id', auth()->id());
I want to modify those extracted dates with my function like that:
foreach ($plants as $plant) {
$plant->watered_at = self::getDateForHumans($plant->history->watered_at);
$plant->fertilized_at = self::getDateForHumans($plant->history->fertilized_at);
}
I get this kind of error:
ErrorException
Trying to get property 'watered_at' of non-object
but if I try to debug it by dd() function i get a positive result
dd(self::getDateForHumans($plant->history->watered_at));
dd() result
Does anybody know how to fix it or what is the workaround?
seems your history has no watered_at member, so it comes to this error.
Check first if you can access this member with
if(isset($plant->history->watered_at)){
$plant->watered_at = self::getDateForHumans($plant->history->watered_at);
}
I have a piece of code like this:
$products = Product::all()
if ($search_value) {
$products = $products->where('name', 'LIKE', "%$search_value%");
}
$products = $products->orderBy('created_at', 'desc')->skip(10)->take(10)->with('tags')->get();
I got the following error:
BadMethodCallException in Macroable.php line 81:
Method orderBy does not exist.
I guess orderBy need to follow Product:: directly, but I can't save $products = Product::, can I?
Any suggestions? Thanks.
You're trying to use orderBy() method on Eloquent collection. Try to use sortByDesc() instead.
Alternatively, you could change $products = Product::all(); to $products = new Product();. Then all your code will work as you expect.
just use the one line code it will work fine
$product= Product::orderBy('created_at','desc')->get();
use sortByDesc('id') or simple sortBy() inside use the variable through which you wanna sort like i add id
If you want to get the list of all data and grab it in descending order try this:
$post = Post::orderBy('id', 'DESC')->get();
You are first getting all() data and then trying to sort which is wrong. You have to fix this by removing
$products = Product::all()
and changing your code into something like this
if ($search_value) {
$products = Product::where('name', 'LIKE', "%$search_value%");
}
else {
$products = Product::orderBy('created_at', 'desc')->skip(10)->take(10)->with('tags')->get();
}
Hope you get idea to tweak your code.
Your query is wrong.
remove all from $products = Product::all() and then put get() at the end of your query.
$table_Data = DB::table('tbl_product')->orderBy('id','DESC');
You can use this...
I have a problem that I can't fix :
this request works in PHPMYADMIN
SELECT cronState.name, command.name, parameter.keyword, eventParameterValue.value
FROM cronState, commandInstance, command, eventParameterValue, parameter
WHERE cronState.id = commandInstance.cronState_id
AND commandInstance.command_id = command.id
AND eventParameterValue.commandInstance_id = commandInstance.id
AND eventParameterValue.parameter_id = parameter.id
AND cronState.id =32
it returns :
name name keyword value
on20 DigitalPinSet State 1
on20 PwmPinSet DutyCycle 20
and it's ALL I want.
LITTLE EDIT : I use an AJAX Request(with a state_name) to this method which should returns the query result.
Now, when I try to implement it with the Laravel Query Builder, it returns some errors.
My code:
$cronState_id = DB::table('cronState')
->where('name', Input::get('state_name'))
->first();
$cronEvent = DB::table('cronState')
->join('commandInstance', 'commandInstance.cronState_id', '=', 'cronState.id')
->join('command', 'commandInstance.command_id', '=', 'command.id')
->join('eventParameterValue', 'eventParameterValue.commandInstance_id', '=', 'commandInstance.id')
->join('eventParameterValue', 'eventParameterValue.parameter_id', '=', 'parameter.id')
->where('cronState.id', '=', $cronState_id->id)
->select('cronState.name', 'command.name', 'parameter.keyword', 'eventParameterValue.value');
foreach($cronEvent as $result){
$ev = $result['keyword'];
}
return $ev;
The error for this code :
{"error":{"type":"ErrorException","message":"Undefined index:
keyword","file":"/var/www/stage/app/controllers/EventController.php","line":48}}
if I change
foreach($cronEvent as $result){
$ev = $result['keyword'];
}
return $ev;
to
return $cronEvent;
(just delete the foreach loop and rename the returning variable), I have this error :
{"error":{"type":"ErrorException","message":"Object of class
Illuminate\Database\Query\Builder could not be converted to
string","file":"/var/www/stage/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Response.php","line":361}}
So, how can access to my data in the object ? Maybe my Query Builder implementation doesn't works...
Thanks for any help !
There is missing get() at the end of that query builder, thus you don't run the query at all. That's it.
I've some problem i can't understand:
I have many-to-many relationship model, if i use ::find(x) it's working alright, but if i use ::where() i get
Undefined property: Illuminate\Database\Eloquent\Collection::$dlists
Here's my code:
$employee = Employee::whereUsername('xyz')->get();
$lists = $employee->dlists;
returns the error.
$employee = Employee::find(1);
$lists = $employee->dlists;
returns the needed output.
what am i mising?
It's because the query returns a Eloquent Collection, if the usernames are unique you can call whereUsername('xyz')->first() then you get an Employee Object where you will have access to dlists.
I have a Poll table, a Students table, and a pivot table between them that includes a token and their three votes.
public function students()
{
return $this->belongsToMany('Student', 'polls_students')->withPivot('token','first','second','third');
}
While working out saving the poll results, I came across some odd behavior that I don't quite understand. I'm hoping somebody can explain what it is I'm missing:
$poll = Poll::find(Input::get('poll_id'));
foreach($poll->students()->where('students.id', '=', Input::get('student_id'))->get() as $student){
var_dump($student->pivot->token);
}
$student = $poll->students()->where('students.id', '=', Input::get('student_id'))->get();
var_dump($student->pivot->token);
In the above code, the foreach loop will successfully display the token, where the second one throws the exception Undefined property: Illuminate\Database\Eloquent\Collection::$pivot
What am I missing? Are these two calls not logically creating the same object? How is 'pivot' working on the first and not the latter?
You first example:
$poll = Poll::find(Input::get('poll_id'));
foreach($poll->students()->where('students.id', '=', Input::get('student_id'))->get() as $student){
var_dump($student->pivot->token);
}
Here $poll->students() retrieves a collection and because of foreach loop you get a single object in your $student variable and you can use $student->pivot->token
You second example:
$student = $poll->students()->where('students.id', '=', Input::get('student_id'))->get();
var_dump($student->pivot->token);
Here you are doing same thing, using $poll->students() you are getting a collection but this time you are not using a loop and trying to do same thing using $student->pivot->token but it's not working because you didn't define any index from which you want to get the pivot->token, if you try something like this
$student->first()->pivot->token
Or maybe
$student->get(1)->pivot->token
Or maybe you can use first() instead of get() like this
$student = $poll->students()->where('students.id', '=', Input::get('student_id'))->first();
Then you can use
$student->pivot->token
Remember that, get() returns a collection even if there is only one record/model.
$poll = Poll::find(Input::get('poll_id'));
foreach($poll->students as $student){
var_dump($student->pivot->where('student_id',$student->id)->where('poll_id',$poll->id)->first()->token);
}