Laravel Eloquent filter not found - php

I am trying to create a filter in Laravel.
I want to filter on the database field transmission but it is not working.
Here is my Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Occasion extends Model
{
protected $table = 'hexon_occasions';
public $primaryKey = 'id';
public $timestamps = true;
}
My Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use EloquentBuilder;
use App\Occasion;
class OccasionController extends Controller
{
public function index(Request $request)
{
$occasions = EloquentBuilder::to(
Occasion::class,
$request->all()
);
return $occasions->get();
}
public function show($slug)
{
$occasions = Occasion::where('slug', $slug)->first();
return view('occasions.show')->with('occasions', $occasions);
}
}
The filter:
namespace App\EloquentFilters\Occasion;
use Fouladgar\EloquentBuilder\Support\Foundation\Contracts\Filter;
use Illuminate\Database\Eloquent\Builder;
class TransmissionFilter implements Filter
{
/**
* Apply the age condition to the query.
*
* #param Builder $builder
* #param mixed $value
*
* #return Builder
*/
public function apply(Builder $builder, $value): Builder
{
return $builder->where('transmission', $value);
}
}
And the URL i am using is: https://www.laravel.bvdodev.nl/occasions?filter[transmission]=H
But I am getting the error: Not found the filter: FilterFilter
Does someone know what I am doing wrong?
Thanks for your time!

Related

delete with eager load in laravel

I'm trying to delete a collection but I want to delete the NFTs related to a collection as well. How do I do this?
‍‍This is my Nft model Nft.php
class Nft extends Model
{
use HasFactory,SoftDeletes;
public function collection()
{
return $this->belongsTo(Collection::class);
}
}
This is my collection model Collection.php
class Collection extends Model
{
use HasFactory, SoftDeletes;
public function nfts()
{
return $this->hasMany(Nft::class);
}
}
This is my COntroller CollectionController.php
public function deleteCollection(int $id):RedirectResponse
{
Collection::with('nfts')->find($id)->delete();
return redirect('/collections');
}
But it did not work!!!
pleas help me!!!!!
At user model:
public function collection() {
return $this->belongsTo(Collection::class)->withTrashed(); }
Keep in mind that the observer events are not fired if you use mass delete for example. In the case Collection::with('nfts')->delete(); it might not work.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Collection extends Model
{
/**
* The "booted" method of the model.
*
* #return void
*/
protected static function booted()
{
static::deleting(function ($collection) {
$collection->nfts()->delete()
});
}
}

where is the attribute in the laravel code comes from?

<?php
namespace App\Models;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* #var array
*/
protected $hidden = ['password', 'remember_token'];
public static function boot()
{
parent::boot();
static::creating(function ($user) {
$user->activation_token = str_random(30);
});
}
public function gravatar($size = '100')
{
$hash = md5(strtolower(trim($this->attributes['email'])));
return "http://www.gravatar.com/avatar/$hash?s=$size";
}
public function statuses()
{
return $this->hasMany(Status::class);
}
public function feed()
{
return $this->statuses()->orderBy('created_at', 'desc');
}
public function followers()
{
return $this->belongsToMany(User::Class, 'followers', 'user_id', 'follower_id');
}
public function followings()
{
return $this->belongsToMany(User::Class, 'followers', 'follower_id', 'user_id');
}
public function follow($user_ids)
{
if (!is_array($user_ids)) {
$user_ids = compact('user_ids');
}
$this->followings()->sync($user_ids, false);
}
public function unfollow($user_ids)
{
if (!is_array($user_ids)) {
$user_ids = compact('user_ids');
}
$this->followings()->detach($user_ids);
}
public function isFollowing($user_id)
{
var_dump($this->followings);die();
return $this->followings->contains($user_id);
}
}
This is a code come from laravel models.
There is a method named $this->followings() .But I don't see any $this->followings attribute assigned in the code.
where is the $this->followings comes from?
thanks
Suggested reading about Laravel model relationships
in particular:
Once the relationship is defined, we may retrieve the related record using Eloquent's dynamic properties. Dynamic properties allow you to access relationship methods as if they were properties defined on the model

Trying to get the username using MODELS but getting this error: Trying to get property of non-object

ERROR: ErrorException in 814a6fb85b2cceb262c3a8191c08e42742940fc7.php line 223: Trying to get property of non-object (View: /var/www/html/m/TS/resources/views/d/show-details.blade.php)
Actually I am trying to get the username who has stored the result in the database i.e. TN has got user_id as a foreign key in the table so I need to get the username from that user_id using models but I am getting this problemTrying to get property of non-objectwhen I try to get theusername` associated with the id. I dont know where I am doing it wrong.
THE ERROR I AM GETTING IS HERE value="{{$tn->users->username}}" WHICH SHOWS IN THE CACHED FILE.
I have given my code below too to look.
Thank you in advance
Controller
public function details($id) {
$d= $this->detail->showDetails($id);
$tn= TN::find($id);
// calling functions from Model
$n = $d->tN;
$o = $d->tO;
return view('details.show-details', compact('d','o', 'n', 'tn'));
}
View
foreach($n as $n)
<input style="font-size:10px;font-weight: bold; color:black; background:#59d864;" value="{{$tn->users->username}}" readonly>
Models
User Model
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\TN;
use App\TO;
use App\UserType;
class User extends Authenticatable
{
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'username', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function tN() {
return $this->hasMany(TN::class);
}
public function tO() {
return $this->hasMany(TO::class);
}
public function userType() {
return $this->belongsTo(UserType::class);
}
}
TO Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\D;
use App\TT;
use App\User;
class TO extends Model
{
protected $fillable = ['o', 'date'];
public function d() {
return $this->belongsTo(D::class);
}
public function tOType() {
return $this->belongsTo(TOType::class);
}
public function users() {
return $this->belongsTo(User::class);
}
}
TN Model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Debtor;
use App\TracingType;
use App\User;
class TN extends Model
{
protected $fillable = ['r', 'date'];
public function d() {
return $this->belongsTo(D::class);
}
public function tType() {
return $this->belongsTo(TType::class);
}
public function users() {
return $this->belongsTo(User::class);
}
}
Hello I have sorted this problem but forgot to post it in here the problem was here
public function users() {
return $this->belongsTo(User::class);
}
This should be public function user() rather than users. Because it belongsTo to User class, it should be singular not plural and for hasMany we use plurals. Thank you for your help though ... :)

how to join two models in laravel 5.2

I have two models as Project and Collaborator this is My tables
Project column
id
project_name
project_note
user_id
Collaborator Model
id
project_id
collaborator_id
I need join this models to get project_name instead project_id of Collaborator Model.
how can I do this. I read www.laravel.com documents but difficult to understand. help me in code...
project Model
<?php
namespace App;
use Auth;
use Illuminate\Database\Eloquent\Model;
class Project extends Model
{
/*
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['project_name', 'project_notes', 'project_status', 'due_date'];
public function scopePersonal($query)
{
return $query->where('user_id', Auth::user()->id);
}
//
}
collaborator Model
<?php
namespace App;
use Auth;
use Illuminate\Database\Eloquent\Model;
class Collaboration extends Model
{
protected $table = 'project_collaborator';
/*
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['project_id', 'collaborator_id'];
/*
* Get the user that is a collaborator on another project
* #return collection
*/
public function user()
{
return $this->belongsTo(User::class, 'collaborator_id');
}
/*
* Query scope to return information about the current project
* #param $query
* #param int $id
* #return query
*/
public function scopeProject($query, $id)
{
return $query->where('project_id', $id);
}
public function scopeColabo($query)
{
return $query->where('collaborator_id',Auth::user()->id);
}
}
Based on your comment, you need to add a relationship hasOne to your collaboration model:
class Collaboration extends Model
{
.....
public function project()
{
return $this->hasOne('App\Project');
}
.....
}
The method project() will define your relationship with your project model. And then you'll be able to get the collaboration project name like this:
$collaborations = Collaboration::with('project')->get();
foreach ( $collaborations as $collaboration ) {
echo $collaboration->project->project_name;
}
You can read more in the documentation.
In your Collaboration class add the followoing relation:
public function project()
{
return $this->belongsTo('App\Project');
}
And in your User class define a relation as:
public function collaborations()
{
return $this->hasMany('App\Collaboration', 'collaborator_id');
}
Then you can get all the collaborations of logged in user by:
$collaborations = auth()->user()->collaborations()->with('project')->get()
or
To get all collaborations you can so as:
$collaborations = Collaboration::with('project')->get();
In your view file:
#foreach ($collaborations as $collaboration)
{{ $collaboration->project->project_name }}
#endforeach

FatalError in Laravel5

I generate one simple insert,update and delete application. when I run my application, I get error. my application files are below....
Route.php
Route::post('insertdata','ContactusController#store');
ContactusController.php
use App\ContactusModel;
use Illuminate\Support\Facades\Input;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
/* namespaces */
use App\User;
use Symfony\Component\HttpKernel\Client;
use Illuminate\Support\Facades\Redirect;
class ContactusController extends Controller {
public function __construct()
{
}
public function index()
{
return view('contact.contact');
}
public function store()
{
$input = Input::all();
ContactusModel::insertall($input);
return view('contact.contact');
}
}
ContactusModel.php
<?php namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class ContactusModel extends Model {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'contactus_models';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['fullname','mobileno', 'email', 'message'];
public static insertall($data)
{
return DB::table('users')->insert($data);
}
}
I run this code. Error will be display like this....
FatalErrorException in ContactusModel.php line 24:
syntax error, unexpected 'insertall' (T_STRING), expecting variable (T_VARIABLE)
public static insertall($data)
{
return DB::table('users')->insert($data);
}
You haven't added the word function
public static function
late night programming :P ?

Categories