Parsing data in view layouts laravel 6 - laravel

I want to make a profile photo on the admin page, this photo is in the layouts.template file, how can I get the $profil to be sent to the layouts.template page?
#if($profil->upload!=null)
<img src="{{asset('backend/assets/img/{{$profil->upload}}.jpg')}}" alt="..." class="avatar-img rounded-circle">
#else
<img src="{{asset('backend/assets/img/mlane.jpg')}}" alt="..." class="avatar-img rounded-circle">
#endif
and i created $profil in UserController
$auth = Auth::user()->id;
$profil = Profil_user::where('user_id',$auth)->first();
how do I get my $profil to be used in layouts.template

You can share variable along all views inside below file
app/Providers/AppServiceProvider.php
public function boot()
{
//Define your user auth call here
if(confition){
$profile_pic = 'admin.png';
} else {
$profile_pic = 'default.png';
}
\View::composer('*', function($view){
$view->with('profile_pic', $profile_pic);
});
}

Related

Laravel pass param url id to controller function

I have a page with data/information on it with a download button that converts my page to pdf. Now, the download button is my problem with how I can do it.
My current page URL.
Route::get('/inventory/{id}', function ($id) {
$inventory = Inventory::find($id);
return view('layouts._inventory-template', ['inventory' => $inventory])
});
My button in the view
<span></span>PDF
Function in ClientController
public function viewPdf($id)
{
$inventory = Inventory::find($id);
$pdf = PDF::loadView('layouts._inventory-template');
return $pdf->download('inventory.pdf');
}
Question: How I can implement the logic of PDF download button in the view page?
In your router:
Route::get('/viewPdf/{id}', 'ClientController#viewPdf');
You can simple call your route in href:
<span></span>PDF
I would suggest:
Route::get('/inventory/{id}', 'ClientController#downloadPdf')->name('download-pdf');
ClientController
public function downloadPdf()
{
$inventory = Inventory::find($id);
$pdf = PDF::loadView('layouts._inventory-template', $inventory);
return $pdf->download('inventory.pdf');
}
Button
Download<i class="la la-download"></i>
Note: change Auth::user()->id in the button if ever you want something else
Simply add download property in a tag
<a href="{{ action('ClientController#viewPdf', ['id' => $request()->route('id')]) }}" class="button button-secondary" download><span></span>PDF</a>

How to filter type to display in view

I have a partners-customers table. Every record that is inserted whether is partner (type '1') or customer (type '2') has logo image and a checkbox to decide if the logo will be displayed in the homepage or not.
The homepage has 2 different carousel slide to display partners and customers based on their type.
How do I:
Filter the type so that partners and customers will be displayed in their own carousel slide in blade view.
Manage to display any partner/customer's logo with the checkbox (boolean, return 1 to display, 0 otherwise).
I would go with this approach:
//My Controller File
public function index()
{
// type = 1: partners
$partners = PartnersCustomer::whereType(1)->get();
// type =2 : customers
$customers = PartnersCustomer::whereType(2)->get();
return view('my-blade-file-path')->with('partners', $partner)->with('customers' , $customers);
}
Inside your blade template
<!-- INSIDE YOUR BLADE TEMPLATE -->
#foreach($customers as $customer)
#if($customer->display_logo == 1)
<img src="{{ $customer->logo_path}}" />
#endif
#endforeach
The approach should be like this :
Controller Function
public function getImage()
{
$data = (new PartnersCustomer)->get();
$customer = data['image'];
if ($data['type'] == 1) {
$partner = data['image'];
}
return view('index.blade.php')->compact('customer', 'partner');
}
View File
<img src="{{ $customer->path_of_logo}}" />
<img src="{{ $partner->path_of_logo}}" />

laravel display data on a page based on id

I have a page that shows links with name of businesses that are retrieved in database like this:
Controller:
public function viewBusiness() {
// Return our "website" object
$business = Business::all();
// Pass the contents of the "html" property to the view
return view('viewBusiness', ['business' => $business]);
}
View:
#extends('master') #section('title', 'Live Oldham') #section('content')
#section('content')
#foreach ($business as $businesses)
<a target="_blank" href="{{ url('business/' . $businesses->name) }}"> {{($businesses->name) }}
</a> #endforeach
#endsection
Route:
Route::get('business/list', 'BusinessController#viewBusiness')->name('viewBusiness');
I then have added a function where user click on a link and it is taken to a page which displays all data for that specific business, however it diplays all data but for all businesses.
Controller:
function displayBusiness() {
$business = Business::all();
$address = Address::all();
return view('displayBusiness', ['business' => $business, 'address' => $address]);
}
View:
#foreach ($business as $businesses)
<p>{{$businesses->name}}</p>
<p>{{$businesses->email}}</p>
#endforeach
#foreach ($address as $addresses)
<p>{{$addresses->firstline_address}}</p>
<p>{{$addresses->secondline_address}}</p>
<p>{{$addresses->town}}</p>
<p>{{$addresses->city}}</p>
<p>{{$addresses->postcode}}</p>
<p>{{$addresses->telephone}}</p>
#endforeach
Route:
Route::get('business/{name}', 'BusinessController#displayBusiness')->name('displayBusiness');
Now here's the question, how can this code be modified so only a business that match either bussiness->name or business->id is displayed. (I guess name is taken when user clicks on a name.
Another question is how to restrict the url so that if localhost/business/{name} is not equal to any business->name in the database returns error? at the moment it shows the page no matter what you enter.
Thanks!
I do not know if I understood the question, but that may be the beginning of a solution ...
First view :
#extends('master') #section('title', 'Live Oldham')
#section('content')
#foreach ($business as $businesses)
<a target="_blank" href="{{ url('business/' . $businesses->id) }}"> {{($businesses->name) }}
</a> #endforeach
#endsection
Second Controller :
function displayBusiness($id) {
$business = Business::find($id);
$address = Address::find($id);
return view('displayBusiness', compact('business', 'address'));
}
Second View :
<p>{{$business->name}}</p>
<p>{{$business->email}}</p>
<p>{{$address->firstline_address}}</p>
<p>{{$address->secondline_address}}</p>
<p>{{$address->town}}</p>
<p>{{$address->city}}</p>
<p>{{$address->postcode}}</p>
<p>{{$address->telephone}}</p>
Second Route :
Route::get('business/{id}', 'BusinessController#displayBusiness')->name('displayBusiness');
Route parameters are available in the controller function as parameters. Now you can build a query with this function. If your query does not return any results, you can send the user back to the business overview.
function displayBusiness($name) {
$business = Business::where('name', $name)->orWhere('id', $name)->first();
if ($business === null)
{
// No business with this name or id found.
// Redirect to businesses list page.
}
$address = Address::all();
return view('displayBusiness', ['business' => $business, 'address' => $address]);
}

How to update image file in laravel?

I am trying to update an image of a database (One-To-Many and Belongs-To Relationships)
i have table proposition with fields id, proposition.
now the proposition table has relations with image table like this
image module
public function proposition()
{
return $this->belongsTo('App\Proposition');
}
proposition module
public function propositionimage() {
return $this->hasMany('App\PropositionImages');
}
now i want to update the image to an proposition:
i want to get the old value of input file and change it with the new value of input file .
the probleme is whene i use this
$proposition = Proposition::find($num_proposition);
$image = $proposition->propositionimage()->get();
it showing all the image of the proposition
i want to update all the image but one by one , any help plz ?
the HTML code
<img src="/images/{{ $propositonimage->imagename }}" alt="">
<div class="caption">
<input type="file" name="image" value="{{$propositonimage->imagename}}" >
</div>
the update function
public function update(PropositionRequest $request, $num_proposition)
{
$proposition = Proposition::find($num_proposition);
$image = $proposition->propositionimage()->get();
// echo "$image";
$images = Input::file('image');
if (Input::hasfile('image')) {
$rules2 = array('image' => 'mimes:jpeg,bmp,png');
$validator2 = Validator::make($images, $rules2);
if ($validator2->passes()) {
$distinationPath = 'images';
$imagename = $images->getClientOriginalName();
$upload_success = $images->move($distinationPath, $imagename);
$extension = $images->getClientOriginalExtension();
}
}
Im not 100% sure what the problem is. But it seems as you want to iterate the collection over the div?
proposition model
public function propositionimage()
{
return $this->hasMany(PropositionImages::class);
}
image model
public function proposition()
{
return $this->belongsTo(Proposition::class);
}
Then you return a view with proposition in a controller, and eager load PropositionImages?
public function index(){
$propositions = \Proposition::with('propositionimage')->get();
return view('image', compact('propositions'));
}
You will have a collection of proposition with corresponding images.
#forelse ($propositions as $propositon)
<img src="/images/{{ $propositon->imagename }}" alt="">
#if($propositon->propositionimage->count())
<div class="caption">
#foreach($propositon->propositionimage as $propositionimage)
name: {{$propositionimage->imagename}}<br>
<input type="file" name="image" value="{{$propositionimage->imagename}}" >
#endforeach
</div>
#else
No proposition image <br>
#endif
#empty
No images :(
#endforelse
Please add more details to your question otherwise.
Edit
I tried this at my local dev machine and it works.
All relations of propositionimage will be iterated to the corresponding proposition.

Laravel view register not found

working on someone's code, figuring out where stuff goes, etc.
the error I am getting:
View [register] not found. (View: /Users/striker/bankproject/app/views/frontend/default/pages/profile.blade.php)
I see that the $sections is being passed from the controller to the view.
Here is the route
Route::get('register',['as'=>'register.create','uses'=>'UsersController#create']);
Route::post('register',['as'=>'register.store','uses'=>'UsersController#store']);
Here is the controller
use Forret\Interfaces\UserInterface;
use View;
use Sentry;
class UsersController extends BaseController{
public function __construct(UserInterface $user){
$this->user = $user;
}
public function show($user_id){
$view['title'] = 'Forret - Profile';
$view['auth_navbar'] = 1;
$view['user'] = Sentry::getUser();
$view['sections'] = ["Frontend::components.sections.profile"];
return View::make('Frontend::pages.profile',$view);
}
public function create(){
$view['title'] = 'Forret - Register';
$view['auth_navbar'] = 0;
$view['user'] = [];
$view['sections'] = ['register'];
return View::make('Frontend::pages.profile',$view);
}
}
Here is the view profile.php
#extends('Frontend::layouts.default')
#section('page_styles')
#stop
#section('content')
#foreach($sections as $section)
#include($section)
#endforeach
#stop
#section('page_scripts')
#stop
Here is the view register.php
#extends('Frontend::layouts.default')
#section('page_styles')
#stop
#section('content')
#foreach($sections as $section)
#include("Frontend::components.sections.$section")
#endforeach
#stop
#section('page_scripts')
#stop
Change the #include in the profile template to:
#include("Frontend::components.sections.$section")

Resources