How do i delete one row in my SQLite table? - php

I'm trying to delete one row of data from my sql table called "cart".
It seems to me that it can not find the id called "cartid". im using a program called insomnia and it wont take my input it feels like.
it is gettting a error that is called :"MethodNotAllowedHttpException"
im using laravel 5.4, it is a requirement for me.
i have other functions that work and i do not understand why this one does not.
https://imgur.com/a/T0aOwJF
some snapshots from my code
i have tried to use
Route::delete('cart-remove/{id}', 'CartController#delete');
and alot more
this is in my 'api.php' file:
Route::delete('cart-remove', 'CartController#delete');
this is in my file called 'CartController':
class CartController extends Controller {
public function delete($id) {
DB::table('cart')->where('cartid', '=', $id)->delete();
return response($id, 200);
}
}
i want to get the id from my program called 'insomnia' to remove that id in my sqlite database.

Use the model's destroy method when deleting entries from the database:
use App\SomeModel;
class SomeController extends Controller {
public function destroy($id) //or delete($id)
{
SomeModel::destroy($id);
return response($id, 200);
}
}

Related

Call a Model method using Laravel Relationship

I'm currently trying to use Laravel Relationships to access my achievements Model using User model, I use this relationship code:
public function achievements()
{
return $this->hasMany('App\Models\User\Achievement');
}
I can easily make some eloquent queries, however I can't access any method that I created there, I can't access this method:
class Achievement extends Model
{
public function achievementsAvailableToClaim(): int
{
// Not an eloquent query
}
}
Using the following code:
Auth::user()->achievements()->achievementsAvailableToClaim();
I believe that I am using this Laravel function in the wrong way, because of that I tried something else without using relationship:
public function achievements()
{
return new \App\Models\User\Achievement;
}
But that would have a performance problem, because would I be creating a new class instance every time I use the achievements function inside user model?
What would be the right way of what I'm trying to do?
it's not working because your eloquent relationship is a hasMany so it return a collection. you can not call the related model function from a collection.
you can var dump it on tinker to understand more what i mean.
You can use laravel scopes.Like local scopes allow you to define common sets of constraints that you may easily re-use throughout your application.
In your case you use this like, Define scope in model:
public function scopeAchievementsAvailableToClaim()
{
return $query->where('achivement_avilable', true);
}
And you can use this like :
Auth::user()->achievements()->achievementsAvailableToClaim();

Why do I have different dump in PHP and JSON?

I use Laravel Eloquent for my project. I get different object results with futher coding.
Php dump (var_dump(), dd(), dump()) object without unnecessary data, file json_encode spawns calculated data including loading something what was not required.
I have two model classes:
class Product extends Model
{
public function reviews()
{
return $this->hasMany(Review::class);
}
}
class Review extends Model
{
public function product()
{
return $this->belongsTo(Product::class);
}
}
To get a product I use
$product = App\Product::find(1)
But since I want to send data to JSON, I need to just load it by typing:
$product->reviews
But I don't want to load it, I just want to get additional calculated data without loading all reviews inside my json
$product['reviews_statistics'] = count($product->reviews);
I expect to find out how to set data for JSON without loading all $product->reviews
I would recomend You to try eagerloading the count:
$product = App\Product::withCount('reviews')->find(1)

Laravel model event won't fire

I'm using ajax to update my model User, the ajax part works fine since the data is updated successfully in the database, inside my controller action the update performed by :
$user->update($data);
The part that doesn't work:
I've used boots method updated inside my model like :
class User extends BaseModel
{
...
public static function boot()
{
parent::boot();
self::updated(function($model){
Log::info("updated");
dd($model);
});
}
}
The event was never reached I'm not sure why.
Problem:
I'm trying to perform an action after the model update but the event doesn't fire.
Here's what the manual states with update()
When issuing a mass update via Eloquent, the saved and updated model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update.
You need to use save to trigger events. Something like:
$user->fill($data);
$user->save();
This of course is assuming that $user is a model and not a query builder instance.
You are accessing the function statically:
instead try using
self::updated(function($model){
Log::info("updated");
dd($model);
});

Load user settings with Laravel 5.6

I'd like to be able to modify the Auth->user() data that Laravel 5.6 uses. I have table called settings with a column called user_id in it that corresponds to a user id.
I tried modifying app\User.php and adding a __construct function:
public function __construct() {
$this->settings = Settings::where('user_id',
Auth->user->id()
)->first();
}
And I created a file app\Settings.php with the following:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class settings extends Model
{
protected $table = "settings";
}
However I'm getting a user error on the Auth->user()->id line in User.php, although I'm sure thats the correct way to reference it?
How can I load the data from the settings table to the User class?
You can just use load() method to lazy load the relation:
auth()->user()->load('settings');
You need to do this just once per request, in a middleware for example. Then you'll be able to use the data in any part of your app:
{{ auth()->user()->settings->theme }}
Of course, to make this work you need to define relationship in the User model, for example:
public function settings()
{
return $this->hasOne(Settings::class);
}

Laravel 5: Access database data from within model

I'm trying to use Laravel to access some data from the database. I have setup my model, which works fine, however I'm trying to fetch some data from another table by creating a method within the model which fetches the data and sets a property of the model equal to the fetched data.
To fetch the data, I'm trying to use the $this->id property to fetch this row's ID and use it to fetch data from another table (eg: SomeOtherModel::where('other_id', '=', $this->id)->get();)
However, none of the data from the database seems to have been fetched by the model at this point (eg: $this->id is null). My constructor looks like so:
public function __construct() {
parent::__construct();
$this->other_data = $this->getOtherData();
}
Is there any way to call the $this->id property from the database so I can use it within the model itself?
Your syntax seemed to me wrong:
SomeOtherModel::where('other_id', '=', $this->id)->get();
Can you try this:
SomeOtherModel::where('other_id', $this->id)->get();
Reference: https://laravel.com/docs/5.1/eloquent#retrieving-single-models
I hope this helps .
The solution is explained here:
class ExampleModel extends Model {
protected $appends = ['otherdata'];
public function getOtherdataAttribute() {
return $this->getOtherData();
}
}

Categories