How to fix laravel relationship query where clause?Laravel 4 Query Builder: LEFT JOIN … AND … queryHow to Create Multiple Where Clause Query Using Laravel Eloquent?Laravel query multiple eloquent relationshipsLaravel 4 belongsToMany Relationship Returns EmptyDescending model relationship laravelLoad laravel model where relationship has conditionLaravel: How to write a join count query on belongsToMany relationships?Laravel query builder orderBy not orderingtoSql() on Eloquent Relationship returning `where id = null`Laravel: Proper Querying Many to Many Relationships

Solve equation using Mathematica

Create two random teams from a list of players

How can flights operated by the same company have such different prices when marketed by another?

Word for soundtrack music which is part of the action of the movie

Raindrops in Python

How can you tell the version of Ubuntu on a system in a .sh (bash) script?

Using Python in a Bash Script

Easy way to get process from window

What Marvel character has this 'W' symbol?

Coworker mumbles to herself when working, how to ask her to stop?

How to foreshadow to avoid a 'deus ex machina'-construction

Would people understand me speaking German all over Europe?

How did astronauts using rovers tell direction without compasses on the Moon?

What clothes would flying-people wear?

Typesetting numbers above, below, left, and right of a symbol

How to innovate in OR

Unknown indication below upper stave

Can drawing a weapon be part of ANY kind of move action or just moving?

Patio gate not at right angle to the house

Can I attune a Circlet of Human Perfection to my animated skeletons to allow them to blend in and speak?

Why tantalum for the Hayabusa bullets?

What is the highest achievable score in Catan

Should students have access to past exams or an exam bank?

Why are we moving in circles with a tandem kayak?



How to fix laravel relationship query where clause?


Laravel 4 Query Builder: LEFT JOIN … AND … queryHow to Create Multiple Where Clause Query Using Laravel Eloquent?Laravel query multiple eloquent relationshipsLaravel 4 belongsToMany Relationship Returns EmptyDescending model relationship laravelLoad laravel model where relationship has conditionLaravel: How to write a join count query on belongsToMany relationships?Laravel query builder orderBy not orderingtoSql() on Eloquent Relationship returning `where id = null`Laravel: Proper Querying Many to Many Relationships






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I am trying to get data from the 'files' table through the 'computer_files' pivot table but the query puts an 'is null' into the query and therefore it doesn't return with the data. However, if I change the is null to an ID and run it as an SQL command it does give the data back.



I have tried going through the laravel docs but it didn't help. What I don't want to do is to use raw queries.



This is the files relationship function:



public function files()
return $this->belongsToMany('AppFile', 'computer_files', 'computer_id', 'file_id');



This is what I do to get the data and currently gives back an empty collection:



$computer = Computer::where('computer_id', session('computer_id'))->first();
pr($computer->files);
die;


Also the 'toSql()' gives this:



select * from `files` inner join `computer_files` on `files`.`id` = `computer_files`.`file_id` where `computer_files`.`computer_id` is null


I expect to see an ID in the query instead of 'is null' so then it will give back the results that I need.










share|improve this question
























  • I would guess your session('computer_id') is null.

    – GTCrais
    Mar 26 at 21:43











  • already checked on that and it is not because that is being set when you login and if i set it to a number it is still the same. also if it would be null then the '$computer' would be null and therefore the files variable would fail.

    – M4ST3RX
    Mar 26 at 21:58












  • computer_id is the primary key in the computers table, right? Have you set protected $primaryKey = 'computer_id'; in the Computer model?

    – Jonas Staudenmeir
    Mar 26 at 22:19












  • yes that is the primary key and i did not set it. but as soon as I did the query changed to where `computer_files`.`computer_id` = ?. Thanks

    – M4ST3RX
    Mar 26 at 22:33

















0















I am trying to get data from the 'files' table through the 'computer_files' pivot table but the query puts an 'is null' into the query and therefore it doesn't return with the data. However, if I change the is null to an ID and run it as an SQL command it does give the data back.



I have tried going through the laravel docs but it didn't help. What I don't want to do is to use raw queries.



This is the files relationship function:



public function files()
return $this->belongsToMany('AppFile', 'computer_files', 'computer_id', 'file_id');



This is what I do to get the data and currently gives back an empty collection:



$computer = Computer::where('computer_id', session('computer_id'))->first();
pr($computer->files);
die;


Also the 'toSql()' gives this:



select * from `files` inner join `computer_files` on `files`.`id` = `computer_files`.`file_id` where `computer_files`.`computer_id` is null


I expect to see an ID in the query instead of 'is null' so then it will give back the results that I need.










share|improve this question
























  • I would guess your session('computer_id') is null.

    – GTCrais
    Mar 26 at 21:43











  • already checked on that and it is not because that is being set when you login and if i set it to a number it is still the same. also if it would be null then the '$computer' would be null and therefore the files variable would fail.

    – M4ST3RX
    Mar 26 at 21:58












  • computer_id is the primary key in the computers table, right? Have you set protected $primaryKey = 'computer_id'; in the Computer model?

    – Jonas Staudenmeir
    Mar 26 at 22:19












  • yes that is the primary key and i did not set it. but as soon as I did the query changed to where `computer_files`.`computer_id` = ?. Thanks

    – M4ST3RX
    Mar 26 at 22:33













0












0








0








I am trying to get data from the 'files' table through the 'computer_files' pivot table but the query puts an 'is null' into the query and therefore it doesn't return with the data. However, if I change the is null to an ID and run it as an SQL command it does give the data back.



I have tried going through the laravel docs but it didn't help. What I don't want to do is to use raw queries.



This is the files relationship function:



public function files()
return $this->belongsToMany('AppFile', 'computer_files', 'computer_id', 'file_id');



This is what I do to get the data and currently gives back an empty collection:



$computer = Computer::where('computer_id', session('computer_id'))->first();
pr($computer->files);
die;


Also the 'toSql()' gives this:



select * from `files` inner join `computer_files` on `files`.`id` = `computer_files`.`file_id` where `computer_files`.`computer_id` is null


I expect to see an ID in the query instead of 'is null' so then it will give back the results that I need.










share|improve this question














I am trying to get data from the 'files' table through the 'computer_files' pivot table but the query puts an 'is null' into the query and therefore it doesn't return with the data. However, if I change the is null to an ID and run it as an SQL command it does give the data back.



I have tried going through the laravel docs but it didn't help. What I don't want to do is to use raw queries.



This is the files relationship function:



public function files()
return $this->belongsToMany('AppFile', 'computer_files', 'computer_id', 'file_id');



This is what I do to get the data and currently gives back an empty collection:



$computer = Computer::where('computer_id', session('computer_id'))->first();
pr($computer->files);
die;


Also the 'toSql()' gives this:



select * from `files` inner join `computer_files` on `files`.`id` = `computer_files`.`file_id` where `computer_files`.`computer_id` is null


I expect to see an ID in the query instead of 'is null' so then it will give back the results that I need.







laravel eloquent relationship






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 26 at 21:40









M4ST3RXM4ST3RX

137 bronze badges




137 bronze badges















  • I would guess your session('computer_id') is null.

    – GTCrais
    Mar 26 at 21:43











  • already checked on that and it is not because that is being set when you login and if i set it to a number it is still the same. also if it would be null then the '$computer' would be null and therefore the files variable would fail.

    – M4ST3RX
    Mar 26 at 21:58












  • computer_id is the primary key in the computers table, right? Have you set protected $primaryKey = 'computer_id'; in the Computer model?

    – Jonas Staudenmeir
    Mar 26 at 22:19












  • yes that is the primary key and i did not set it. but as soon as I did the query changed to where `computer_files`.`computer_id` = ?. Thanks

    – M4ST3RX
    Mar 26 at 22:33

















  • I would guess your session('computer_id') is null.

    – GTCrais
    Mar 26 at 21:43











  • already checked on that and it is not because that is being set when you login and if i set it to a number it is still the same. also if it would be null then the '$computer' would be null and therefore the files variable would fail.

    – M4ST3RX
    Mar 26 at 21:58












  • computer_id is the primary key in the computers table, right? Have you set protected $primaryKey = 'computer_id'; in the Computer model?

    – Jonas Staudenmeir
    Mar 26 at 22:19












  • yes that is the primary key and i did not set it. but as soon as I did the query changed to where `computer_files`.`computer_id` = ?. Thanks

    – M4ST3RX
    Mar 26 at 22:33
















I would guess your session('computer_id') is null.

– GTCrais
Mar 26 at 21:43





I would guess your session('computer_id') is null.

– GTCrais
Mar 26 at 21:43













already checked on that and it is not because that is being set when you login and if i set it to a number it is still the same. also if it would be null then the '$computer' would be null and therefore the files variable would fail.

– M4ST3RX
Mar 26 at 21:58






already checked on that and it is not because that is being set when you login and if i set it to a number it is still the same. also if it would be null then the '$computer' would be null and therefore the files variable would fail.

– M4ST3RX
Mar 26 at 21:58














computer_id is the primary key in the computers table, right? Have you set protected $primaryKey = 'computer_id'; in the Computer model?

– Jonas Staudenmeir
Mar 26 at 22:19






computer_id is the primary key in the computers table, right? Have you set protected $primaryKey = 'computer_id'; in the Computer model?

– Jonas Staudenmeir
Mar 26 at 22:19














yes that is the primary key and i did not set it. but as soon as I did the query changed to where `computer_files`.`computer_id` = ?. Thanks

– M4ST3RX
Mar 26 at 22:33





yes that is the primary key and i did not set it. but as soon as I did the query changed to where `computer_files`.`computer_id` = ?. Thanks

– M4ST3RX
Mar 26 at 22:33












0






active

oldest

votes










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55366620%2fhow-to-fix-laravel-relationship-query-where-clause%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes




Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.







Is this question similar to what you get asked at work? Learn more about asking and sharing private information with your coworkers using Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55366620%2fhow-to-fix-laravel-relationship-query-where-clause%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript