When I try to save an item in Laravel it says the following.
Call to undefined method Illuminate\Database\Query\Builder::save()
I have followed the documentation, and i can delete items fine just not update. Is there anything wrong with my code?
public function publishedMain($id, $state){
$userId = Auth::user()->id;
$userAdmin = Auth::user()->admin;
if($userAdmin == "0"){
$clients = Clients::whereRaw('id = ? and parent = ?', array($id, $state));
$clients->active = $state;
$clients->save();
die('not admin');
}else{
$clients = Clients::whereRaw('id = ?', array($id));
$clients->active = $state;
$clients->save();
die('admin');
}
}
Many Thanks
Brent
You call the save() method on the query builder, not on your model.
You're missing the ->first() piece:
$clients = Clients::whereRaw('id = ? and parent = ?', array($id, $state))->first();
Of course, this will get the first client, so naming the variable $clients makes not much sense.
You shall also check if your $clients variable is not null, in which case it didn't find anything.
(btw, your model should be called Client, not Clients)
Related
I have a relation hasMany but it doesn't work at my controller
code season model :
public function retro () {
return $this->hasMany('App\Models\Retrocession','season_id');
}
code controller :
$hotelagencie = HotelAgency::find($id) ;
$hotel = $hotelagencie->hotel;
$season = $hotel->seasons;
return $season->retro
Ereur : Property [retro] does not exist on this collection instance.
Thxyou .
We confirmed what you want(Select all the retro from all the seasons), so I think I can give you an answer:
This is what you got:
$hotelagencie = HotelAgency::find($id) ;
$hotel = $hotelagencie->hotel;
$season = $hotel->seasons;
return $season->retro
This is what I think it needs to be:
$hotelagencie = HotelAgency::find($id) ;
$hotel = $hotelagencie->hotel;
$seasons = $hotel->seasons;
return $seasons->pluck('retro');
For more info on what pluck() is and how to use it, please refer to the official docs:
https://laravel.com/docs/6.x/collections#method-pluck
I hope it helps ;)
I'm trying to show a title based on whether a specific column in my table is 1 or 0. In my Controller I have (edited out some irrelevant code):
public function show(Company $id){
$vegan = Company::find($id);
$vegetarian = Company::find($id);
return view('allProducts')->with([
'vegan' => $vegan,
'vegetarian' => $vegetarian,
]);
}
and in my view:
#if($vegan->vegan == 1)
<h3 class="text-center">Vegan</h3>
#endif
However I get error message
ErrorException (E_ERROR)
Property [vegan] does not exist on this collection instance. (View: C:\xampp\htdocs\EdenBeauty\resources\views\allProducts.blade.php)
Ive tried the following but I get errors every time:
#if($vegan[0]->vegan == 1)
This gives undefined offset errors
The problem is you're missing first() after your queries:
$vegan = Company::find($id)->first();
$vegetarian = Company::find($id)->first();
In this line, you're injecting a Company into your show method via URL parameter:
public function show(Company $id){ ... }
At that point, $id is either a Company instance or null. Calling $vegan = Company::find($id) doesn't make any sense, and I'm actually surprised you don't get an error at that point in the code.
Also, if you're using injection, name the variable correctly Company $company to avoid confusion, and reference later:
public function show(Company $company){
$vegan = $company;
$vegetarian = $company;
// Or `$vegan = Company::find($company->id);`
// (This is redundant, but demonstrates the syntax)
return view("...")->with(...);
}
Alternatively, remove injection and query:
public function show($id){
$vegan = Company::find($id); // Note can use use `firstOrFail()`, etc.
$vegetarian = Company::find($id);
...
}
Either way, find() does not return a Collection, so $vegan->vegan would not return "Property [vegan] does not exist on this collection instance.", but something about your usage is treating it that way.
I'm having problems with this Query. I want to obtain only the cars with the atribute $categoria and I do this:
public function listcategoriaAction($categoria)
{
$posts = $this->get('doctrine')->getManager()->createQueryBuilder()->select('p')->from('CarsCarsBundle:Post', 'p')->where('p.categoria = :categoria')->setParameter('categoria', $categoria)->getQuery()->getResult();
return $this->render('CarsCarsBundle:Cars:list.html.twig', array('posts' => $posts));
}
But what I recieve is an empty array. Any tips will be appreciated
First of all, I assume that this code is in the controller. I strongly recommend you to avoid putting queries on your controller, instead use repositories.
I think this error happened because you didn't hydrate previously the category id you received by parameter. This would do the trick:
$dm = $this->get('doctrine')->getManager();
//This gets the object from db
$category = $dm->getRepository('CarsCarsBundle:Category')->findOneById($categoria);
if ($category !== null) {
$posts = $dm->getRepository('CarsCarsBundle:Post')->findOneByCategory($category);
} else {
//The id received is not on the db.
}
I have a list of properties for a real estate application and im trying to implement a like/unlike functionality based on each property detail. The idea is to add a like or remove it matching the current property and user. This is my code so far, but it only remove likes so it doesnt work as expected. If anyone can suggest for a better approach ill be appreciated.
//Controller
public function storeLike($id)
{
$like = Like::firstOrNew(array('property_id' => $id));
$user = Auth::id();
try{
$liked = Like::get_like_user($id);
}catch(Exception $ex){
$liked = null;
}
if($liked){
$liked->total_likes -= 1;
$liked->status = false;
$liked->save();
}else{
$like->user_id = $user;
$like->total_likes += 1;
$like->status = true;
$like->save();
}
return Redirect::to('/detalle/propiedad/' . $id);
}
// Model
public static function get_like_user($id)
{
return static::with('property', 'user')->where('property_id', $id)
->where('user_id', Auth::id())->first();
}
// Route
Route::get('store/like/{id}', array('as' => 'store.like', 'uses' => 'LikeController#storeLike'));
#Andrés Da Viá Looks like you are returning object from model. In case there is no data in database, it will still return an object - so far my guessing. Can you do something like below in the if($liked){ code?
Try this instead:
if(isset($liked -> user_id)){
Also try to print $liked variable after try and catch blocks. Use var_dump.
If this still does not work for you then let me know. I will try to create code based on your question.
Fix it by adding a where clause in my model to make the status equal to True ->where('status', 1)->first();
I have 2 tables: listings and listings_specifications
Listings table
id
type
status
location
specifications_id
Listings_specifications table
id
listing_id
swimming_pool
water_well
I need to select the specifications (checkboxes) on the same form with which I add a listing. I have created all the forms, views, models, controllers but I think I got some logic wrong.
Listing.php model
protected $table = 'listings';
public function contact()
{
return $this->BelongsTo('contacts');
}
public function specifications()
{
return $this->BelongsTo('listings_specifications');
}
Specification.php model
protected $table = 'listings_specifications';
public function listings()
{
return $this->BelongsTo('listings');
}
ListingsController.php (where the data gets saved in the database)
$listing = new Listing;
$contact = new Contact;
$listing->status = Input::get('status');
$listing->listingfor = Input::get('listingfor');
$listing->propertystatus = Input::get('propertystatus');
$listing->propertytype = Input::get('propertytype');
$listing->userid = Auth::user()->id;
$listing->location = Input::get('location');
$listing->contact_id = $contact_id;
$listing->save();
$specifications = Specification::find($id);
if( $listings->save() ) {
$specifications = new Specification;
$specifications->listing_id = $id;
$specifications->swimming_pool = Input::get('swimming_pool');
$specifications->water_front = Input::get('water_front');
$specifications->save();
}
I'm getting this error: Undefined variable: id
Where did I go wrong?
Thank you
It looks like you have some logic errors.
First of all, you are never setting $id anywhere, but that's okay because you really don't need it.
Remove the $specifications = Specification::find($id); line because that's not doing anything.
Then change your last section to something like this...
if( $listings->save() ) {
$specifications = new Specification;
$specifications->swimming_pool = Input::get('swimming_pool');
$specifications->water_front = Input::get('water_front');
$listing->specifications()->save($specifications);
}
$listing->specifications()->save($specifications); will automatically save the new specification with the correct listing_id for you.
Modify your Listing model's specifications relationship to...
public function specifications()
{
return $this->hasMany('Specification');
}
I'm assuming here one listing can have many specifications. If not, you can easily just change that to a hasOne.
You use $id in the line $specifications = Specification::find($id); but you don't define it before.