Update integer and date column to null in Laravel [closed] - php

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]

Related

Laravel Eloquent returns Array only on first record in database (`where` method) [closed]

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.

calling member function where() on null error [closed]

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

Receiving Fatal error: Call to a member function title() on array [closed]

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

mysql: how to select with string containing "-" [closed]

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 5 years ago.
Improve this question
Could someone please point me where did I go wrong?
I've an apps using PHP and mysql. The thing is, we are using some file uploader plugins that require us to save the unique id that has format something like this a41ddc78-3fee-4bf3-88c1-83028ae22f1. At some point, I need to select data from the database using this fields and as you can see, I can't get the field when I do manual query on it. any help is appreciate on how to solve this problem. Thank you
SELECT * FROM product_image WHERE unique_id = 'a41ddc78-3fee-4bf3-88c1-83028ae22f1'
the query works fine...

Insert a single value into database table which has multiple columns [closed]

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 years ago.
Improve this question
I couldn't find an anwser on google, so here's my question.
Let's say, I have a table which has name,lastName,Age columns.
What I want to do, is insert only "name" into the table.
I have a function
function c_mysqlInsert($ptable,$pvaluename,$pvalue,$database=""){
global $con_dbaccounts;
$dbname = ($database=="")?$con_dbaccounts:$database;
$con = c_dbconnect($dbname);
mysql_query($con,"INSERT INTO $ptable ($pvaluename) VALUES('$pvalue')");
}
/**
which doesn't seem to insert any new things into the $pdbtable, even when $ptable, $pvaluename and $database are all correct. It does connect to the database $database.
I thought it would create a new row with $pvalue and the other columns empty (or NULL), but it doesn't.
So I guess I can't do it this way - call c_mysqlInsert function multiple times to insert into all three columns, so is there any other way I could make this function work?
Your insert statement has the $pdbtable variable but the variable passed into your function is $ptable. Change the variable in your insert and you should be golden.

Categories