Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
This post was edited and submitted for review 8 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
My Laravel App is showing this message:
Call to a member function where() on null
My Controller file looks:
{
$product_category=product_category::find('id')->where('id')->get();
return view('product_category', compact('product_category'));
}
Can you guys help me to solve this?
this syntax is not correct!
read this document for find():
https://laravel.com/docs/9.x/eloquent#retrieving-single-models
read this document for where():
https://laravel.com/docs/9.x/queries#where-clauses
my description:
when you want to find a single product_category row you must write this (for example product_category with id 7):
$product_category=product_category::find(7);
or
$product_category=product_category::where('id', 1)->first();
when you use first() function at the end of your code, its return a single row and that is the first row. when using get() function at the end of your code, its return a set of rows that are in your Query.
when you use find() and its return value is null, you cant call where() after that!
I think you must write your controller like this:
public function productCategory($id)
{
$product_category=product_category::find($id);
return view('product_category', compact('product_category'));
}
I suggest you read about Laravel Eloquent much:
https://laravel.com/docs/9.x/eloquent
and about Laravel Query builder:
https://laravel.com/docs/9.x/queries
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
I have code like this:
$subOffers = SubOffer::get()->where('offers_id', 1);
And when i return $subOffers i got an array like this.
[{"id":1,"offers_id":"1","price":"123.0","start_date":"2022-07-23","stop_date":"2022-07-24","additional_info":"r832ufr803yw98fhew98f8h93wq"}]
But when i change 1 to 2 or 3 or 4 or ... i got an object like this.
{"3":{"id":4,"offers_id":"4","price":"12,341","start_date":"2022-07-10","stop_date":"2022-07-13","additional_info":null}}
in this case i changed it to 4 and I got n-1 key.
What's going on here? Why do I receive an object instead of array? It happens when the offers_id is above 1. Offers_id is a foreign key.
get() method on eloquent returns a laravel collection, anything you put after that, e.g. where(), is affecting the generated collection see https://laravel.com/docs/9.x/collections, collection is an object similar to an array, but with a large amount of functions similar to database queries but all is in local memory.
If you have where() method before the get(), you are building a database query and it is not executed until the get(). You are getting different results because query building and collection methods work in similar ways but they are doing different things.
Okay, I found a solution
$subOffers = SubOffer::where('offers_id', $id)->get();
just I had to swap where with get
But still I don't know, what's going on here? I want to know, why is this happened.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 months ago.
Improve this question
I am quite new to laravel and I have a weird problem with foreach() blade directive.
the problem is when I want to iterate over a collection it throws an error and says:
foreach() argument must be of type array|object, string given
but I have printed the collection using print_r() method and it the variable does not hold any string, it hold a collection object.
that is the piece of code that throws the error while reaching the foreach directive:
<section class="col-span-8 col-start-5 mt-10 space-y-6">
{{print_r($post->comments)}}
#foreach($post->comments as $comment) // here
<x-post-comment :comment="$comment"></x-post-comment>
#endforeach
</section>
basically, in the above code in the foreach I have a Eloquent relationship and between Post and Comment Models and the relationship works perfectly in tinker, but here it says that it is not a collection or an array. also, I have a post-comment blade component that renders the layout for a commment inside the foreach.
and that is the implementation of the relationship between each post and its comments:
function comments()
{
return $this->hasMany(Comment::class);
}
as I mentioned, the above relationship works perfectly, but I don't know what is wrong with it.
any help would be appreciated!
The problem was inside <x-post-comment> component I was writing #props('comment') instead of #props(['comment']) So, I was missing the square brackets around 'comment'.
The error message was about foreach not props that why I was thinking all about the foreach part.
(answered by #Yassin in the comments)
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have a table where I want to delete a member from the club but I don't want to delete the member from the database, just leave the member without the club, reason why I'm trying to use patch instead of delete in this case. I tried to set the value to null but it is returning me this error Trying to get property 'NULL' of non-object, is there a way I could do this?
I am not using migrations for the table, it's a table I already had created in postgreSQL, the column I'm trying to edit is nullable.
This is what I'm trying to do:
$trash = null;
Member::where('doc_iden',$id)->update(array(
'id_club'=> $trash,
));
Membresia::where(['id_lec'=>$id,'id_club'=>$idclub,'fec_f'->$trash])->update(array(
'fec_f'=>date('Y-m-d'),
));
I tried to do it with NULL instead of null and setting the value directly without using a variable but I have the same error and I don't know what to do here.
Update: The first bit works, the second doesn't
You have a typo.
'fec_f'->$trash]
should be
'fec_f'=>$trash]
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm learning PHP POO and I'm having trouble using the variables to the META TITLE and DESCRIPTION. I've checked all the topics related but couldn't find an example close to my problem.
Here's the part of the code concerned :
$model_Article = new Model_Article($db);
$contenuArticle = $model_Article->StockageDonneesById($id);
// die(var_dump($contenuArticle));
$affichage = new View_Article($contenuArticle->title(),$contenuArticle->description());
$affichage->setContenuArticle($contenuArticle);
$affichage->head();
$affichage->nav();
$affichage->genererArticle();
$affichage->foot();
The variables $contenuArticle->title()and $contenuArticle->description() are supposed to generate TITLE and DESCRIPTION metas, but they send back the error Fatal error: Call to a member function title() on array
Var_dump show that the Array isn't empty and return the right content.
I'm pretty sure it's a common error but I'm lacking experience to point it. Let me know if I can provide more specific intels on my files.
Using var_dump($contenuArticle) shows the return of $model_Article->StockageDonneesById($id) is an array, with 1 element.
changing line 6 to:
$affichage = new View_Article($contenuArticle[0]->title(),$contenuArticle[0]->description());
Solved the issue
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have a line of code that simply calls a function with a number of parameters (actually 14 of them...).
It doesn't access an array value (i.e. doesn't do array[i]).
list($fs_response, $flume_response, $create_queues_response, $hdfs_response, $impala_msg) = performAllUpdates($fs_backup, $new_item, $flume, $flume_backup, $logger, $hdfs_backup, $hdfs_local_copy, $properties, $env, $hdfs_controller, $python, $hdfa_response, $impala_msg, $create_queues_response);
However I'm getting a Notice: Undefined offset: 4 on this line for some reason.
Any idea?
In order for you to successfully extract an array of 5 elements like you tried via "list", you need to make sure the performAllUpdates function returns an array of 5 elements minimum. For example, the following return statement in the function will work:
return array($response1,$response2,$response3,$response4,$response5);
But of course $response1 through $response5 need to be replaced with actual values or variables used in the function.
I also recommend modifying the function to return the array always, and if the function is meant to produce an error based on invalid input, then put in invalid values for the array. For example, you can use this return statement to show an error:
return array(-1,-1,-1,-1,-1);
That way, your attempt to receive 5 elements will always be successful and then you can test the results by checking the values of any of the 5 variables you asked for from the function.