How do I make relations laravel in different folders? - laravel

My user model like this :
namespace App\Models\Auth;
...
class User extends Authenticatable
{
...
public function vendor()
{
return $this->belongsTo(Vendor::class, 'vendor_id', 'id');
}
}
My vendor model like this :
namespace App\Models;
...
class Vendor extends Model
{
...
public function users()
{
return $this->hasMany(User::class, 'id', 'vendor_id');
}
}
If the relation run, there exist error like this :
Class 'App\Models\Auth\Vendor' not found
It seems that the error occurred because the vendor model is not in the auth folder
How do I solve that error without moving the vendor model to the auth folder?

In simple word, you need to import Vendor class into the User class.
namespace App\Models\Auth;
use App\Models\Vendor; //code to be added
...
class User extends Authenticatable
{
...
public function vendor()
{
return $this->belongsTo(Vendor::class, 'vendor_id', 'id');
}
}

Pretty sure it's because you didn't explicitly load the Vendor class into the User file.
Add into your User file
use App\Models\Vendor;

Related

Call to a member function reply() on null laravel botman

This is the code of model. I am unable to change access the function of conversation using the model method as i am passing data from view to botman conversation constructor through request.
<?php
This is the conversation class
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Conversations\TestConversation;
class CheckSaved extends Model
{
public function hasDate($scheduled_date):bool
{
if($scheduled_date)
{
$testconv = new TestConversation($scheduled_date);
$testconv->showOther();
}
return false;
}
}][1]
<?php
namespace App\Conversations;
use BotMan\BotMan\Messages\Incoming\Answer;
use BotMan\BotMan\Messages\Conversations\Conversation;
use App\Http\Traits\PropertyDetails;
use App\Models\CheckSaved;
class TestConversation extends Conversation
{
public $is_saved;
public function __construct($request)
{
if(isset($request->scheduled_date))
{
$checkDate = new CheckSaved();
$this->is_saved = $checkDate->hasDate($request->scheduled_date);
}
}
public function sayHi()
{
// dd($request);
$this->say("<input type='datetime-local' id='birthdaytime' name='birthdaytime'>");
$this->say("<button class='btn-date' onclick='test()'>Save</button>");
}
public function showOther()
{
$this->say('Hi');
}
public function run()
{
$this->sayHi();
}
}
This is the image of Model
I am getting reply() on null if try to call a function called "showOther()" inside TestConversation. I am stuck on it please someone help or contact me.

How to get reverse in laravel one to many relationship

I am writing an application that I use laravel eloquent relationship
table structure
clients
id
payroll_id
name
Payroll
id
payroll_id
basic
hmo
Client Model
use Illuminate\Database\Eloquent\Model;
class AdminClient extends Model
{
public function payroll()
{
return $this->hasMany(Payroll::class, "payroll_id", "payroll_id");
}
}
Payroll Model
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Payroll extends Model
{
public function clients()
{
return $this->belongsTo(AdminClient::class, "payroll_id", "payroll_id");
}
}
I am able to do
dd(AdminClient::with("payroll")->get());
but the reverse below is not working
dd(Payroll::with("clients")->get());
I am getting a null value for the clients.
Any Solutions Thank you.
use this in AdminClient
public function payroll()
{
return $this->hasMany(Payroll::class);
}
and use this in Payroll
public function clients()
{
return $this->belongsTo(AdminClient::class);
}
or if it not work then use this in AdminClient
public function payroll()
{
return $this->hasMany(Payroll::class, "forign key of AdminClient");
}
and use this in Payroll
public function clients()
{
return $this->belongsTo(AdminClient::class, "payroll_id");
}
hope its work

one to many relationship laravel with multiple foreign key

I am on a project with Laravel.
I have two database tables that are in a One-To-Many relationship with each other. They are joined by three conditions. How do I model this relationship in Eloquent?
I am not supposed to modify the database schema, since it has to remain backward compatible with other things.
I have tried the following, but it doesn't work.
The owning side:
use Illuminate\Database\Eloquent\Model;
class Route extends Model
{
public function trips()
{
return $this->hasMany('Trip', 'route_name,source_file', 'route_name,source_file')
}
}
The inverse side:
use Illuminate\Database\Eloquent\Model;
class Trip extends Model
{
public function routes()
{
return $this->belongsTo('Route', 'route_name,source_file', 'route_name,source_file');
}
}
I don't want to use "use Awobaz\Compoships\Compoships;"
You need to give hasMany() and belongsTo method the namespace of your models.
Something like 'App\Models\Trip' or Trip::class
class Route extends Model
{
public function trips()
{
return $this->hasMany('App\Models\Trip', 'route_name,source_file', 'route_name,source_file')
}
}
class Trip extends Model
{
public function routes()
{
return $this->belongsTo('App\Models\Route', 'route_name,source_file', 'route_name,source_file');
}
}

Eloquent : delete rows from multiple table with same id

I am a bit new to Laravel. I am trying to delete a project from a table along with its images and plans from 2 other tables. How to do this in Laravel Eloquent?
Here is the delete controller of the project:
public function destroy($id)
{
$project = Projects::findOrFail($id);
$project->delete();
return Redirect::to('admin/view-project')->with('message', 'Project deleted successfully');
}
How can I get this to be done from the model? I didn't understand that.
Here is the Projects model:
class Projects extends Eloquent implements UserInterface, RemindableInterface
{
use UserTrait, RemindableTrait;
protected $table = 'project_info_arabic';
public function projectImages()
{
return $this->hasMany('ProjectsImage');
}
public function projectPlans()
{
return $this->hasMany('ProjectsPlans');
}
}
Can kindly anybody help?
The better way when thinking about data consistency is the implementation of foreign keys on the database layer. That does it automatically for you and you don't need to think about it anymore.
See https://laravel.com/docs/5.3/migrations#foreign-key-constraints
You could try model events in laravel.Check this
class Projects extends Eloquent implements UserInterface, RemindableInterface
{
public static function boot()
{
parent::boot();
Projects::deleted(function($project)
{
$project->projectImages()->delete();
$project->projectPlans()->delete();
});
}
}

Call to undefined method Illuminate\Support\Facades\Log::save()

I created 'Log' class in app/models :
class Log extends Eloquent {
public function user() {
return $this->belongsTo('user');
}
}
When i try to save log object in my controller i got this error (Call to undefined method Illuminate\Support\Facades\Log::save() ) I thik because in (app/config/app) in providers laravel define Log class => 'Log'=> 'Illuminate\Support\Facades\Log',.
How can i resolve this problem without change class name ?
Yes the problem is indeed a conflict with the Log facade alias. To fix it use namespaces:
<?php namespace YourApp;
class Log extends Eloquent {
public function user() {
return $this->belongsTo('user');
}
}
And then you can use your class like so:
$log = new YourApp\Log();
You could of course rename the alias name, but namespacing your classes is a much better approach.

Resources