Laravel queue to store data - laravel-5

I wanna try to store data into database via database queue Laravel.
But I always get this error "Undefined offset: 0"
this is my controller :
public function store(Request $request)
{
$order = new Order;
$order->code = $request->code;
$order->created_at = $request->created_at;
$this->dispatch(new SalesOrder($order));
}
and this is my SalesOrder Jobs :
protected $order;
public function __construct(Order $order)
{
$this->order= $order;
}
public function handle()
{
$this->order->save();
}
is there something wrong in my code? Please somebody help me fix this issue. Than's anyway.

Instead of pass Order object pass in job order data, And the in job save order.
Controller code.
public function store(Request $request)
{
$data['code'] = $request->code;
$data['created_at'] = $request->created_at;
$this->dispatch(new SalesOrder($data));
}
Job code
protected $data;
public function __construct(array $data)
{
$this->data = $data;
}
public function handle(Order $order)
{
if (!$order->craete($this->data)) {
// when not saved try again
$this->release();
}
return true;
}

Try running
$order = new Order;
$order->code = $request->code;
$order->created_at = $request->created_at;
inside your job so it looks like this
protected $order;
protected $request
public function __construct($request)
{
$this->order = new Order;
$this->request = $request;
}
public function handle()
{
$this->order->code = $request->code;
$this->order->created_at = $request->created_at;
$this->order->save();
}
and in your controller
public function store(Request $request)
{
$this->dispatch(new SalesOrder($request));
}
This should work. Did not test this am writing on my phone.

Related

laravel Notification Undefined index: user_id

I going to do massage notification, I already make the notifications steps,
but gives me this error
when I do dd($notifiable); I found all data
Undefined index: user_id OR
Undefined index: name
public function store(Request $request)
{
$chating=new chats();
$chating->chat = $request->input('chat');
$chating->user_id = Auth::id();
$chating->employee_id = $request->input('employeeid');
$chating->save();
$user_id=$request->input('employeeid');
auth()->user()->notify(new SendMassages($user_id));
return redirect()->back();
}
database notifications table coulmn data {"user_id":5,"name":"Ibrahim"}
Model
protected $user_id;
protected $name;
public function __construct($user_id)
{
$this->user_id = $user_id;
}
public function via($notifiable)
{
return ['database'];
}
public function toDatabase($notifiable)
{
return [
'user_id' => $this->user_id,
'user'=>$notifiable
];
}
View:
{{ $notification->data['name'] }}
Model:
class SendMassages extends Notification
{
use Queueable;
public $user;
public $user_id;
public function __construct($user_id)
{
$this->user_id = $user_id;
}
public function via($notifiable)
{
return ['database'];
}
public function toDatabase($notifiable)
{
// dd($notifiable);
return [
'user_id' => $this->user_id,
'user'=>$notifiable
];
}
}
It seems that you have problem with encapsulation in your code. First you have to make the property of your model into public if you want the quick and not-safe way but if you want the OOP way, You must write setters to do that logic for you.
First and not-safe approach :
class YourModel {
public $user_id;
.
.
.
}
The OOP way:
Class {
public function setUserId(int $userId)
{
$this->user_id = $userId;
}
.
.
.
}
if you are willing to get the exact answer please copy your controllerclass and model in here.

laravel notifications not storing into my database plus error

i couldn't store the notification into my notification table inside my database,
i was trying to make notification every time there is a new Post how can i make this work.
Error:
Notification:
use Queueable;
public $post;
public function __construct()
{
}
public function via($notifiable)
{
return ['database'];
}
public function toDatabase($notifiable)
{
return [
'title' => $this->post->title,
];
}
public function toArray($notifiable)
{
return [
];
}
}
Post Controller:
public function store(Request $request)
{
$this->validate($request, [
'title'=>'required|max:100',
'body' =>'required',
]);
$title = $request['title'];
$body = $request['body'];
$post = Post::create($request->only('title', 'body'));
Auth::user()->notify(new NotifyPost($post));
return redirect()->route('posts.index')
->with('flash_message', 'Article,
'. $post->title.' created');
}
You need to inject the post within the construct, or else it never gets resolved.
protected $post;
public function __construct(Post $post)
{
$this->post = $post;
}

pass data to laravel markdown with events

Im using events to trigger an event to send emails to the user, but im getting an error when i tried to pass extra data.
Order.php
protected $events = [
'created' => Events\NewOrder::class
];
NewOrder.php
use App\Order;
class NewOrder
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $order;
public function __construct(Order $order)
{
$this->order = $order;
}
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
the Listener:
public function handle(NewOrder $event)
{
Mail::to(Auth::user()->email)->send( new UserOrder( $event->pedido));
}
UserOrder.php
public function __construct(Order $order)
{
$this->order = $order;
$id_order = $order->id;
$details = DB::table('order_product')
->where('order_id', '=', $id_order)
->get();
$this->$details = $details;
}
public function build()
{
return $this->markdown('emails.userorder')->with('details', $this->details);
}
Markdown mail: userorder
#foreach($details as $detail)
{{ $detail->description }}
#endforeach
gave me this error:
Invalid argument supplied for foreach()
Change
$this->$details = $details;
to
$this->details = $details;
Also be sure that variable is declared above the __construct method, like so
public $details; //or protected $details;

Updating Pivot Table Laravel 5.4

I'm trying to save my Pivot but nothing happened, it doesnt prompt any error I dunno where to debug cause there is no prompt, any idea ? attached here is my code.
Controller note: Email and Name is updating properly.
public function update(Request $request, User $User)
{
$user=auth()->user();
$User->name = $request->get('name');
$User->email = $request->get('email');
$User->warehouse_id = $request->get('warehouse_id');
$User->role_id = $request->get('role_id');
$User->save();
$User->roles()->updateExistingPivot($request->get('role_id'), ['role_id' => $request->get('role_id')]);
$User->warehouse()->updateExistingPivot($request->get('warehouse_id'), ['warehouse_id' => $request->get('warehouse_id')]);
return redirect()->route('user.index');
}
Model for relationship
Warehouse
class Warehouse extends Model
{
protected $fillable = ['warehouse','warehouse_description','created_by'];
public $primaryKey='id';
public function users()
{
return $this->belongsToMany(User::class);
}
}
User
public function roles()
{
return $this->belongsToMany(Role::class);
}
public function warehouse()
{
return $this->belongsToMany(Warehouse::class);
}
Role
class Role extends Model
{
protected $fillable = ['role'];
public $primaryKey='id';
public function users()
{
return $this->belongsToMany(User::class);
}
}
What I did was putting isnerting the previews id before yusing updateExistingPivot
Here is my Controller
public function update(Request $request, User $User)
{
$User->name = $request->get('name');
$User->email = $request->get('email');
$User->warehouse_id = $request->get('warehouse_id');
$User->role_id = $request->get('role_id');
$User->roles()->updateExistingPivot($User->role_id, ['role_id' => $request->get('role_id')]);
$User->warehouse()->updateExistingPivot($User->warehouse_id, ['warehouse_id' => $request->get('warehouse_id')]);
$User->save();
return redirect()->route('user.index');
}

how to save data in multiple tables with the one form

I have 2 tables: customers and address one form to save data into this tables. For this, I made the relation like:
Address model
public function customer()
{
return $this->belongsTo(Customer::class);
}
customer model
public function address()
{
return $this->hasMany(Address::class);
}
How can i save the address in table? I already done with customer i can save, update and delete. I read the DOC of Eloquent of laravel, but i don't really get it.
Here is my controller
class CustomerController extends Controller
{
public function index()
{
// $customer = Customer::all();
$customer = Customer::with('address')->get();;
return view('customer.index', compact('customer'));
}
public function create(Address $address)
{
$address = $address->all();
return view('customer.create');
}
public function store(CustomerRequest $request , Customer $customer)
{
$customer = Customer::create($request->all());
return redirect()->route('customer.index',compact('customer'));
}
public function show(Customer $customer)
{
return view('customer.show',compact('customer'));
}
public function edit(Customer $customer)
{
return view('customer.edit', compact('customer'));
}
public function update(CustomerRequest $request, $id)
{
$customer = Customer::find($id)->update($request->all());
return redirect()->route('customer.index',compact('customer'));
}
public function destroy($id)
{
Customer::destroy($id);
return redirect()->route('customer.index');
}
}
Any suggestions would be appreciated!!

Resources