PHP function stripos() expects parameter 1 to be string, object given - laravel

I'm running through Laravel 6.0's addSelect() method as the official documentation described. However, I'm getting an error as my title suggested.
Two corresponding tables are:
CREATE TABLE `destinations` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(90) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1
CREATE TABLE `flights` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(90) NOT NULL,
`destination_id` int(11) NOT NULL,
`arrived_at` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1
The corresponding controller:
use App\Flight;
use App\Destination;
class TestController extends Controller
{
public function Index()
{
return Destination::addSelect(['last_flight' =>
Flight::select('name')
->whereColumn('destination_id', 'destinations.id')
->orderBy('arrived_at', 'desc')
->limit(1)
])->get();
}
}
What is the object provided as a parameter, which is responsible for the error?

Related

Cakephp 3.9 Multiple Associations with the Same Table

I am working on Cakephp 3.9 and I have 3 tables in my application.
CREATE TABLE `leads` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
`created_dt` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
CREATE TABLE `clients` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`lead_id` int(10) NOT NULL,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
CREATE TABLE `contacts` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`lead_id` int(10) NOT NULL,
`first_name` varchar(100) NOT NULL,
`last_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1
There is a one-to-one relation between the Leads, Clients, and Contacts tables.
I also have a Users table:
CREATE TABLE `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
... and a leads_users table
CREATE TABLE `leads_users` (
`lead_id` int(10) NOT NULL,
`user_id` int(10) NOT NULL,
`user_type` enum('Opener','Closer') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin
In my application, the users can be associated with the leads as Openers and Closers. There can be multiple openers and multiple closers for a lead. I have defined my models as below:
class LeadsTable extends Table
{
public function initialize(array $config)
{
$this->hasOne('Contacts');
$this->hasOne('Clients');
$this->belongsToMany('Users', [
'className' => 'Users'
])
->setConditions(['LeadsUsers.user_type' => 'Opener'])
->setProperty('openers');
$this->belongsToMany('Users', [
'className' => 'Users'
])
->setConditions(['LeadsUsers.user_type' => 'Closer'])
->setProperty('closers');
}
}
class ClientsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Leads');
}
}
class ContactsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Leads');
}
}
class UsersTable extends Table
{
public function initialize(array $config)
{
$this->belongsToMany('Leads');
}
}
Now in the Leads controller, I am trying to find all the leads, their contacts, clients, openers, and closers by using the below line of code.
$result = $this->Leads->find('all')->contain(['Contacts', 'Clients', 'Users']);
With this, I am getting the closers but not the openers. In the LeadsTable, if I comment out the closers association then I am getting the openers. So looks like the second association is overwriting the first one.
If I change the LeadsTable association to below:
class LeadsTable extends Table
{
public function initialize(array $config)
{
$this->hasOne('Contacts');
$this->hasOne('Clients');
$this->belongsToMany('Openers', [
'className' => 'Users'
])
->setConditions(['LeadsUsers.user_type' => 'Opener'])
->setProperty('openers');
$this->belongsToMany('Closers', [
'className' => 'Users'
])
->setConditions(['LeadsUsers.user_type' => 'Closer'])
->setProperty('closers');
}
}
I am getting the below error:
The Users association is not defined on Leads.
Please let me know what am I doing wrong.
Thank you in advance.
You're getting this error because you have to change your controller query as well.
$result = $this->Leads->find('all')->contain(['Contacts', 'Clients', 'Closers','Openers']);
So I ended up changing my database a bit and that fixed it.
CREATE TABLE `leads_openers` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`lead_id` int(10) NOT NULL,
`opener_id` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
CREATE TABLE `leads_closers` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`lead_id` int(10) NOT NULL,
`closer_id` int(10) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
and then I changed my models like below:
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class OpenersTable extends Table
{
public function initialize(array $config)
{
$this->setTable('users');
$this->setPrimaryKey('id');
$this->belongsToMany('Leads', [
'joinTable' => 'leads_openers',
]);
}
}
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class ClosersTable extends Table
{
public function initialize(array $config)
{
$this->setTable('users');
$this->setPrimaryKey('id');
$this->belongsToMany('Leads', [
'joinTable' => 'leads_closers',
]);
}
}
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class LeadsTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
$this->belongsToMany('Openers', [
'joinTable' => 'leads_openers',
])
->setProperty('openers');
$this->belongsToMany('Closers', [
'joinTable' => 'leads_closers',
])
->setProperty('closers');
}
}
and then in the controller, I am doing this:
$leads = $this->Leads->find('all')
->where(['Leads.status IN' => $search_condition_lead])
->contain(['Contacts', 'Openers', 'Closers']);

Laravel 5.4 Model Relationship

I have created three tables users, courses and user_courses as shown below
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`address` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`status` enum('0','1') COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
CREATE TABLE `courses` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`title` text,
`description` text,
`price` varchar(255) DEFAULT NULL,
`schedule` varchar(255) DEFAULT NULL,
`duration` varchar(255) DEFAULT NULL,
`summary` text,
`skills` text,
`mode` enum('0','1') DEFAULT NULL COMMENT '0-Online 1 -Instructor',
`random_token` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
CREATE TABLE `user_courses` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) DEFAULT NULL,
`course_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=latin1
Now with these tables, I want to bind relationship such as when I fetch Users I'm able to get courses for a user and when I get to courses I want users associated with the course.
Please help how I can achieve it.
I have got my answer so posting it here if it can help anyone.
The main thing here is to assign a many-to-many relationship. In my user model I have defined
public function courses()
{
return $this->belongsToMany('App\Course');
}
In course model
public function users()
{
return $this->belongsToMany('App\User');
}
Actually, it depends on how you want to use the relationship.In some parts of the code you will need $user->courses or more likely to query $course->users or both.
Now here user_course table will be assumed as a pivot table. So in model, you can write it as
public function courses()
{
return $this->belongsToMany('App\Course', 'user_courses');
}
Here you can also specify the actual names of the fields of that particular pivot table i.e user_courses table.Then, what all we have to do is just add two more parameters first is the current model field and then add the field of the model being joined like
public function courses()
{
return $this->belongsToMany('App\Course', 'user_courses','user_id', 'course_id');
}
So using the above model relationship you can easily fetch users with all the respective courses by
User::->with('courses')->get();
First Fix
First, fix the user_course table structure the users table has id as integer while it's being referenced in user_course in user_id as bigint.
Solution
The first thing to do is to create models in the application. Then create relationships within models and finally use those relationships.
Create models
By using command line
php artisan make:model Course
php artisan make:model UserCourse
You can create them manually if you like. By default, they will be created in app folder with namespace App. For example, the user model will be App/User and so on.
The user model will already exists as its shipped with laravel default installation.
Create relationships
In user model add the following function
public function courses()
{
$this->belongsToMany(Course::class, 'user_course');
}
You can leave the Course Model empty if you are not planning to make a reverese relation from course to user. The one above defines relation from user to courses
Usage
Say in a controller you can use this as
public function someFunctionInController()
{
$usersWithCourses = \App\User::with('courses')->get()->toArray();
//result of a single user record will look something like this
/**
[
'id' => 1,
'name' => 'some name'
... //other users table columns
//the courses will be an array of array content and will be automatically injected
'courses' => [[
'id' => 1 //the course id
... //course table columns,
'pivot' => [
'user_id' => 1,
'course_id' => 1
]
],[
'id' => 3 //the course id
... //course table columns,
'pivot' => [
'user_id' => 1,
'course_id' => 3
]
]]
]
**/
}

Laravel Models Poperty of non-object

I have a People table
CREATE TABLE `people` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`job_title` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`profile_pic` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`departments` varchar(500) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=95 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
An a departments table
CREATE TABLE `departments` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`people_id` int(11) NOT NULL,
`department_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=50 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
One person can be in many departments
In the departments table the foreign_key is people_id
In my people model
public function departments()
{
return $this->hasMany('App\Models\Departments', 'id', 'people_id');
}
and finally I try and return some results
return $people::find(1)->departments;
I get the error,
**ErrorException in web.php line 129:
Trying to get property of non-object**
Forgive me if this is a stupid error, I have used laravel for a while but never set up models with relationships before.
Try this Piece of code:-
Get All records
$people = People::with('departments')->get();
$people = json_decode(json_encode($people),true) //Convert in to array
echo "<pre>"; print_r($people); die; //print it
Get 1 record
$people = People::with('departments')->first();
$people = json_decode(json_encode($people),true) //Convert in to array
echo "<pre>"; print_r($people); die; //print it
Hope it helps!
So in the end it was a stupid issue.
$people::find(1)->departments;
Find takes an id but the id of 1 did not exist.

How to save INT fields as null in laravel

I'm getting an error when saving some empty text, textarea fields. Laravel forms this sql query:
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'inside_area' at row 1 (SQL: insert into `ads` (`street`, `quarter`, `building_number`, `inside_area`, `price`, `admin_comment`, `valid_to`, `price_heating`, `rooms`, `floor_number`, `floors_number`, `kitchen_area`, `years`, `public_comment`, `video_url`, `3d_url`, `user_id`, `updated_at`, `created_at`) values (, , , , , , , , , , , , , , , , 1, 2017-03-13 14:33:50, 2017-03-13 14:33:50))
P.S. db table was created not in laravel way - I'm using existing tables, this may be important.
UPDATED: problem are only with INT fields, if they has empty form field on saving!
Table:
CREATE TABLE `ads` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`user_id` int(11) DEFAULT NULL,
`ad_type_id` int(11) DEFAULT NULL,
`city_id` int(11) DEFAULT NULL,
`street` varchar(255) DEFAULT NULL,
`quarter` varchar(255) DEFAULT NULL,
`building_number` varchar(255) DEFAULT NULL,
`inside_area` int(11) DEFAULT NULL,
`price` int(11) DEFAULT NULL,
`show_price_per_meter` int(1) DEFAULT NULL,
`price_heating` int(10) DEFAULT NULL,
`admin_comment` text,
`valid_to` datetime DEFAULT NULL,
`rooms` int(11) DEFAULT NULL,
`floor_number` int(11) DEFAULT NULL,
`floors_number` int(11) DEFAULT NULL,
`kitchen_area` int(11) DEFAULT NULL,
`balcony` int(1) DEFAULT NULL,
`balcony_glazed` int(1) DEFAULT NULL,
`years` int(11) DEFAULT NULL,
`dyn_house_type_id` int(11) DEFAULT NULL,
`dyn_heating_id` int(11) DEFAULT NULL,
`dyn_installation_ids` int(11) DEFAULT NULL,
`public_comment` text,
`video_url` varchar(11) DEFAULT NULL,
`3d_url` varchar(11) DEFAULT NULL,
`available_for_trade` int(1) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
FORM:
{!! Form::open(['url'=>'ads']) !!}
{!! Form::number('inside_area', null, ['class'=>'form-control']) !!}
{!! Form::close() !!}
Route:
Route::resource('ads', 'AdsController');
Save action:
public function store() {
$input = Request::all();
\App\Ad::create($input);
return redirect('/ads/my_index');
}
P.S.2 If I provide any value to inside_area field, it goes ok and the next error is for price field.
You can use attribute mutator for each integer attribute. In these mutators you can prepare data before inserting into DB.
In your case you should create mutators in your model Ads for every field with the type INT:
// define a mutator for the field "inside_area"
public function setInsideAreaAttribute($value)
{
$this->attributes['inside_area'] = (int) $value;
}
I understand that creating such a mutator for each INT field is boring, but it will also grant you ability to control user input before inserting into DB.
You can find more information about mutators here (for the latest version of Laravel at this moment):
Laravel 5.4, Defining a Mutator:
https://laravel.com/docs/5.4/eloquent-mutators#defining-a-mutator
Perhaps, you are passing string data to 'inside_area' variable. Because there is more default null fields at your table, like 'city_id', 'ad_type_id'
another way is to use Iatstuti and provide in Ad.php model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Iatstuti\Database\Support\NullableFields;
class Ad extends Model {
use NullableFields;
public $timestamps = true;
protected $nullable = [
'inside_area'
];
protected $fillable = [
'user_id',
'ad_type_id',
'inside_area'
];
}

where_not_in not return match by array value as param

trying to get TraineeID where $arrTID not in tbl_attendance_processed but my code below return all TraineeID from tbl_attendance_processed.
If I use where_in then the query return absolute result those TraineeID are really matched. Just not returning appropriate when I use where_not_in.
Model:
public function getNotMatch_TID($arrTID,$atnDate){
$this->db->select('TraineeID');
$this->db->where_not_in('TraineeID', $arrTID);
$this->db->where('attnDate', $atnDate);
$query = $this->db->get('tbl_attendance_processed');
return $query->result();
}
controller:
$arrTID = $this->input->post('Present');
$atnDate = $this->input->post('attnDate');
$nonMatch= $this->AttendanceModel->getNotMatch_TID($arrTID,$atnDate);
print_r($nonMatch);
echo last query:
table schema:
CREATE TABLE `tbl_attendance_processed` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`TraineeID` varchar(7) CHARACTER SET utf8 NOT NULL,
`attnDate` date NOT NULL,
`inTime` time NOT NULL,
`outTime` time NOT NULL,
`Status` varchar(10) NOT NULL,
`ClassCount` tinyint(1) NOT NULL DEFAULT '1',
`Reason` varchar(255) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=3186 DEFAULT CHARSET=latin1

Resources