Trouble creating Laravel relationship - laravel

So I'm trying to have plans associated to the current user.
I have modal and migration called Plan. I have a method called user that I use the belongsTo() to get all the users that's associated to that plan. I'm not sure why its not working
Modal
namespace App\Models;
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Plan extends Model
{
use HasFactory;
protected $fillable = [
'name',
'slug',
'stripe_plan',
'cost',
'description'
];
public function getRouteKeyName()
{
return 'slug';
}
public function user()
{
return $this->belongsTo(User::class);
}
}
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePlansTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('plans', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug')->unique();
$table->string('stripe_plan');
$table->float('cost');
$table->text('description')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('plans');
}
}
I call the Plan model from my controller
controller that calls the model
the view that i call the users method
It show blanks
the output on the screen

You didn't load the relationship. You can use the with() to achieve this.
<?php
// controller
class PlanController extends Controller {
public function method(){
$plans = Plan::with('user')->get();
// Continue with Code
// if you use compact to pass data to the view
// else $plans = Plan::all();
// views('view_name', ['plans' => $plans] should word just fine
}
}

Related

Image value should be null when image is not uploaded

Employee Controller
<?php
namespace App\Http\Controllers;
use App\Talent\Employee\Requests\EmployeeCreateRequest;
use App\Talent\Employee\EmployeeManager;
use Illuminate\Http\Response;
use App\Talent\User\UserManager;
use App\Talent\Documents\DocumentManager;
use Illuminate\Support\Facades\Hash;
class EmployeeController extends Controller
{
public function __construct(private EmployeeManager $employeeManager,private UserManager $userManager,private DocumentManager $documentManager)
{
}
public function store(EmployeeCreateRequest $request){
$validated = $request->validated();
$userArray=[
'name'=>$validated['first_name']." ".$validated['last_name'],
'email'=>$validated['email'],
'password'=>Hash::make("Introcept#123"),
'role'=>'user'
];
$userCreate=$this->userManager->store($userArray);
return $this->employeeStore($validated,$userCreate,$request);
}
public function employeeStore($validated,$userCreate,$request){
if($request->hasFile($validated['avatar']))
{
$validated['avatar']=$validated['avatar']->store('employeeimages','public');
}
else
{
$validated['avatar']='null';
}
$userId=$userCreate->id;
$userIdArray=[
'user_id'=>$userId,
'status'=>'Active',
];
$employeeArray=array_merge($validated,$userIdArray);
$employeeCreate=$this->employeeManager->store($employeeArray);
$employeeId=$employeeCreate->id;
foreach($validated['documents'] as $document){
$name=$document->getClientOriginalName();
$type=$document->getClientMimeType();
$path=$document->store('employeedocuments','public');
$documentArray=[
'employee_id'=>$employeeId,
'original_name'=>$name,
'type'=>$type,
'path'=>$path,
];
$documentCreate=$this->documentManager->store($documentArray);
}
return response([
'userId'=>$userId,
'employeeId'=>$employeeId,
'message'=>'Personal detail added successfully',
],Response::HTTP_OK);
}
}
Employee.php
<?php
namespace App\Talent\Employee\Model;
use App\Talent\Documents\Model\Document;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
use HasFactory;
protected $fillable = [
'first_name',
'last_name',
'email',
'contact_number',
'date_of_birth',
'current_address',
'pan_number',
'bank_account_number',
'avatar',
'status',
'user_id',
];
**EmployeeRequest.php**
<?php
namespace App\Talent\Employee\Requests;
use Illuminate\Foundation\Http\FormRequest;
class EmployeeCreateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* #return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* #return array<string, mixed>
*/
public function rules()
{
return [
'first_name'=>'required|string',
'last_name'=>'required|string',
'email'=>'required|email|unique:employees,email',
'contact_number'=>'required|string',
'date_of_birth'=>'required|date',
'current_address'=>'required|string',
'pan_number'=>'string|nullable',
'bank_account_number'=>'string|nullable',
'avatar' => 'nullable|image|mimes:jpg,jpeg,png',
'documents'=>'required',
'documents.*'=>'max:5000|mimes:pdf,png,jpg,jpeg',
];
}
public function messages()
{
return [
'documents.max' => "Error uploading file:File too big.Max file size:5MB",
];
}
}
EmployeeMigration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('employees', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained('users')->restrictOnDelete();
$table->string('status');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('contact_number');
$table->date('date_of_birth');
$table->string('current_address');
$table->string('pan_number')->nullable();
$table->string('bank_account_number')->nullable();
$table->string('avatar')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('employees');
}
};
I am trying to create an API that stores employee personal details into the database. Here, image field is optional so I made avatar field nullable in both migration and validation request as well. But when I try save details into the database without uploading image it shows undefined avatar. I don't know what happening here any help or suggestions will be really appreciated. Thank you.
You are using the string value of null instead of the keyword null.

Laravel Showing all users and check if belong to Bans table

Hi im working on admin dashboard
and i want to show all users and check if the user blocked get information about the ban from bans table
im using cybercog/laravel-ban to ban users
Laravel 5.8
So i have two table
Users table and Bans table
Bans table has the user id of the banded user in column called : bannable_id
User model :
namespace App;
use Illuminate\Notifications\Notifiable;
use Cog\Contracts\Ban\Bannable as BannableContract;
use Cog\Laravel\Ban\Traits\Bannable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Events\Verified;
class User extends Authenticatable implements MustVerifyEmail,BannableContract
{
use Notifiable;
use Bannable;
protected $fillable = [
'name', 'email', 'password',
'last_login_at',
'last_login_userganet',
'avatar','uid'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function UserBan()
{
return $this->hasOne('App\Ban','bannable_id');
}
Ban Model :
namespace App;
use Illuminate\Database\Eloquent\Model;
class Ban extends Model
{
protected $fillable = [
'id','expired_at','bannable_type'
];
protected $casts = [
'bannable_type'
];
public function Users()
{
return $this->belongsTo('App\User');
}
}
User Controller :
namespace App\Http\Controllers;
use App\User;
use App\Ban;
use App\Http\Requests\UserRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Cog\Contracts\Ban\Bannable;
class UserController extends Controller
{
/**
* Display a listing of the users
*
* #param \App\User $model
* #return \Illuminate\View\View
*/
public function index(User $model,Ban $banModel)
{
return view('users.index', ['users' => $model->paginate(15),'bans'=>$banModel->paginate(15)]);
}
Users schema :
<?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->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->timestamp('last_login')->nullable();
$table->string('last_login_provider')->nullable();
$table->string('last_login_useragent')->nullable();
$table->string('last_login_ip')->nullable();
$table->string('uid')->nullable();
$table->string('password');
$table->string('avatar')->nullable();
$table->string('facebook_id')->unique()->nullable();
$table->string('google_id')->unique()->nullable();
$table->string('twitter_id')->unique()->nullable();
$table->string('instagram_id')->unique()->nullable();
$table->enum('user_type',['member','admin'])->default('member');
$table->boolean('blocked')->default(false);
$table->timestamp('banned_at')->nullable();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
I could not found the bans schema but this is the table of bans :
you are using wrong relationship, you should use polymorphic. Look link below.
https://laravel.com/docs/5.8/eloquent-relationships#one-to-one-polymorphic-relations
it would be something like
User model
public function ban()
{
return $this->morphOne('App\Ban', 'bannable');
}
Ban Model
public function bannable()
{
return $this->morphTo();
}
get all users in controller
define
use App\User;
then
$users = User::all();
then you can foreach it and call ban() method on each
foreach($users as $user)
{
//return associated ban model
$user->ban;
}
Or for better performance, use
User::with(ban)->get();
which highly decrease number of SQL calls. It retrieves all user with ban model. Look at eager loading in link below.
https://laravel.com/docs/5.8/eloquent-relationships#eager-loading
Hope it helps. :)

base table or view not found in laravel after create category and redirect to category.index

i have problem with creating cateogories.
before i change the category relationship to many to many i didnt have problem.
whene i post new category and after that its redirect to category list page, i got this error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'myblog.category_post' doesn't exist (SQL: select posts.*, category_post.category_id as pivot_category_id, category_post.post_id as pivot_post_id from posts inner join category_post on posts.id = category_post.post_id where category_post.category_id = 1) (View: C:\Users\M0RT3Z4\Desktop\MyBlog\resources\views\admin\category\index.blade.php)
but the category insert in database. if a record insert in to category table, the category list will not show and i will get error.
Post Model:
namespace App\Models;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use Sluggable;
protected $fillable = [
'title', 'body', 'views', 'category_id', 'user_id'
];
public function comment()
{
$this->hasMany(Comment::class);
}
public function category()
{
return $this->belongsToMany(Category::class);
}
public function tags()
{
return $this->belongsToMany(Tag::class);
}
/**
* Return the sluggable configuration array for this model.
*
* #return array
*/
public function sluggable(): array
{
return [
'slug' => [
'source' => 'title'
]
];
}
public function hastags($id)
{
return in_array($id, $this->tags()->pluck('id')->toArray());
}
public function hascategories($id)
{
return in_array($id,$this->category()->pluck('id')->toArray());
}
}
Category Model:
<?php
namespace App\Models;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use Sluggable;
protected $fillable = [
'name','slug'
];
public function post()
{
return $this->belongsToMany(Post::class);
}
/**
* Return the sluggable configuration array for this model.
*
* #return array
*/
public function sluggable(): array
{
return [
'slug' => [
'source' => 'name'
]
];
}
}
CategoryController:
<?php
namespace App\Http\Controllers\Admin;
use App\Models\Category;
use App\Models\Post;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;
class CategoryController extends Controller
{
public function index()
{
$post = Post::all();
$cats = Category::orderBy('id','DESC')->paginate(10);
return view('admin.category.index',compact(['cats','post']));
}
public function create()
{
return view('admin.category.create');
}
public function show(Category $category)
{
return view('admin.category.show',compact('category'));
}
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|min:3|unique:categories',
]);
$cat = new Category();
$cat->name = $request->name;
$cat->slug = $request->slug;
$cat->save();
Session::flash('success','Category Created Successfully');
return redirect()->route('admin.category.index');
}
public function edit(Category $category)
{
return view('admin.category.edit',compact('category'));
}
public function update(Category $category,Request $request)
{
$this->validate($request, [
'name' => 'required|min:3|unique:categories',
]);
$category->name = $request->name;
$category->save();
Session::flash('update','Category Updated Successfully');
return redirect()->route('admin.category.index');
}
public function destroy(Category $category)
{
$category->delete();
Session::flash('delete','Category Deleted Successfully');
return redirect()->route('admin.category.index');
}
}
categories migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
}
post_category migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostCategory extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('post_category', function (Blueprint $table) {
$table->unsignedInteger('post_id');
$table->unsignedInteger('category_id');
$table->foreign('post_id')->on('posts')->references('id')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('category_id')->on('categories')->references('id')->onUpdate('cascade')->onDelete('cascade');
$table->unique(['post_id', 'category_id']);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('post_category');
}
}
posts migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title')->unique();
$table->string('slug')->unique()->nullable();
$table->text('body');
$table->string('image')->nullable();
$table->unsignedInteger('views')->default(0);
$table->unsignedInteger('category_id')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
}
you should use best practice for migration naming convention to avoid this issue
rename create_post_category to category_post (alphabetical order)
or you can specify within $this->belongsToMany(Category::class,'category_post') additional arguments for the pivot table and keys

Laravel store data from href

I want to make option where you can create item from navbar where you just click on button in navbar and it creates an item. Here is my code
This is navbar blade
<ul class="dropdown-menu">
<li><a href="{{ action('InventoryController#store') }}">Create
Inventory</a></li>
</ul>
and this is my store method in controller
public function store(Request $request)
{
$inventory = new Inventory();
$inventory->company_id = $request->get('company_id');
$inventory->save();
return redirect('inventories')->with('success', 'Inventory has been added');
}
After i click on that button it only goes to page inventories but new inventory is not created.
Update:
my route is this:
Route::get('/create-inventory/{id}', 'InventoryController#store')->middleware('auth');
and my store function looks like this now:
public function store(Request $request, $id)
{
$inventory = new Inventory();
$inventory->company_id = $id;
$inventory->save();
return redirect('inventories')->with('success', 'Inventory has been added');
}
This is my inventory.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Inventory extends Model
{
**protected $fillable = ['company_id'];**
}
This is my database migration file
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateInventoriesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('inventories', function (Blueprint $table) {
$table->increments('id');
$table->integer('company_id');
$table->timestamps();
$table->dateTime('finished_at');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('inventories');
}
}
UPDATE 2
This is my inventory.php now
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Inventory extends Model
{
protected $fillable = ['company_id'];
}
It still not working.
I believe the problem might be with the generated link, maybe this works:
Create Inventory

MorphMany laravel relation does not work for some reason

I am using laravel 5.6 version, here is my Test model file
<?php
namespace App\Model\data\Models;
use Illuminate\database\Eloquent\Model;
class Test extends Model
{
protected $guarded = ['id'];
protected $table = 'tests';
/*
* Test - TestTranslation relation
* Test has many translations
*/
public function translations()
{
return $this->hasMany('App\Model\Data\Models\TestTranslation');
}
/*
* Test - Image relation
* Test can have an thumbnail
*/
public function thumbnails()
{
return $this->morphMany('App\Model\Data\Models\Image', 'type');
}
}
Here is my Image model
<?php
namespace App\Model\Data\Models;
use Illuminate\Database\Eloquent\Model;
class Image extends Model
{
protected $guarded = ['id'];
protected $table = 'images';
/*
* Image belongs to Post, Account, Comment
*/
public function type()
{
return $this->morphTo();
}
}
Here is my Database scheme for Images table:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateImagesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->string('path');
$table->enum('type', ['avatar', 'thumbnail', 'generic'])->default('generic');
$table->integer('type_id')->unsigned()->nullable();
$table->enum('type_type',[
"Account",
"Comment",
"Post",
"Skill",
"Test"
])->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('images');
}
}
In AppServiceProvider I linked 'Test' to my model file:
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use Laravel\Dusk\DuskServiceProvider;
use Illuminate\Database\Eloquent\Relations\Relation;
use App\Model\Data\Models\Account;
use App\Model\Data\Models\EmailChangeConfirmation;
use App\Model\Observers\Common\AccountObserver;
use App\Model\Observers\Common\EmailChangeConfirmationObserver;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Schema::defaultStringLength(191);
Relation::morphMap([
'Account' => 'App\Model\Data\Models\Account',
'Comment' => 'App\Model\Data\Models\Comment',
'Post' => 'App\Model\Data\Models\Post',
'Skill' => 'App\Model\Data\Models\Skill',
'Test' => 'App\Model\Data\Models\Test'
]);
if(env('APP_ENV') != 'testing') {
Account::observe(AccountObserver::class);
EmailChangeConfirmation::observe(EmailChangeConfirmationObserver::class);
}
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
if ($this->app->environment('local', 'testing')) {
$this->app->register(DuskServiceProvider::class);
}
}
}
As you can see, I have more morhpMany types, and all of them works fine. But for some reason, this Test relation that I added recently always returns an empty collection, even tough I am sure that test has thumbnails. Cause:
in this screenshot, I have records in database that each test has two images. Their ID matches ids of created tests and type is "Test". I tried to clear all caches, run composer dump-autoload but none of them helped.
Maybe someone can help identify what is wrong here? Because as I said before other types as Post, Account works fine.

Resources