How to write forgetpassword manually for laravel - php

I wrote a login system myself and for authentication I us auth and User model.
Now I want to wrote Reset Password,
I found PasswordBroker so I wrote a method in my controller:
public function forgetPassword(Request $request, PasswordBroker $password) {
if (empty($request->session()->get('email'))) {
return redirect()->route('login');
}
$response = $password->sendResetLink([$request->session()->get('email')], function($message)
{
$message->subject('Password Reset');
});
}
But when I trying to reset my password I get this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from `users` where `0` = email#domain.com limit 1)
What should I do?
UPDATE :
my users migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}

I found the solution,
In sendResetLink we should pass email as key in first parameter,
so my code should change to this:
public function forgetPassword(Request $request, PasswordBroker $password) {
if (empty($request->session()->get('email'))) {
return redirect()->route('login');
}
$response = $password->sendResetLink(['email' => $request->session()->get('email')], function($message)
{
$message->subject('Password Reset');
});
}

Related

Integrity constraint violation: 1048 Column 'discussion_id' cannot be null (SQL: insert into `replies`

I am getting this error SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'discussion_id' cannot be null (SQL: insert into replies (content, discussion_id, user_id, updated_at, created_at) values (asdfghjl, ?, 1, 2021-05-09 03:09:05, 2021-05-09 03:09:05)).
Here is my reply model
<?php
namespace LaravelForum;
class Reply extends Model
{
public function users()
{
return $this->belongsTo(User::class,'user_id');
}
public function discussion()
{
return $this->belongsTo(Discussion::class);
}
}
reply table
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRepliesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('replies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->integer('discussion_id');
$table->string('content');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('replies');
}
}
Discussion model
<?php
namespace LaravelForum;
class Discussion extends Model
{
public function author()
{
return $this->belongsTo(User::class,'user_id');
}
public function getRouteKeyName()
{
return 'slug';
}
}
discussion table
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateDiscussionsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('discussions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id');
$table->string('title');
$table->text('content');
$table->string('slug');
$table->integer('channel_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('discussions');
}
}
You have missed Discussion Id While insert into database .... It will be great
if you send image of insert query from controller or check it out there.

Laravel error SQLSTATE[42S22]: Column not found: 1054 Unknown column 'categoria_id' in 'field list'

Im trying to create a many to many relation between 'Categoria' table and 'Noticia' table and then with Factory generate data, but i'm getting that error. I have created a pivot table, but i cant figure out whats wrong, i'm pretty new to this....
The Noticia MODEL:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Noticia extends Model
{
public function categorias(){
return $this->belongsTo(Categoria::class);
}
public function autores(){
return $this->belongsTo(Autor::class);
}
}
The Categoria Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Categoria extends Model
{
public function noticia()
{
return $this->belongsToMany(Noticia::class);
}
}
The Pivot Migration:
class CreateCategoriaNoticiaTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categoria_noticia', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('noticia_id');
$table->foreign('noticia_id')->references('id')->on('noticias');
$table->unsignedBigInteger('categoria_id');
$table->foreign('categoria_id')->references('id')->on('categorias');
$table->timestamps();
});
}
The Noticia Migration
class CreateNoticiasTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('autors', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('nombre');
$table->timestamps();
});
Schema::create('noticias', function (Blueprint $table) {
$table->bigIncrements('id');
$table->date('fecha');
$table->string('titulo');
$table->text('contenido');
$table->string('image');
$table->unsignedBigInteger('autor_id');
$table->foreign('autor_id')->references('id')->on('autors');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('noticias');
Schema::dropIfExists('autors');
Schema::dropIfExists('categorias');
}
}
The Categoria Migration:
lass CreateCategoriasTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categorias', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('nombre');
$table->timestamps();
});
}
The Noticia Factory:
$factory->define(Noticia::class, function (Faker $faker) {
return [
'autor_id' => factory(\App\Autor::class),
'categoria_id'=> factory(\App\Categoria::class),
'titulo' => $faker->sentence(4),
'image' => $faker->randomElement(['img1.jpg', 'img2.jpg', 'img3.jpg', 'img4.jpg']),
'contenido' => $faker->paragraph,
'fecha'=> $faker->date,
];
});
The Categoria Factopry:
$factory->define(Categoria::class, function (Faker $faker) {
return [
'nombre'=> $faker->name
];
});
And the Seeds
Noticia:
public function run()
{
factory(Noticia::class, 100)->create();
}
}
And i'M gettiing this error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'categoria_id' in 'field list' (SQL: insert into categorias (categoria_id, updated_at, created_at) values (Ciencia, 2020-06-11 22:22:38, 2020-06-11 22:22:38))
Thanks in advance!!!
A few things, first as you said, 'Categoria' and 'Noticia' are many-To-many, so both sides should be belongsToMany.
So for your Noticia model:
public function categorias(){
return $this->belongsToMany(Categoria::class);
}
And the error is telling you that you do not have the 'categoria_id' in your Noticia table which is true.
So what you need to do is that.
Remove this line from the migration.
'categoria_id'=> factory(\App\Categoria::class),
And then
factory(App\Noticia::class, 100)->create()->each(function($n) {
$n->categorias()->save(factory(App\Categoria::class)->make());
});
This should work.
The categorias migration doesn't define a field named categoria_id, it's just id.

Trying to get property 'id' of non-object Laravel 7.X

Now I have two Models: user.php and Trx.php. I have created a one to many relationships as below:
User Model:
public function transactions()
{
return $this->hasMany('App\Trx');
}
Trx Model:
public function user()
{
return $this->belongsTo('App\User');
}
And this is the controller method for process data from Trx.php model and sending data to the views:
public function purchasedPlan()
{
$page_title = 'Purchased Plan';
$transactions = Trx::where('type', 7)->orderBy('id', 'DESC')->paginate(config('constants.table.default'));
$empty_message = 'No data found.';
// dd($transactions);
return view('admin.reports.transactions', compact('page_title', 'transactions', 'empty_message'));
}
Now the problem is that when I try accessing the data from the model, that's the $transactions, everything is working well except when I try getting the user in the relationship established like this in the blade template:
transactions.blade.php
<td> {{ $trx->user->username }}</td>
With that, I get this error:
Error:
[2020-05-24 05:35:40] production.ERROR: Trying to get property 'id' of non-object (View: /home/icashers/public_html/acc/core/resources/views/admin/reports/transactions.blade.php) {"exception":"[object] (ErrorException(code: 0): Trying to get property 'id' of non-object (View: /home/icashers/public_html/acc/core/resources/views/admin/reports/transactions.blade.php)
What am I doing wrong please? Anyone who's seeing what I am not please help. Thank you in advance😂
Trx.php Migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTrxesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('trxes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->nullable();
$table->string('amount',50)->nullable();
$table->string('main_amo',50)->nullable();
$table->string('charge',50)->nullable();
$table->string('type',50)->nullable();
$table->string('title')->nullable();
$table->string('trx')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('trxes');
}
}
User.php Migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('firstname');
$table->string('lastname');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('mobile')->unique();
$table->decimal('balance', 11, 2)->default(0);
$table->string('password');
$table->string('image')->nullable();
$table->text('address')->nullable()->comment('contains full address');
$table->tinyInteger('status')->default(1)->comment('0: banned, 1: active');
$table->tinyInteger('ev')->default(0)->comment('0: email unverified, 1: email verified');
$table->tinyInteger('sv')->default(0)->comment('0: sms unverified, 1: sms verified');
$table->string('ver_code')->nullable()->comment('stores verification code');
$table->dateTime('ver_code_send_at')->nullable()->comment('verification send time');
$table->tinyInteger('ts')->default(0)->comment('0: 2fa off, 1: 2fa on');
$table->tinyInteger('tv')->default(1)->comment('0: 2fa unverified, 1: 2fa verified');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
Your migration has an error.... no foreign key defined>
public function up()
{
Schema::create('trxes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->nullable();
$table->foreign('user_id')->references('id')->on('users');
$table->string('amount',50)->nullable();
$table->string('main_amo',50)->nullable();
$table->string('charge',50)->nullable();
$table->string('type',50)->nullable();
$table->string('title')->nullable();
$table->string('trx')->nullable();
$table->timestamps();
});
}

Inner Join-Laravel

I have three tables in my database. User, Employer and Jobs.
Some Users are Employers who have posted some jobs.
I am trying to display jobs by users.
My code:
User Model
public function jobs(){
return $this->hasManyThrough('App\Employer','App\Job');
}
Routes:
Route::get('/find_user_jobs',function(){
$user=User::find(1);
foreach($user->jobs as $job){
echo $job->created_at."<br>";
}
});
But I get this error
Column not found: 1054 Unknown column 'jobs.user_id' in 'field list' (SQL: select `employers`.*, `jobs`.`user_id` from `employers` inner join `jobs` on `jobs`.`id` = `employers`.`job_id` where `jobs`.`user_id` = 1)
I get that its trying to find user_id in jobs but here is what I want it to do
My desire for the program
When I give the user id, go to employers table search for user_id, if it exists ,go to jobs table and search for the employers_id and return all the jobs with the employer_id.
User Migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->integer('employer_id');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
Job migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateJobsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('jobs', function (Blueprint $table) {
$table->increments('id');
$table->integer('employer_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('jobs');
}
}
Employer Migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateEmployersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('employers', function (Blueprint $table) {
$table->increments('id');
$table->string('company_name');
$table->integer('user_id');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('employers');
}
}
The relationship arguments are in the wrong order:
public function jobs(){
return $this->hasManyThrough('App\Job', 'App\Employer');
}
Because Employee are User who have Jobs you can create a model App\Employee which extends the App\User Model
class Job {
public function employee()
{
return $this->belongsTo(App\Employee::class,'user_id');
}
}
and Create an Employee class like this
Here in the Employee Model I set the $table property to users, when we make some query that query will have the target table set to users instead of the employees table which would be the default behavior of Eloquent.
class Employee extends User
{
protected $table = "users";
public function jobs()
{
return $this->hasMany(Job::class, 'user_id');
}
}
You can use directly the Employee Model and get jobs
And here are the corresponding migration
the create_user_table
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->string('company_name')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::drop('users');
}
}
The create_job_table
class CreateJobsTable extends Migration
{
public function up()
{
Schema::create('jobs', function (Blueprint $table) {
$table->increments('id');
$table->integer("user_id")->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
public function down()
{
Schema::drop('jobs');
}
}

Laravel converting DB query to eloquent

I'm new to laravel and have been pondering with this issue for a short while now. The main reason that I'd like to use eloquent is so that the datetime stamps work as the DB method ignores the created_at and updated_at fields. I am trying to reproduce the following query to eloquent:
$user_results = DB::table('users')->
leftJoin('roles', 'users.role_id','=', 'roles.id')->
get();
I have the following database setup
user migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('role_id')->unsigned()->index();
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
roles migration
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRolesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('rolename');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('roles');
}
}
and the user model contains
public function role() {
return $this->belongsTo('App\Role');
}
Any help would be much appreciated.
thanks.
Simply you can replace DB::table('users') with App\User
App\User::leftJoin('roles', 'users.role_id','=', 'roles.id')->get();

Categories