how to filter a table using row value=NULL in laravel controller - mysql

public function complaint_pending(){
$complaints = Complaint::whereNull('answear')->get();
return view('admin.complaint.panding',compact('complaints'));
}
I want to show Complaint all data which answer row is NULL
My DB pic is below
enter image description here

you have type error in function.
change answear to answer in complaint_pending method.

try the following code
public function complaint_pending(){
$complaints = Complaint::where('answear',NULL)->get();
return view('admin.complaint.panding',compact('complaints'));
}

That's not work but it's work perfectly ...
public function complaint_pending(){
$complaints = Complaint::where('answear', NULL)->get();
return view('admin.complaint.panding',compact('complaints'));
}

Related

Symfony 4 - update JSON user roles

I have entity User with field roles:
/**
* #ORM\Column(name="roles", type="json")
*/
private $roles = [];
public function getRoles(): array
{
return $roles = $this->roles;
}
public function setRoles($roles): self
{
$this->roles[] = $roles;
return $this;
}
I want to add functionality to update user role from ROLE_ADMIN to ROLE_USER. I tried this in my controller but instead of replacing ROLE_ADMIN with ROLE_USER it inerts this: "ROLE_ADMIN""ROLE_USER". This is my controller:
public function updateuser(Request $request, $id) {
$entityManager = $this->getDoctrine()->getManager();
$usr = $this->getDoctrine()->getRepository(User::class)->find($id);
$usr->setRoles("ROLE_USER");
$entityManager->persist($usr);
$entityManager->flush();
First of all its best practice that every users has at least a default role like ROLE_USER. If you want to give users extra roles then you add them with beside ROLE_USER, like for example ROLE_ADMIN.
Now take a close look how arrays work in PHP. Let's take the code of you setter function setRoles.
When you write the value assignment like this $this->roles[] = $roles, then a value is added to the array . Thats why you in you code you have both roles inside you array now. The already existing ROLE_ADMIN and after the function call you added ROLE_USER.
When you write the value assignment like this $this->roles = $roles, then the whole array is overwritten with the new value.
Conclusion:
Thats how you code should look like if you want a simple solution:
public function setRoles(array $roles): self
{
$this->roles = $roles;
return $this;
}
Then you can call it like this:
$user->setRoles(['ROLE_USER']);
The setRoles function only accepts array.
So your code should change accordingly:
$usr->setRoles(["ROLE_USER"]);
Furthermore, if you want to store it as json, you can use json_encode:
$usr->setRoles(json_encode(["ROLE_USER"]));

Method Illuminate\Database\Eloquent\Collection::detach does not exist. (how can I solve?)

I need to delete and detach all relationship between BrandName table and stellar_category table.
public function destroy($id)
{
$brand_name = BrandName::find($id);
$brand_name->stellar_category->detach();
$brand_name->delete();
return back()->withMessage('Your Brand is Deleted');`enter code here`
}
Try to change it to
$brand_name->stellar_category()->detach();

How do I return a union as an Active Record object?

I have these union in my controller :
$expression = new Expression('"News"');
$featuredNews2= news::find()
->alias('ne')
->select(['ne.title', 'ne.content','ne.featuredOrder', 'category'=>$expression])
->innerJoinWith('featuredOrder1');
$expression2 = new Expression('"Event"');
$featuredEvents2 = event::find()
->select(['ev.title', 'ev.content','ev.featuredOrder','category'=>$expression2])
->from('event ev')
->innerJoinWith('featuredOrder2');
$union = $featuredNews2->union($featuredEvents2);
The relation in model :
news model
public function getFeaturedOrder1()
{
return $this->hasOne(Featured::className(), ['featuredOrder' => 'featuredOrder']);
}
event model
public function getFeaturedOrder2()
{
return $this->hasOne(Featured::className(), ['featuredOrder' => 'featuredOrder']);
}
I need to return the query as an Active Query because I need to access my model's method e.g : $model->featuredOrder1->preview in my view.
The following works but it returns an array, as the result I can't access my model's method :
$unionQuery = (new \yii\db\Query)->select('*')
->from($union)
->orderBy('featuredOrder')->all(\Yii::$app->db2);
I have two questions :
How to return the equivalent $unionQuery above but as an active query object? I have googled and search on SO but what I found is how to return it as array.
This is out of curiosity, I wonder why I should provide my db connection as argument in my $unionQuery all() method above. If I didn't use an argument that point to db2, it will look for table name inside my db database instead ( db is my parent database, this db2 is my module's database/the correct one). This only happen with a union. My news and event model already have this in getdb() function:
return Yii::$app->get('db2');
update
I've tried this too :
$unionProvider = (new ActiveQuery(Featured::className()))->select('*')
->from(['union' => $featuredEvents2->union($featuredNews2)])
->orderBy('featuredOrder');
With this relation in featured model:
public function getNews()
{
return $this->hasOne(News::className(), ['featuredOrder' => 'featuredOrder']);
}
public function getEvents()
{
return $this->hasOne(Event::className(), ['featuredOrder' => 'featuredOrder']);
}
and in the view, I tried this :
foreach($unionProvider as $key=>$model){
echo $model->news->title;
}
but get this error : Trying to get property of non-object
Update 2
I forgot to add ->all() in my $unionProvider, but after that I got this error instead : PHP Fatal Error – yii\base\ErrorException
Allowed memory size of 134217728 bytes exhausted (tried to allocate 12288 bytes)
Might be something wrong with my query? Can't figure it out
You can try using pure SQL. Plus you can test to see if it is returning the correct results and then add it in the statement below.
$customers = Customer::findBySql('SELECT * FROM customer')->all();
Learn more in yii docs

Retrieve and edit data on page using Laravel

I cant display my data, laravel's 404 page popping.
Here is my foreach codes.
#foreach($este as $row)
{{$row['価格']}}
{{$row['間取り']}}
{{$row['販売戸数']}}
{{$row['総戸数']}}
{{$row['専有面積']}}
{{$row['専有面積']}}
{{$row['その他面積']}}
{{$row['所在階/構造 階建']}}
{{$row['完成時期']}}
{{$row['住所']}}
#endforeach
What am I doing wrong here.
I want to edit table with css but data doesn't display.
My controller, and route is here:
public function sumos($id)
{
$este = estates::all();
return view('pages.sumo', ['este' => $este]);
}
Route::get("sumo/{id}", "PagesController#sumos");
Change your controller and route to the following
public function sumos()
{
$data['este'] = estates::all();
return view('pages.sumo', $data);
}
Route::get("sumo", "PagesController#sumos");
then your view to
#foreach($este as $row)
{{$row->価格}}
{{$row->間取り}}
{{$row->販売戸数}}
{{$row->総戸数}}
{{$row->専有面積}}
{{$row->専有面積}}
{{$row->その他面積}}
{{$row->所在階/構造 階建}}
{{$row->完成時期}}
{{$row->住所}}
#endforeach
Don't return the value from the controller . Instead send the values to the view as parameter and then use it in your view
Like
public function sumos()
{
$este = estates::get();
return view('viewName', ['este' => $este]);
}
And in your view
#foreach($este as $row)
write markup for your items here.
#endforeach
1.Actually get is for retrieve all the data from table ,
2.After getting related data pass it on view
public function sumos()
{
$este = estates::get();
return view("editPage" , compact('este'));
}
After View data in table using
#foreach($este as $est) //data here like html css #endforeach
add route to update data and pass it into controller
Then find related row using `
public function find($id){
$estates = estates::find($id);
//after changes
$estates->save();
return redirect()->back();
}
`
firs you have to create a view file
How to use views in Laravel?
then just use this construction:
public function sumos()
{
$este = estates::all();
return view('name_of_your_view_file', compact('este');
}
change your route to
Route::get("sumo/{id}", "PagesController#sumos");

Can I update 1 object only with Linq to SQL?

Its a simple question, but I'm not aware of the answer and I couldn't get it to work.
Can I update only one entity on the entire DataContext? Or should I follow plain ADO.NET for this operation only?
Edit:
public MyObject GetMyObjectById(int selectedId)
{
DataContext db = _dbManager.GetContext();
return db.MyObject.SingleOrDefault(p => p.Id == selectedId);
}
I am getting an object with the above query...
I am querying then for an integer...on another table/object
public int GetMyInteger()
{
DataContext db = _dbManager.GetContext();
return db.MyAnotherObject.FirstOrDefault().MyInteger;
}
Everything is fine for all my operations...but now i just want to update only the integer i got from the database...
public void SetMyInteger(int updInteger)
{
DataContext db = new DataContext(ConnectionString);
MyAnotherObject theEntity = db.MyAnotherObject.FirstOrDefault();
atheEntity.MyInteger = updInteger;
db.SubmitChanges(ConflictMode.ContinueOnConflict);
}
The above method deleted MyObject i got from the first query!!! Of course if i use the static context DataContext tries to update MyObject and MyAnotherObject which seems the correct behaviour.
Edit:
I have changed the method getting the integer with a new datacontext as well and seems to working correctly, i have a strange thought on why called the delete method, because it was the method that was called, but again .. is working now...
Thank you all for your time.
Yes it's possible. What have you tried? It should be as simple as this:
using (var dc = new YourDataContext())
{
Person p = dc.Persons.Take(1).Single();
p.FirstName = "Ahmad";
dc.SubmitChanges();
}
Yes, you can:
Foo foo = dc.Foos.Where(foo => foo.Id == 345).Single();
foo.Name = "foo";
dc.SubmitChanges();