You could do it with volumetrics but as your image looks rather cartoony, what you can do is create a cylinder and give it a material with a mix between Emission and Transparent BSDF with a Separate XYZ as factor:
If you want to add transparency to the periphery, add a new Mix Shader, plug the Transparent BSDF and use an Input > Layer Weight node as factor:
If you use Eevee you can put a bit of Bloom effect, and to have transparency you'll need to choose the Alpha Blend option in the Material panel > Settings > Blend Mode.
For the object blocking the ray you could cheat with an object that you put above the obstacle, give it a mix between Emission and Transparency:
A more realistic way to do it is to use volumetrics (works for both Cycles and Eevee): Create a cube, give it a material with a Shader > Principled Volume that you plug into the Volume input of the Material Output. Lower down the Principled Volume Density value. Create a Spot light.
If you don't want the volumetric to affect other parts of your scene, and if you want straight rays, give your volumetric material to a cylinder (put underneath an Area light with Shape > Disk):