Only allow one feedback per task [Laravel 5] - php

I'm trying to make a feedback system where a user can only give one feedback per 'task'-item.
Currently one user can give as many feedbacks as he/she wants but ofcourse I want to limit this, seeing there is a rating behind it, ect..
Any help would be much appreciated!
Thanks
These are my models:
//Feedback
public function user()
{
return $this->belongsTo('App\User');
}
public function tnb()
{
return $this->belongsTo('App\Tnb');
}
//Tnb
public function user()
{
return $this->belongsTo('App\User');
}
public function feedbacks()
{
return $this->hasMany('App\Feedback');
}
// User
public function tnbs()
{
return $this->hasMany('App\Tnb');
}
public function feedbacks()
{
return $this->hasMany('App\Feedback');
}
My database tabels:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
Schema::create('tnb', function (Blueprint $table) {
$table->increments('id');
$table->string('type');
$table->string('name');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onCascade('update')->onDelete('cascade');
$table->integer('group_id')->unsigned();
$table->foreign('group_id')->references('id')->on('groups')->onCascade('update')->onDelete('cascade');
$table->string('desc');
$table->string('slug');
$table->date('startdate');
$table->time('starttime');
$table->date('enddate');
$table->time('endtime');
$table->timestamps();
});
Schema::create('feedbacks', function (Blueprint $table) {
$table->increments('id');
$table->string('score');
$table->string('feedback');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onCascade('update')->onDelete('cascade');
$table->integer('tnb_id')->unsigned();
$table->foreign('tnb_id')->references('id')->on('tnb')->onCascade('update')->onDelete('cascade');
$table->timestamps();
});
I've been trying a couple things in my controller but until now I'm not getting anywhere:
public function store(Request $request, Group $group, Tnb $tnb)
{
$user = auth()->user();
$feedback = new Feedback(
array(
'score' => $request->get('score'),
'feedback' => $request->get('feedback')
));
$feedback->user()->associate($user);
//dd($feedback->user()->whereId(Auth::user()->id)->count());
$tnb->feedbacks()->save($feedback);
\Flash::success('Your feedback has succesfully been sumbitted!');
return redirect()->back();
}

Found the solution!
public function store(Request $request, Group $group, Tnb $tnb)
{
$user = auth()->user();
$feedback = new Feedback(
array(
'score' => $request->get('score'),
'feedback' => $request->get('feedback')
));
$feedback->user()->associate($user);
foreach($user->feedbacks as $feedback)
{
if ($tnb->user_id == $user->id)
{
\Flash::error('You may not give a feedback on your own tasks and bookings!');
return redirect()->back();
if($feedback->user_id == $user->id)
{
\Flash::error('You have already posted a feedback!');
return redirect()->back();
}
}
}
$tnb->feedbacks()->save($feedback);
\Flash::success('Your feedback has succesfully been sumbitted!');
return redirect()->back();
}

Related

show pop up only first time user login (Laravel)

I have a simple question about a project I working on it
I need to show a Modal Popup once when the user login in first time no more only once!!!
I create this code but it still not working
test.blade.php
#if ($first_time_login)
<h3>Welcome Popup</h3>
#else
<h3>Hey! 🖐 Nothing to Show</h3>
#endif
TestController
public function Test()
{
if (Auth::user()->first_time_login) {
$first_time_login = true;
Auth::user()->first_time_login = 1;
Auth::user()->save();
} else {
$first_time_login = false;
}
return view(
'test',
['first_time_login' => $first_time_login]
);
}
2014_10_12_000000_create_users_table.php
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable();
$table->string('email')->unique()->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password')->nullable();
$table->rememberToken()->nullable();
$table->timestamps();
$table->string('first_time_login')->default(false);
});
I see a few things on your code.
Firstly you are declaring the first_time_login field as a string, it should be boolean with default value of true. Like this:
2014_10_12_000000_create_users_table.php
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->nullable();
$table->string('email')->unique()->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password')->nullable();
$table->rememberToken()->nullable();
$table->timestamps();
$table->boolean('first_time_login')->default(true);
});
Another thing, after checking if it's a first time login you are setting it to 1. This will make your field remain as true. Change it to:
TestController
public function Test()
{
if (Auth::user()->first_time_login) {
$first_time_login = true;
Auth::user()->first_time_login = false;
Auth::user()->save();
} else {
$first_time_login = false;
}
return view(
'test',
['first_time_login' => $first_time_login]
);
}
That should do it.

How get correct values from DB in Laravel 5.8

I'm begginer in Laravel. I have this code:
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
use psCMS\Presenters\UserPresenter;
public static $roles = [];
public function roles()
{
return $this->belongsToMany('App\Role');
}
public function comments()
{
return $this->hasMany('App\Comments');
}
public function hasRole(array $roles)
{
foreach($roles as $role)
{
if(isset(self::$roles[$role]))
{
if(self::$roles[$role]) return true;
}
else
{
self::$roles[$role] = $this->roles()->where('name', $role)->exists();
if(self::$roles[$role]) return true;
}
}
return false;
}
}
class Role extends Model
{
protected $quarded = [];
public $timestamps = false;
public function users()
{
return $this->belongsToMany('App\User');
}
}
and schema:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('company_id')->unsigned();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->boolean('enable')->default(0);
$table->string('name', 120)->nullable();
$table->string('surname', 120)->nullable();
$table->string('email', 120)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->bigInteger('counter')->default(0);
$table->rememberToken();
$table->timestamps();
$table->engine = "InnoDB";
});
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->engine = "InnoDB";
});
Schema::create('role_user', function (Blueprint $table) {
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->bigInteger('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->engine = "InnoDB";
});
And my user:
DB::table('users')->insert([
'name' => 'Marian',
'surname' => 'La',
'email' => 'marian#icloud.com',
'email_verified_at' => \Carbon\Carbon::now(),
'password' => Hash::make('passw'),
]);
DB::table('role_user')->insert([
'user_id' => 1,
'role_id' => 1,
]);
This code work fine. I have problem with my role.
How can i print user role in blade?
I make this code:
public function getAdmin(int $id)
{
return User::find($id);
}
$admin = $this->getAdmin(1);
And now my $admin - has admin object.
When i print in blade file: $admin->name, $admin->surname - it's work.
When i print: {{ $admin->roles }}
i have result:
[{"id":1,"name":"admin","pivot":{"user_id":1,"role_id":1}}]
How can I show correct value (admin):
I need result: admin not this:
[{"id":1,"name":"admin","pivot":{"user_id":1,"role_id":1}}]
That is Many to Many relationship and each user may have many roles ! So use foreach to print all of rules in your blade :
#foreach ($admin->roles as $role)
{{ $role -> name }},
#endforeach
https://laravel.com/docs/5.8/eloquent-relationships#many-to-many
Hope this helps !

Eloquent How do I store data if i have 2 models with "one-to-many" on one model?

Ok, i have the following ER Diagram that shows my relations in DB:
I am willing to generate a User when the Order is made. The Order can contain many Products, also the Orders are grouped by their transaction_id, so i can include all the products in the same order for a User. I am trying to save them all but i am getting Field 'transaction_id' doesn't have a default value. I've managed to save the user, also to link the order with the product and the user but i still can't figure out how to save transaction_id linked with order.
These are my migrations:
Schema::create('orders', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index()->nullable();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('transaction_id')->unsigned();
$table->foreign('transaction_id')->references('id')->on('transactions');
$table->timestamps();
Schema::create('order_product', function (Blueprint $table) {
$table->integer('order_id')->unsigned()->index();
$table->foreign('order_id')->references('id')->on('orders')->onDelete('cascade');
$table->integer('product_id')->unsigned()->index();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->integer('quantity')->unsigned();
$table->timestamps();
});
Schema::create('products', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->float('price',8,2);
$table->timestamps();
});
Schema::create('transactions', function (Blueprint $table) {
$table->increments('id');
$table->enum('status',['open','closed']);
$table->timestamps();
});
My models:
User Model:
public function orders() {return $this->hasMany(Order::class);}
Order Model:
public function user()
{
return $this->belongsTo(User::class);
}
public function products()
{
return $this->belongsToMany(Product::class)
->withPivot('quantity')
->withTimestamps();
}
public function transaction()
{
return $this->belongsTo(Transaction::class);
}
Transaction Model:
public function orders() {return $this->hasMany(Order::class);}
This is how i'm trying to save them:
$user->save();
$transaction->save();
$order = new Order();
$order->user_id = $user->id;
$user->orders()->save($order);
$transaction->orders()->save($order);
$order->products()->attach($cartItem->id, ['quantity' => $cartItem->qty]);
P.S. Sorry for the long post, i am out of ideeas.
Use this:
$order = new Order();
$order->user_id = $user->id;
$order->transaction_id = $transaction->id;
$order->save();

one to many And one to many relationship laravel

This is projects migrate
Schema::create('projects', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('start_date');
$table->string('end_date');
$table->string('con');
$table->timestamps();
});
and this is timesheets migrate
Schema::create('timesheets', function (Blueprint $table) {
$table->increments('id');
$table->string('user_id');
$table->string('project_id');
$table->string('day');
$table->string('month');
$table->string('year');
$table->string('jalali');
$table->string('timesheet_h');
$table->string('timesheet_m');
$table->timestamps();
});
and this users migrate
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('mobile');
$table->string('salary_base');
$table->string('salary_base_h');
$table->string('start_contract');
$table->string('end_contract');
$table->string('start_insurance');
$table->string('end_insurance')->nullable();
$table->string('first_salary');
$table->string('date_birth');
$table->string('melli_code');
$table->string('s_number');
$table->string('nda');
$table->string('work_rules');
$table->string('end_work')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
This my project model
public function timesheets()
{
return $this->hasMany(timesheet::class,'project_id');
}
This my timesheet model :
public function projects()
{
return $this->belongsTo(project::class,'project_id');
}
public function users()
{
return $this->belongsTo(User::class,'user_id','id');
}
and This my User model
public function project_peoples()
{
return $this->hasMany('App\project_people');
}
public function timesheets()
{
return $this->belongsTo(timesheet::class);
}
public function projects()
{
return $this->belongsTo(project::class);
}
Now I return my query from projects
public function allProject()
{
$projects=project::with(['timesheets','users'])->get();
return $projects;
}
This is ok but user_id Users in timesheets.user_id And I can not get it out timesheets and get it
This controller return project and timesheet by project_id in timesheet but user_id in timesheet I do not know how to get this into the system
Use dot syntax to load nested relationships:
project::with('timesheets.users')->get();
[{"id":1,"name":"\u067e\u0631\u0648\u0698\u0647 \u062a\u0627\u06cc\u0645 \u0634\u06cc\u062a","start_date":"1111\/11\/11","end_date":"1111\/11\/11","con":"\u062f\u0631\u062d\u0627\u0644 \u0627\u062c\u0631\u0627","created_at":"2018-01-02 10:54:11","updated_at":"2018-01-02 10:54:11","timesheets":[{"id":7,"user_id":"2","project_id":"1","day":"12","month":"10","year":"1396","jalali":"1396\/10\/12","timesheet_h":"5","timesheet_m":"24","created_at":"2018-01-02 12:40:12","updated_at":"2018-01-02 13:47:09","users":{"id":2,"name":"\u0645\u0633\u0639\u0648\u062f \u0633\u0644\u06cc\u0645\u0627\u0646\u06cc","mobile":"0000","salary_base":"1000000","salary_base_h":"20000","start_contract":"1111\/11\/11","end_contract":"1000\/00\/00","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"1212","s_number":"1212","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 12:36:07","updated_at":"2018-01-02 12:36:07"}},{"id":8,"user_id":"1","project_id":"1","day":"13","month":"10","year":"1396","jalali":"1396\/10\/13","timesheet_h":"10","timesheet_m":"10","created_at":"2018-01-03 05:59:13","updated_at":"2018-01-03 05:59:13","users":{"id":1,"name":"\u0645\u062c\u06cc\u062f \u0641\u06cc\u0636\u06cc","mobile":"00","salary_base":"3000000","salary_base_h":"10000","start_contract":"1111\/11\/11","end_contract":"1111\/11\/11","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"00","s_number":"00","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 10:53:48","updated_at":"2018-01-02 10:53:48"}},{"id":9,"user_id":"2","project_id":"1","day":"14","month":"10","year":"1396","jalali":"1396\/10\/14","timesheet_h":"10","timesheet_m":"15","created_at":"2018-01-04 07:17:44","updated_at":"2018-01-04 07:17:44","users":{"id":2,"name":"\u0645\u0633\u0639\u0648\u062f \u0633\u0644\u06cc\u0645\u0627\u0646\u06cc","mobile":"0000","salary_base":"1000000","salary_base_h":"20000","start_contract":"1111\/11\/11","end_contract":"1000\/00\/00","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"1212","s_number":"1212","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 12:36:07","updated_at":"2018-01-02 12:36:07"}},{"id":10,"user_id":"2","project_id":"1","day":"16","month":"10","year":"1396","jalali":"1396\/10\/16","timesheet_h":"10","timesheet_m":"60","created_at":"2018-01-06 07:17:21","updated_at":"2018-01-06 07:17:21","users":{"id":2,"name":"\u0645\u0633\u0639\u0648\u062f \u0633\u0644\u06cc\u0645\u0627\u0646\u06cc","mobile":"0000","salary_base":"1000000","salary_base_h":"20000","start_contract":"1111\/11\/11","end_contract":"1000\/00\/00","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"1212","s_number":"1212","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 12:36:07","updated_at":"2018-01-02 12:36:07"}}]},{"id":2,"name":"\u067e\u0631\u0648\u0698\u0647 \u062a\u0633\u062a\u06cc","start_date":"1111\/11\/11","end_date":"1111\/11\/11","con":"\u062f\u0631\u062d\u0627\u0644 \u0627\u062c\u0631\u0627","created_at":"2018-01-02 11:28:03","updated_at":"2018-01-02 11:28:03","timesheets":[{"id":3,"user_id":"1","project_id":"2","day":"12","month":"10","year":"1396","jalali":"1396\/10\/11","timesheet_h":"8","timesheet_m":"12","created_at":"2018-01-02 11:39:46","updated_at":"2018-01-02 11:39:46","users":{"id":1,"name":"\u0645\u062c\u06cc\u062f \u0641\u06cc\u0636\u06cc","mobile":"00","salary_base":"3000000","salary_base_h":"10000","start_contract":"1111\/11\/11","end_contract":"1111\/11\/11","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"00","s_number":"00","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 10:53:48","updated_at":"2018-01-02 10:53:48"}},{"id":4,"user_id":"1","project_id":"2","day":"12","month":"10","year":"1396","jalali":"1396\/10\/10","timesheet_h":"4","timesheet_m":"11","created_at":"2018-01-02 11:40:41","updated_at":"2018-01-02 13:49:45","users":{"id":1,"name":"\u0645\u062c\u06cc\u062f \u0641\u06cc\u0636\u06cc","mobile":"00","salary_base":"3000000","salary_base_h":"10000","start_contract":"1111\/11\/11","end_contract":"1111\/11\/11","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"00","s_number":"00","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 10:53:48","updated_at":"2018-01-02 10:53:48"}},{"id":6,"user_id":"1","project_id":"2","day":"12","month":"10","year":"1396","jalali":"1396\/10\/12","timesheet_h":"1","timesheet_m":"12","created_at":"2018-01-02 12:06:31","updated_at":"2018-01-02 13:49:37","users":{"id":1,"name":"\u0645\u062c\u06cc\u062f \u0641\u06cc\u0636\u06cc","mobile":"00","salary_base":"3000000","salary_base_h":"10000","start_contract":"1111\/11\/11","end_contract":"1111\/11\/11","start_insurance":"1111\/11\/11","end_insurance":null,"first_salary":"100000","date_birth":"1111\/11\/11","melli_code":"00","s_number":"00","nda":"\u062f\u0627\u0631\u062f","work_rules":"\u062f\u0627\u0631\u062f","end_work":null,"created_at":"2018-01-02 10:53:48","updated_at":"2018-01-02 10:53:48"}}]}]
This is my return i have 2 users but repeat my 2 users :(

I want to display job with company that related to single postjob by category in laravel?

I have three table below:
I want to display all Related job post by category in Single jobpost. and I already have single job post page but in the same single job page I want to display related jobs in the left side. see my picture!
what is controller should be and in the Single job page (view) should be? please help?
My jobController
public function show($id, $company_id)
{
$singleJob = Job::find($id);
$company = Company::find($company_id);
$similarJob = Job::with('company')->where('category_id', $id)->get();
return view('pages.single-job')->with([
'singleJob'=> $singleJob,
'company'=> $company,
'similarJob' => $similarJob,
]);
}
My relationship
job.php
public function company(){
return $this->belongsTo(Company::class);
}
Job.php
public function category(){
return $this->belongsTo(Category::class);
}
//category.php
public function job(){
return $this->hasMany(Job::class);
}
//company.php
public function job(){
return $this->hasMany(Job::class);
}
Job table
Schema::create('jobs', function (Blueprint $table) {
$table->increments('id');
$table->integer('company_id');
$table->string('jobTitle');
$table->longText('jobDescription');
Company Table
Schema::create('company_types', function (Blueprint $table) {
$table->increments('id');
$table->integer('admin_id');
$table->string('name');
$table->timestamps();
});
Category table
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('name');
$table->timestamps();
});
You can use whereHas like this :
public function show($id, $company_id)
{
$singleJob = Job::find($id);
$company = Company::find($company_id);
$similarJobs = Job::with('company')
->whereHas('category', function ($query) use($singleJob) {
$query->where('id', $singleJob->category->id);
})
->get();
return view('pages.single-job')->with([
'singleJob'=> $singleJob,
'company'=> $company,
'similarJobs' => $similarJobs,
]);
}
And in the view you can use it like this :
#foreach ($similarJobs as $similarJob)
// Add the job partial, componnent or just your HTML here for showing the Job
#endforeach
For the question in the comment, to find jobs that have a company that belongs to a given industry :
$some_industry_type_id = 1;
$jobsOfIndustryType = Job::whereHas('company', function ($query) use($some_industry_type_id) {
$query->where('industry_type_id', $some_industry_type_id);
})
->get();

Categories