I have this code which store offer and maxoffer but I can't use it into my Mail function:
public function store(Requests\OfferRequest $request)
{
$offer = new Offer($request->all());
Auth::user()->offer()->save($offer);
$maxoffer = Maxoffer::where('article_id', $request->input('article_id'))
->where('start', Carbon::createFromFormat('m/d/Y h:i a', $request->input('start')))
->first();
//dd($maxoffer);
if($maxoffer == null)
{
Auth::user()->maxoffer()->create($request->all());
}
else
{
if($maxoffer->price < $request->input('price'))
{
$key = '';
$newOffer = Maxoffer::where('id', $maxoffer->id)
->update(['price'=>$request->input('price'),'user_id'=>Auth::user()->id, 'key'=>$key, 'provera'=>$request->input('provera')]);
}
}
Alert::success('Keep looking for best rates. Good luck...', 'Thanks for bidding!')->persistent("Close");
$user = Auth::user();
Mail::send('emails.newoffer', compact('user', 'maxoffer'), function ($m) use ($user) {
$m->from('info#sss.com', $maxoffer->article()->hname);
$m->to($user->email, $user->name)->subject('Someone have the bigger offer than you');
});
return Redirect::back();
}
so In Maxoffer controller I have:
public function user(){
return $this->belongsTo('App\User');
}
public function article(){
return $this->belongsTo('App\Article');
}
but in Mail function I cant use it. WHY?
Why $maxoffer->article()->hname inside Mail:: is a problem...
laravel error:
i get errors: ErrorException in 22b7e7ff4b942f1d8fa25f9b1c9a1748 line 6: Undefined property: Illuminate\Database\Eloquent\Relations\BelongsTo::$hname (View: /var/www/html/resources/views/emails/newoffer.blade.php)
Pass $maxoffer to the closure. like this use($user, $maxoffer)
Mail::send('emails.newoffer', compact('user', 'maxoffer'), function ($m) use ($user, $maxoffer) {
$m->from('info#sss.com', $maxoffer->article()->hname);
$m->to($user->email, $user->name)->subject('Someone have the bigger offer than you');
});
Related
I have a project in Laravel 7 that has a column in the user table called organ_id that has different numbers for different users (1,2,...)
Now I want to use organ_id inside my controller, which is as follows, and by using if, different information is given to each user according to the organ number.
ProductsController.php
namespace App\Http\Controllers;
use App\User;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
if (Auth::check())
{
$organ_id = Auth::user()->getId();
}
class ProductsController extends Controller
{
public function test(Request $request) {
if ($organ_id == 1) {
function orders(Request $request) {
$data = DB::table('products_1')
->orderBy('id', 'asc')
->paginate(14);
return view('products', compact('data'));
}
function posts(Request $request) {
$data = DB::table('posts')
->orderBy('id', 'asc')
->paginate(25);
return view('posts', compact('data'));
}
/*
other functions
.
.
.
*/
}
if ($organ_id == 2) {
function orders(Request $request) {
$data = DB::table('products_2')
->orderBy('id', 'asc')
->paginate(14);
return view('products', compact('data'));
}
function posts(Request $request) {
$data = DB::table('posts')
->orderBy('writer_id', 'asc')
->paginate(10);
return view('posts', compact('data'));
}
/*
other functions
.
.
.
*/
}
}
}
User.php
public function getId()
{
return $this->organ_id;
}
Since it is not possible to use if in the controller, I wrote the public function test() and put if inside this function.
Unfortunately, in this case, the browser can not find the orders function.
you can do that
$organ_id = User::where('id',auth()->user()->id)->pluck('organ_id')->first();
if ($organ_id == 1) {
//code
}
I'm having the following error when I try to check a permission using policies:
Too few arguments to function App\Policies\AnswerPolicy::view(), 1 passed in /Users/georgio/Projects/Laravel/municipality-app/vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php on line 710 and exactly 2 expected (View: /Users/georgio/Projects/Laravel/municipality-app/resources/views/layouts/loggedin.blade.php)
AnswerPolicy
<?php
namespace App\Policies;
use App\Answer;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class AnswerPolicy
{
use HandlesAuthorization;
public function view(User $user, Answer $answer)
{
return true;
}
public function edit(User $user)
{
return true;
}
public function create(User $user)
{
return true;
}
public function delete(User $user)
{
return true;
}
public function update(User $user)
{
return true;
}
}
AnswersController
<?php
namespace App\Http\Controllers;
use App\Question;
use App\Answer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AnswersController extends Controller
{
public function index()
{
$this->authorize('view');
return view('answers.show')->with('questions', Question::all());
}
public function create($questionId)
{
$this->authorize('view-submit-answer');
return view('answers.create')->with('question',Question::find($questionId));
}
public function store()
{
$this->authorize('');
$this->validate(request(),[
'answer' => 'required',
]);
$data = request()->all();
$answer = new Answer();
$answer->answer = $data['answer'];
$answer->question_id = $data['question_id'];
$answer->user_id = Auth::id();
$answer->save();
return redirect('/questions/answers');
}
public function edit($answerId)
{ $this->authorize('view-edit-answer');
return view('answers.edit')->with('answer', Answer::find($answerId));
}
public function update($answerId)
{
$this->validate(request(),[
'answer' => 'required',
]);
$data = request()->all();
$answer = Answer::find($answerId);
$answer->answer = $data['answer'];
$answer->save();
return redirect('/answers-question');
}
public function destroy($questionID)
{
$this->authorize('delete-answer');
$answer = Answer::where('question_id', $questionID)->where('user_id', Auth()->id());
$answer->delete();
return redirect('/questions/answers');
}
}
loggedin.blade.php
This is only the part of my code that is causing the error
#can('view', App\Answer::class)
<li>
<a href="/questions/answers">
<span><i class="fa fa-pencil"></i></span>
<span>Answer Question</span>
</a>
</li>
#endcan
I even tried replacing $answer with App\Answer::class and I still had the exact same error.
You can check the full error stack at:
https://flareapp.io/share/xPQQQXP1#F66
The documentation says
When defining policy methods that will not receive a model instance, such as a create method, it will not receive a model instance. Instead, you should define the method as only expecting the authenticated user
I don't see an Answer instance inside your #can statement there. Does one exist? If so, you should be doing this:
#can('view', $answer)
Or, if one doesn't exist at that point, define your method like this
public function view(User $user)
And call it like this:
#can('view', \App\Answer::class)
Seeking for your help again. Just wondering for any idea's to return something if the column is NULL. Please see below codes: Thank you!
Application Controller
public function index()
{
$user_id = Auth::user()->id;
$applications = application::where('user_id', '=', $user_id)->first()->get();
if (empty($applications)) {
return redirect(route('user.application.index'))->with('message','Application is Succesfully');;
}
else{
return view('user.application.index',compact('applications'));
}
}
If user is not authenticated then you will get unexpected error. So first check the auth as Auth::check()
Try this :
use Illuminate\Support\Facades\Auth;
public function index()
{
if(!Auth::check()) {
return view('login');
}
$applications = Application::where('user_id', $Auth::user()->id)->first();
if (empty($applications)) {
return redirect(route('user.application.index'))->with('message','Application is Succesfully');;
}
else{
return view('user.application.index',compact('applications'));
}
}
For example I can use:
$address = $user->address
which will return the addresses for the user.
// User.php (model)
public function address()
{
return $this->hasMany(Address::class, 'refer_id', 'id');
}
public function billingAddress()
{
return $this->address()
->where('type', '=', 1)
->where('refer_id', '=', $this->id)
->first();
}
However, I would like to return the BillingAddress for the user depending on this where clause. How do I do it?
EDIT:
If I use this inside... OrderController#index it returns correctly
$orders = Order::with('order_fulfillment', 'cart.product', 'user.address', 'payment')->get();
return new OrderResource($orders);
However, If I change it to:
$orders = Order::with('order_fulfillment', 'cart.product', 'user.billingAddress', 'payment')->get();
return new OrderResource($orders);
I get this error:
Symfony\Component\Debug\Exception\FatalThrowableError
Call to a member function addEagerConstraints() on null
One option is you can use whereHas in your query. For example,
$orders = Order::with('order_fulfillment', 'cart.product', 'user.address', 'payment')
->whereHas(
'address', function ($query) {
$query->where('type', '=', 1)
->first();
}
)->get();
return new OrderResource($orders);
This is one option. try to dd($orders) an find if its working.
You had an another option like this, in your model
public function address()
{
return $this->hasMany(Address::class, 'refer_id', 'id');
}
Add relations like
public function billingAddress()
{
return $this->hasOne(Address::class, 'refer_id', 'id')->where('type', 1);
}
And
public function shippingAddress()
{
return $this->hasOne(Address::class, 'refer_id', 'id')->where('type', 2);
}
Then in your query,
$orders = Order::with('order_fulfillment', 'cart.product', 'user.address','user.billingAddress', 'user.shippingAddress', 'payment')->get(); return new OrderResource($orders);
I put logic from my function index() of UserController in trait taht i created:
public function index()
{
$this->authorize('view', Auth::user());
$users = QueryBuilder::for(User::class)
->allowedIncludes('kids','roles','articles','recordings')
->allowedFilters('first_name', 'last_name', 'email')
->get();
return UserResource::collection($users);
}
and this is my trait :
<?php
namespace App\Http\Traits;
use App\Models\User;
use Spatie\QueryBuilder\QueryBuilder;
trait Filterable
{
public function filter()
{
$users = QueryBuilder::for(User::class)
->allowedIncludes('kids','roles','articles','recordings')
->allowedFilters('first_name', 'last_name', 'email')
->get();
return $users;
}
}
So now my function index() looks like this:
use Filterable;
public function index()
{
$this->authorize('view', Auth::user());
$users = $this->filter();
return UserResource::collection($users);
Now when i do this in my postman
{{url}}/api/users?filter[first_name]=anna
it works and it returns anna from my database but when I try
{{url}}/api/users?include=roles
it return every user from database but does not include roles.
Can somebody help me with this?
This is taken straight from the github page: https://github.com/spatie/laravel-query-builder#custom-filters
Custom filters
use Spatie\QueryBuilder\Filters\Filter;
use Illuminate\Database\Eloquent\Builder;
class FiltersUserPermission implements Filter
{
public function __invoke(Builder $query, $value, string $property) : Builder
{
return $query->whereHas('permissions', function (Builder $query) use ($value) {
$query->where('name', $value);
});
}
}
use Spatie\QueryBuilder\Filter;
// GET /users?filter[permission]=createPosts
$users = QueryBuilder::for(User::class)
->allowedFilters(Filter::custom('permission', FiltersUserPermission::class))
->get();
// $users will contain all users that have the `createPosts` permission