My problem is this I am using following codes to access users to the system in different roles.
public function show($id)
{
if (Permission::where('status', 1)->where('project_id', $id)->exists()) {
// if((Permission::where('status', '=', '1')->first()) && (Permission::where('project_id','=',$id)->first())){
$project = Project::find($id);
$tasks = $this->getTasks($id);
$files = $this->getFiles($id);
$comments = $this->getComments($id);
$collaborators = $this->getCollaborators($id);
$permissions = $this->getPermissions($id);
returnview('collaborators.show')->withProject($project)->withTasks($tasks)->withFiles($files)->withComments($comments)->withCollaborators($collaborators);
}
else if
//return('hi');
(Permission::where('status', 2)->where('project_id', $id)->exists()) {
$project = Project::find($id);
$tasks = $this->getTasks($id);
$files = $this->getFiles($id);
$comments = $this->getComments($id);
$collaborators = $this->getCollaborators($id);
$permissions = $this->getPermissions($id);
return view('collaborators.manager')->withProject($project)->withTasks($tasks)->withFiles($files)->withComments($comments)->withCollaborators($collaborators);
}
In My permission table there is collaborator_id and it is same to users table user id. I need validate (compare) collaborator_id with logged user Auth::user->id. in following scripts.
if (Permission::where('status', 1)->where('project_id', $id)->exists())
how can do it?
Just use another where clause,
Permission::where('status', 1)->where('project_id', $id)->where('collaborator_id', Auth::User()->id)
Suggestion: You can reduce your code by avoiding same/duplicate code in if and else.
Related
I create order system in Laravel, I can add new order as user and I can display all order as admin user. I have only problem with edit this order as user.
This is my controller
public function index(){
$orders = Orders::with('category', 'type')->where('user_id', auth()->user()->id)->orderBy('id', 'desc')->paginate(6);
return view('account.orders', compact('orders'));
}
public function store(Request $request) {
$order = new Orders;
$order->user_id = auth()->user()->id;
$order->category_id = $request->input('category_id');
$order->type_id = $request->input('type_id');
$order->name = $request->input('name');
$order->description = $request->input('description');
$order->price = $request->input('price');
$order->save();
return back()->with([
'status'=> [
'type'=>'success',
'content'=>'Zmiany zostaĆy zapisane',
]
]);
}
public function edit(Orders $order_id){
$categories = Categories::get();
$types = OrdersCategories::get();
$order = Orders::where('user_id', auth()->user()->id)->findOrFail($order_id);
return dd($order_id);
}
When I click edit in user dashboard not show data from database.
Try This,in your controller edit function.
try {
$categories = Categories::get();
$types = OrdersCategories::get();
$order = Orders::where('user_id', auth()->user()->id)
->findOrFail($order_id);
return redirect()->action('Admin\OrderController#index');
} catch (\Exception $exception) {
return back()->withError($exception->getMessage())->withInput();
}
Because your model is called Orders. You should be using /{orders}/ instead of /{order}/ in your routes if you want to make use of route-model binding.
Change the route to Route::get('/{orders}/edit', 'OrderController#edit')->name('edit');
I'm creating a wishlist where the user can save his own products and save them. What I want is that a user can't save a product more than one time.
So, what I'm doing is, if the product_id exists in the DB, I don't want to save the new record.
This is my code, what am I doing wrong?
public function addWishlist(Wishlist $wishlist) {
$wishlist->user_id = request('user_id');
$wishlist->product_id = request('product_id');
if(DB::table('wishlists')->where('product_id' !== $wishlist->product_id)) {
$wishlist->save();
return redirect()->back();
}
}
what about this
public function addWishlist(Wishlist $wishlist) {
$userId = request('user_id');
$productId = request('product_id');
$found = Wishlist::where('user_id', $userId)->where('product_id', $productId)->count();
if($found == 0) {
$wishlist->user_id = $userId;
$wishlist->product_id = $productId;
$wishlist->save();
return redirect()->back();
}
}
public function addWishlist(Wishlist $wishlist) {
if(DB::table('wishlists')->where(['user_id'=>$user_id,'product_id'=>$wishlist->product_id])->exists()){
// record already exist
return redirect()->back();
}else{
// record not exist , save the record
$withlist = new Wishlist();
$wishlist->user_id = request('user_id');
$wishlist->product_id = request('product_id');
$wishlist->save();
return redirect()->back();
}
}
You can use firstOrNew and you need to check with user_id and product_id combination
$wishlist::firstOrNew(array('user_id' => request('user_id'),'product_id'=>request('product_id')));
$wishlist->user_id = request('user_id');
$wishlist->product_id = request('product_id');
$wishlist->save();
return redirect()->back();
Have you tried firstOrCreate()
$user = User::firstOrCreate(['email' => $email])
I am having trouble understanding and knowing how to create a one to one relationship with CRUD and file upload together, is it possible to give me a simple example on how to do it? Because when I tried to do it, for some reason my id and user_id doesn't match at all
Example, in user_info table (jack with id 1) has one userImage table(image uploaded with a user_id = 1).
This is the part that I am having trouble with in my controller:
public function store1(Request $request){
$this->validate($request, [
'input_img' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$user_info = Session::get('data');
$UserImage = new UserImage($request->input()) ;
if($file = $request->hasFile('input_img')) {
$file = $request->file('input_img');
$fileName = $file->getClientOriginalExtension() ;
$destinationPath = public_path().'/images' ;
$file->move($destinationPath,$fileName);
$UserImage->userImage = $fileName ;
$UserImage = UserImage::create(['file' => $request->file('input_img')]);
$UserImage->user_infos()->associate($user_info);
}
$UserImage->save() ;
//dd($UserImage);
return redirect('/home');
}
Sample of one to one relationship. I wish it can help you.
Example of table of Column of user table:
id
name
email
password
Example of table of Column of userimage table:
id
user_id
image_link
uploaded
User Model
public function userImage()
{
return $this->hasOne(App\UserImage::class);
}
UserImage Model
public function user()
{
return $this->hasOne(App\User::class);
}
CRUD in controller
public function store(Request $request)
{
$user = new App\User;
$user->name = $request->name;
$user->email = $request->email;
$user->password = $request->password;
$user->save();
$user->userImage->create([
'image_link' => $request->image,
'uploaded' => $request->date,
]);
return back();
}
public function update(Request $request, $id)
{
$user = App\User::find($id);
$user->name = $request->name;
$user->email = $request->email;
$user->password = $request->password;
$user->save();
$user->userImage->image_link = $request->image;
$user->userImage->uploaded = $request->date;
$user->userImage->save();
return back();
}
public function destroy($id)
{
$user = App\User::find($id)->delete();
return back();
}
Create a foreign key in your user_info table referencing userImage table.
Create unique constraint on the foreign key.
I have created a wishlist for a shopping cart.So when a non authenticate user wants to see his wishlist after he added to the wishlist. How to show the wishlist based on him before he logged in?
This is my wishlist i have made for only authenticate users:
public function addWish(Request $request)
{
if(Auth::check()){
$name = $request->name;
$product = Product::where('name' , '=', $name)->first();
$product_id = $product->id;
$product = DB::table('wishes')
->where('wishes.product_id','=',$product_id)
->where('wishes.status','=',1)
->select('wishes.product_id')
->first();
if(!$product){
$wish = new Wish();
$wish->user_id =Auth::user()->id;
$name = $request->name;
$product = Product::where('name' , '=', $name)->first();
$wish->product_id = $product->id;
// $product = Product::find($cart->product_id);
$wish->price =$product->price;
$wish->status = 1;
$wish->save();
return redirect('shop-wish');
}
else{
return redirect('shop-wish');
}
}
And this one is for show the list :
public function getWishPage()
{
$id = Auth::user()->id;
$wishList = \DB::table('wishes')
->join('products','wishes.product_id','products.id')
->select('products.feature_image','products.name','products.price as p_price','wishes.id')
->where('wishes.status','=',1)
->where('wishes.user_id','=',$id)
->get();
return view('cart.wishlist',compact('wishList'));
}
But how do i show the non-authenticate users wishlist? Any suggestion or solution would be appreciable?
public function addWish(Request $request)
{
if (Auth::check()){
// ...
} else {
$name = $request->name;
$product = Product::where('name' , '=', $name)->first();
$product_id = $product->id;
if (\Session::has("wishList.$product_id") === true) {
return redirect('shop-wish');
}
\Session::put("wishList.$product_id", $product_id);
return redirect('shop-wish');
}
}
and:
public function getWishPage()
{
if (Auth::check()) {
$wishListId = \Session::get('wishList');
dd($wishListId);
} else {
$id = Auth::user()->id;
$wishList = \DB::table('wishes')
->join('products','wishes.product_id','products.id')
->select('products.feature_image','products.name','products.price as p_price','wishes.id')
->where('wishes.status','=',1)
->where('wishes.user_id','=',$id)
->get();
return view('cart.wishlist',compact('wishList'));
}
}
I try to make a search engine for users. The search will be with multiple fields so as the user can be selecting whatever he want and get the result.
routes.php:
Route::get('search/{tag?}/{town?}/{education?}/{contract?}', 'DisplayJobs#getSearch');
DisplayJobs.php Controller
public function getSearch($tag = null, $town = null, $education = null, $contract = null)
{
//get already database values to send them to the form
$tags = \App\Tag::lists('name', 'id');
$contract = \App\Contract::lists('name', 'id');
$towns = \App\Town::lists('name', 'id');
$education = \App\Education::lists('name', 'id');
$tagQueryBuilder = Tag::query();
$townQueryBuilder = Town::query();
$educationQueryBuilder = Education::query();
$contractQueryBuilder = Contract::query();
if(Input::has('tag'))
{
$tagQueryBuilder->TagOfUser(Input::get('tag'));
}
if(Input::has('town'))
{
$townQueryBuilder->TownOfUser(Input::get('town'));
}
if(Input::has('education'))
{
$educationQueryBuilder->EducationOfUser(Input::get('education'));
}
if(Input::has('contact'))
{
$contractQueryBuilder->ContactOfUser(Input::get('contact'));
}
return view('main.search_jobs', compact('tags', 'towns', 'contract', 'education'));
}
If I try with each single query it works perfectly but I want to combined result data from all the queries or a way to query all the data at once.
In each model I have my query scope like this (Tag.php) Model:
public function jobs()
{
return $this->belongsToMany('App\Job');
}
public function scopeTagOfUser($query, $tag)
{
return $query->where('id', '=', $tag)->with('jobs');
}
After a lot of hours I found a solution. I will post it below so if anyone has the same problem can see one solution.
First I have delete all of the scope queries in the models and all of the work completed to the controller like bellow:
public function getSearch($tag = null, $town = null, $education = null, $contract = null)
{
//get already database values to send them to the form
$tags = \App\Tag::lists('name', 'id');
$towns = \App\Town::lists('name', 'id');
$contract = \App\Contract::lists('name', 'id');
$education = \App\Education::lists('name', 'id');
//get inputs from users
$getTagFromUser = Input::get('tag');
$getTownFromUser = Input::get('town');
$getContractFromUser = Input::get('contract');
$getEducationFromUser = Input::get('education');
$tagQuery = DB::table('jobs')
->join('job_tag', 'jobs.id', '=', 'job_tag.job_id')
->join('tags', 'job_tag.tag_id', '=', 'tags.id')
->where('tags.id', '=', $getTagFromUser);
$townQuery = DB::table('jobs')
->join('job_town', 'jobs.id', '=', 'job_town.job_id')
->join('towns', 'job_town.town_id', '=', 'towns.id')
->where('towns.id', '=', $getTownFromUser);
$contractQuery = DB::table('jobs')
->join('job_contract', 'jobs.id', '=', 'job_contract.job_id')
->join('contracts', 'job_contract.contract_id', '=', 'contracts.id')
->where('contracts.id', '=', $getContractFromUser);
$educationQuery = DB::table('jobs')
->join('job_education', 'jobs.id', '=', 'job_education.job_id')
->join('education', 'job_education.education_id', '=', 'education.id')
->where('education.id', '=', $getEducationFromUser);
$finalQuery = $tagQuery->union($townQuery)->union($contractQuery)->union($educationQuery)->get();
return view('main.search_jobs', compact('tags', 'towns', 'contract', 'education', 'finalQuery'));
}