If I understand your problem fully, then I think you are over-coding the issue.
With Laravel, you can do query your model from the controller like this:
public function get_download($file){
// $file= "4864654684";
$e = Contact::where('file', $file)->first();
dd($e);
}
This will return the row as a Collection that you can then manipulate as you wish. You can safely remove the method on the model completely.
What you are doing in the code example above is searching the Contact model to find a a match in the 'file' column that matches the value of the $file param and then just get the first result that matches.
Does that help?
It bothers me a lot as I return these 3000 lines of text about this, I would like to avoid it ... because it stops my browser ...
object (Illuminate \ Database \ Eloquent \ Builder) # 358 (4) {["query": protected] => object (Illuminate \ Database \ Query \ Builder) # 351 ....
I do not understand that you can not run querys from the model, then what is there? I think it's wrong to do it from the controller, then where is the MVC ....
well back to the query in the model, when the array returned is something but when you do not see me back ....
This eloquent object ....
object (Illuminate \ Database \ Eloquent \ Builder) # 358 (4) {["query": protected] => object (Illuminate \ Database \ Query \ Builder) # 351 ....
How many lines are returning? It should be returning a single object which has a few lines of text, but that is irrelevant.
Are you saving the a file to the database (blob). That isn't best practice really. Your DB will die quite quickly.
Can you explain what it is you are trying to do and your general set up?
I want to query the database from the model .... why is this so difficult?
look ... if I do this from the controller ...
$ek = DB::table('carrers_contact')->where('file', $file)->first();
var_dump($ek);
exit;
if the result is returned ...
object(stdClass)#359 (11) { ["id"]=> int(1) ["id_carrers_job"]=> int(3) ["first_name"]=> string(4) "test" ["last_name"]=> string(9) "Test" ["email"]=> string(24) "[email protected]" ["city"]=> string(15) "test" ["state"]=> int(2) ["file"]=> string(13) "8949498484646" ["file_type"]=> string(3) "pdf" ["created_at"]=> string(19) "2014-09-26 17:49:16" ["updated_at"]=> string(19) "2014-09-29 15:28:24" }
if you do not find anything ...
NULL
all perfect, until there ...
but if I make a call to a method of the model ....
this is the CONTROLLER...
$ek=Contact::Findfile($file);
var_dump($ek);
This is the MODEL ...
public function ScopeFindfile($query,$file){
return DB::table('carrers_contact')->where('file', $file)->first();
}
// The $ query parameter, I read that you had not put that well, but so is laravel ...: S
if find returns a result ...
object(stdClass)#359 (11) { ["id"]=> int(1) ["id_carrers_job"]=> int(3) ["first_name"]=> string(4) "test" ["last_name"]=> string(9) "Test" ["email"]=> string(24) "[email protected]" ["city"]=> string(15) "test" ["state"]=> int(2) ["file"]=> string(13) "8949498484646" ["file_type"]=> string(3) "pdf" ["created_at"]=> string(19) "2014-09-26 17:49:16" ["updated_at"]=> string(19) "2014-09-29 15:28:24" }
if returns not find anything ...
object (Illuminate \ Database \ Eloquent \ Builder) # 358 (4) {["query": protected] => object (Illuminate \ Database \ Query \ Builder) # 351 (23) {["connection": protected] => object (Illuminate \ Database \ MySqlConnection) # 344 (16) {["I pdo": protected] => object (PDO) # 345 (0) {} ["readPdo": protected] => NULL ["queryGrammar": protected ] => object (Illuminate \ Database \ Query \ Grammars \ MySqlGrammar) # 346 (2) {["selectComponents": protected] => array (11) {[0] => string (9) "aggregate" [1] => string (7) "columns" [2] => string (4) "from" [3] => string (5) "joins" [4] => string (6) "wheres" [5] => string (6) "groups" [6] => string (7) "havings" [7] => string (6) "orders" [8] => string (5) "limit" [9] => string ( 6) "offset" [10] => string (4) "lock"} ["tablePrefix": protected] => string (7) "barcel_"} ["schemaGrammar": protected] => NULL ["postprocessor" protected] => object (Illuminate \ Database \ Query \ Processors \ MySqlProcessor) # 347 (0) {} ["events": protected] => object (Illuminate \ Events \ Dispatcher) # 14 (5) {["container" : protected] => object (Illuminate \ Foundation \ Application) # 2 (16) {["booted": protected] => bool (true) ["bootingCallbacks": protected] => array (0) {} ["bootedCallbacks ": protected] => array (2) {[0] => object (Closure) # 74 (2) {[" static "] => array (2) {[" app "] => * RECURSION * [" I "] => object (Illuminate \ View \ ViewServiceProvider) # 67 (2) {[" app ": protected] => * RECURSION * [" defer ": protected] => bool (false)}} [" this " ] => object (Illuminate \ View \ ViewServiceProvider) # 67 (2) {["app": protected] => * RECURSION * ["defer": protected] => bool (false)}} [1] => object (Closure) # 46 (1) {["static"] => array (2) {["app"] => * RECURSION * ["env"] => string (10) "production"}}} [" finishCallbacks ": protected] => array (0) {} [" shutdownCallbacks ": protected] => array (0) {} [" middleware ": protected] => array (1) {[0] => array (2 ) {["class"] => string (26) "Illuminate \ Http \ FrameGuard" ["parameters"] => array (0) {}}} ["serviceProviders": protected] => array (16) {[ 0] => object (Illuminate \ Events \ EventServiceProvider) # 11 (2) {["app": protected] => * RECURSION * ["defer": protected] => bool (false)} [1] => object (Illuminate \ Exception \ ExceptionServiceProvider) # 15 (2) {["app": protected] => * RECURSION * ["defer": protected] => bool (false)} [2] => object (Illuminate \ Routing \ RoutingServiceProvider) # 26 (2) {["app": protected] => * RECURSION * ["defer": protected] => bool (false)} [3] =>
AND SO AS TO LINES !! 10000
Its not difficult at all. Its insanely easy with Laravel.
There are differences in the objects that are being returned. You can do different things with them. In a production app, you aren't going to be var_dumping - I hope!!!
An object is being return whatever method you are using - Be it a stdClass or a model object. If you use my example above, you'll receive an object that you can do a lot with.
What is being returned is processed in milliseconds and isn't a performance hit. If you want to limit what is returned, you can use things like ->select('first_name') or ->lists('first_name').
If you are trying to get a single element try: $ek->first_name;.
You haven't explained what you are trying to achieve. A var_dump isn't the way you would produce anything of value. Are you trying to create a json api? Pass the data to a view? What?
I'd suggest reading the docs and learning a little about what you are doing and what you are getting in return.
good as you said, we will continue with the MVC rule using MODELS.
that way, I know. the call to the method in the model did not find results ,,. ?? like I said if I do from the driver returns me NULL, but if I call the method from a model returns the object me that eloquent ....
this would be the controller
$ Ek = Contact :: FindFile ($ file);
echo (!empty ($ ek)) ?"ok !!!! user": "user not";
but not !!
parameter passing
$ File = 434343434 // VALID PARAMETER
$ File = 11111111111 // INVALID PARAMETER
the variable results found or not printed, "user ok !!"
WHY QUERY METHOD TO MODEL WHEN NOT FIND ANYTHING, NO RETURNS "NULL" or "FALSE" RETURNS THAT OBJECT RARE , ....100000 lines of that thing ....
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community